atxp1.c 9.2 KB

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