atxp1.c 8.8 KB

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