atxp1.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. atxp1.c - kernel module for setting CPU VID and general purpose
  3. I/Os using the Attansic ATXP1 chip.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/init.h>
  18. #include <linux/module.h>
  19. #include <linux/jiffies.h>
  20. #include <linux/i2c.h>
  21. #include <linux/hwmon.h>
  22. #include <linux/hwmon-vid.h>
  23. #include <linux/err.h>
  24. #include <linux/mutex.h>
  25. #include <linux/sysfs.h>
  26. MODULE_LICENSE("GPL");
  27. MODULE_DESCRIPTION("System voltages control via Attansic ATXP1");
  28. MODULE_VERSION("0.6.3");
  29. MODULE_AUTHOR("Sebastian Witt <se.witt@gmx.net>");
  30. #define ATXP1_VID 0x00
  31. #define ATXP1_CVID 0x01
  32. #define ATXP1_GPIO1 0x06
  33. #define ATXP1_GPIO2 0x0a
  34. #define ATXP1_VIDENA 0x20
  35. #define ATXP1_VIDMASK 0x1f
  36. #define ATXP1_GPIO1MASK 0x0f
  37. static const unsigned short normal_i2c[] = { 0x37, 0x4e, I2C_CLIENT_END };
  38. static int atxp1_probe(struct i2c_client *client,
  39. const struct i2c_device_id *id);
  40. static int atxp1_remove(struct i2c_client *client);
  41. static struct atxp1_data * atxp1_update_device(struct device *dev);
  42. static int atxp1_detect(struct i2c_client *client, struct i2c_board_info *info);
  43. static const struct i2c_device_id atxp1_id[] = {
  44. { "atxp1", 0 },
  45. { }
  46. };
  47. MODULE_DEVICE_TABLE(i2c, atxp1_id);
  48. static struct i2c_driver atxp1_driver = {
  49. .class = I2C_CLASS_HWMON,
  50. .driver = {
  51. .name = "atxp1",
  52. },
  53. .probe = atxp1_probe,
  54. .remove = atxp1_remove,
  55. .id_table = atxp1_id,
  56. .detect = atxp1_detect,
  57. .address_list = normal_i2c,
  58. };
  59. struct atxp1_data {
  60. struct device *hwmon_dev;
  61. struct mutex update_lock;
  62. unsigned long last_updated;
  63. u8 valid;
  64. struct {
  65. u8 vid; /* VID output register */
  66. u8 cpu_vid; /* VID input from CPU */
  67. u8 gpio1; /* General purpose I/O register 1 */
  68. u8 gpio2; /* General purpose I/O register 2 */
  69. } reg;
  70. u8 vrm; /* Detected CPU VRM */
  71. };
  72. static struct atxp1_data * atxp1_update_device(struct device *dev)
  73. {
  74. struct i2c_client *client;
  75. struct atxp1_data *data;
  76. client = to_i2c_client(dev);
  77. data = i2c_get_clientdata(client);
  78. mutex_lock(&data->update_lock);
  79. if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
  80. /* Update local register data */
  81. data->reg.vid = i2c_smbus_read_byte_data(client, ATXP1_VID);
  82. data->reg.cpu_vid = i2c_smbus_read_byte_data(client, ATXP1_CVID);
  83. data->reg.gpio1 = i2c_smbus_read_byte_data(client, ATXP1_GPIO1);
  84. data->reg.gpio2 = i2c_smbus_read_byte_data(client, ATXP1_GPIO2);
  85. data->valid = 1;
  86. }
  87. mutex_unlock(&data->update_lock);
  88. return(data);
  89. }
  90. /* sys file functions for cpu0_vid */
  91. static ssize_t atxp1_showvcore(struct device *dev, struct device_attribute *attr, char *buf)
  92. {
  93. int size;
  94. struct atxp1_data *data;
  95. data = atxp1_update_device(dev);
  96. size = sprintf(buf, "%d\n", vid_from_reg(data->reg.vid & ATXP1_VIDMASK, data->vrm));
  97. return size;
  98. }
  99. static ssize_t atxp1_storevcore(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  100. {
  101. struct atxp1_data *data;
  102. struct i2c_client *client;
  103. int vid, cvid;
  104. unsigned int vcore;
  105. client = to_i2c_client(dev);
  106. data = atxp1_update_device(dev);
  107. vcore = simple_strtoul(buf, NULL, 10);
  108. vcore /= 25;
  109. vcore *= 25;
  110. /* Calculate VID */
  111. vid = vid_to_reg(vcore, data->vrm);
  112. if (vid < 0) {
  113. dev_err(dev, "VID calculation failed.\n");
  114. return -1;
  115. }
  116. /* If output enabled, use control register value. Otherwise original CPU VID */
  117. if (data->reg.vid & ATXP1_VIDENA)
  118. cvid = data->reg.vid & ATXP1_VIDMASK;
  119. else
  120. cvid = data->reg.cpu_vid;
  121. /* Nothing changed, aborting */
  122. if (vid == cvid)
  123. return count;
  124. dev_dbg(dev, "Setting VCore to %d mV (0x%02x)\n", vcore, vid);
  125. /* Write every 25 mV step to increase stability */
  126. if (cvid > vid) {
  127. for (; cvid >= vid; cvid--) {
  128. i2c_smbus_write_byte_data(client, ATXP1_VID, cvid | ATXP1_VIDENA);
  129. }
  130. }
  131. else {
  132. for (; cvid <= vid; cvid++) {
  133. i2c_smbus_write_byte_data(client, ATXP1_VID, cvid | ATXP1_VIDENA);
  134. }
  135. }
  136. data->valid = 0;
  137. return count;
  138. }
  139. /* CPU core reference voltage
  140. unit: millivolt
  141. */
  142. static DEVICE_ATTR(cpu0_vid, S_IRUGO | S_IWUSR, atxp1_showvcore, atxp1_storevcore);
  143. /* sys file functions for GPIO1 */
  144. static ssize_t atxp1_showgpio1(struct device *dev, struct device_attribute *attr, char *buf)
  145. {
  146. int size;
  147. struct atxp1_data *data;
  148. data = atxp1_update_device(dev);
  149. size = sprintf(buf, "0x%02x\n", data->reg.gpio1 & ATXP1_GPIO1MASK);
  150. return size;
  151. }
  152. static ssize_t atxp1_storegpio1(struct device *dev, struct device_attribute *attr, const char*buf, size_t count)
  153. {
  154. struct atxp1_data *data;
  155. struct i2c_client *client;
  156. unsigned int value;
  157. client = to_i2c_client(dev);
  158. data = atxp1_update_device(dev);
  159. value = simple_strtoul(buf, NULL, 16);
  160. value &= ATXP1_GPIO1MASK;
  161. if (value != (data->reg.gpio1 & ATXP1_GPIO1MASK)) {
  162. dev_info(dev, "Writing 0x%x to GPIO1.\n", value);
  163. i2c_smbus_write_byte_data(client, ATXP1_GPIO1, value);
  164. data->valid = 0;
  165. }
  166. return count;
  167. }
  168. /* GPIO1 data register
  169. unit: Four bit as hex (e.g. 0x0f)
  170. */
  171. static DEVICE_ATTR(gpio1, S_IRUGO | S_IWUSR, atxp1_showgpio1, atxp1_storegpio1);
  172. /* sys file functions for GPIO2 */
  173. static ssize_t atxp1_showgpio2(struct device *dev, struct device_attribute *attr, char *buf)
  174. {
  175. int size;
  176. struct atxp1_data *data;
  177. data = atxp1_update_device(dev);
  178. size = sprintf(buf, "0x%02x\n", data->reg.gpio2);
  179. return size;
  180. }
  181. static ssize_t atxp1_storegpio2(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  182. {
  183. struct atxp1_data *data;
  184. struct i2c_client *client;
  185. unsigned int value;
  186. client = to_i2c_client(dev);
  187. data = atxp1_update_device(dev);
  188. value = simple_strtoul(buf, NULL, 16) & 0xff;
  189. if (value != data->reg.gpio2) {
  190. dev_info(dev, "Writing 0x%x to GPIO1.\n", value);
  191. i2c_smbus_write_byte_data(client, ATXP1_GPIO2, value);
  192. data->valid = 0;
  193. }
  194. return count;
  195. }
  196. /* GPIO2 data register
  197. unit: Eight bit as hex (e.g. 0xff)
  198. */
  199. static DEVICE_ATTR(gpio2, S_IRUGO | S_IWUSR, atxp1_showgpio2, atxp1_storegpio2);
  200. static struct attribute *atxp1_attributes[] = {
  201. &dev_attr_gpio1.attr,
  202. &dev_attr_gpio2.attr,
  203. &dev_attr_cpu0_vid.attr,
  204. NULL
  205. };
  206. static const struct attribute_group atxp1_group = {
  207. .attrs = atxp1_attributes,
  208. };
  209. /* Return 0 if detection is successful, -ENODEV otherwise */
  210. static int atxp1_detect(struct i2c_client *new_client,
  211. struct i2c_board_info *info)
  212. {
  213. struct i2c_adapter *adapter = new_client->adapter;
  214. u8 temp;
  215. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  216. return -ENODEV;
  217. /* Detect ATXP1, checking if vendor ID registers are all zero */
  218. if (!((i2c_smbus_read_byte_data(new_client, 0x3e) == 0) &&
  219. (i2c_smbus_read_byte_data(new_client, 0x3f) == 0) &&
  220. (i2c_smbus_read_byte_data(new_client, 0xfe) == 0) &&
  221. (i2c_smbus_read_byte_data(new_client, 0xff) == 0)))
  222. return -ENODEV;
  223. /* No vendor ID, now checking if registers 0x10,0x11 (non-existent)
  224. * showing the same as register 0x00 */
  225. temp = i2c_smbus_read_byte_data(new_client, 0x00);
  226. if (!((i2c_smbus_read_byte_data(new_client, 0x10) == temp) &&
  227. (i2c_smbus_read_byte_data(new_client, 0x11) == temp)))
  228. return -ENODEV;
  229. /* Get VRM */
  230. temp = vid_which_vrm();
  231. if ((temp != 90) && (temp != 91)) {
  232. dev_err(&adapter->dev, "atxp1: Not supporting VRM %d.%d\n",
  233. temp / 10, temp % 10);
  234. return -ENODEV;
  235. }
  236. strlcpy(info->type, "atxp1", I2C_NAME_SIZE);
  237. return 0;
  238. }
  239. static int atxp1_probe(struct i2c_client *new_client,
  240. const struct i2c_device_id *id)
  241. {
  242. struct atxp1_data *data;
  243. int err;
  244. data = kzalloc(sizeof(struct atxp1_data), GFP_KERNEL);
  245. if (!data) {
  246. err = -ENOMEM;
  247. goto exit;
  248. }
  249. /* Get VRM */
  250. data->vrm = vid_which_vrm();
  251. i2c_set_clientdata(new_client, data);
  252. data->valid = 0;
  253. mutex_init(&data->update_lock);
  254. /* Register sysfs hooks */
  255. if ((err = sysfs_create_group(&new_client->dev.kobj, &atxp1_group)))
  256. goto exit_free;
  257. data->hwmon_dev = hwmon_device_register(&new_client->dev);
  258. if (IS_ERR(data->hwmon_dev)) {
  259. err = PTR_ERR(data->hwmon_dev);
  260. goto exit_remove_files;
  261. }
  262. dev_info(&new_client->dev, "Using VRM: %d.%d\n",
  263. data->vrm / 10, data->vrm % 10);
  264. return 0;
  265. exit_remove_files:
  266. sysfs_remove_group(&new_client->dev.kobj, &atxp1_group);
  267. exit_free:
  268. kfree(data);
  269. exit:
  270. return err;
  271. };
  272. static int atxp1_remove(struct i2c_client *client)
  273. {
  274. struct atxp1_data * data = i2c_get_clientdata(client);
  275. hwmon_device_unregister(data->hwmon_dev);
  276. sysfs_remove_group(&client->dev.kobj, &atxp1_group);
  277. kfree(data);
  278. return 0;
  279. };
  280. static int __init atxp1_init(void)
  281. {
  282. return i2c_add_driver(&atxp1_driver);
  283. };
  284. static void __exit atxp1_exit(void)
  285. {
  286. i2c_del_driver(&atxp1_driver);
  287. };
  288. module_init(atxp1_init);
  289. module_exit(atxp1_exit);