atxp1.c 9.0 KB

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