atxp1.c 8.8 KB

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