atxp1.c 9.2 KB

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