atxp1.c 9.2 KB

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