gsc.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * A hwmon driver for the Gateworks System Controller
  3. * Copyright (C) 2009 Gateworks Corporation
  4. *
  5. * Author: Chris Lang <clang@gateworks.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License,
  9. * as published by the Free Software Foundation - version 2.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/i2c.h>
  13. #include <linux/hwmon.h>
  14. #include <linux/hwmon-sysfs.h>
  15. #include <linux/err.h>
  16. #include <linux/slab.h>
  17. #define DRV_VERSION "0.2"
  18. enum chips { gsp };
  19. /* AD7418 registers */
  20. #define GSP_REG_TEMP_IN 0x00
  21. #define GSP_REG_VIN 0x02
  22. #define GSP_REG_3P3 0x05
  23. #define GSP_REG_BAT 0x08
  24. #define GSP_REG_5P0 0x0b
  25. #define GSP_REG_CORE 0x0e
  26. #define GSP_REG_CPU1 0x11
  27. #define GSP_REG_CPU2 0x14
  28. #define GSP_REG_DRAM 0x17
  29. #define GSP_REG_EXT_BAT 0x1a
  30. #define GSP_REG_IO1 0x1d
  31. #define GSP_REG_IO2 0x20
  32. #define GSP_REG_PCIE 0x23
  33. #define GSP_REG_CURRENT 0x26
  34. #define GSP_FAN_0 0x2C
  35. #define GSP_FAN_1 0x2E
  36. #define GSP_FAN_2 0x30
  37. #define GSP_FAN_3 0x32
  38. #define GSP_FAN_4 0x34
  39. #define GSP_FAN_5 0x36
  40. struct gsp_sensor_info {
  41. const char* name;
  42. int reg;
  43. };
  44. static const struct gsp_sensor_info gsp_sensors[] = {
  45. {"temp", GSP_REG_TEMP_IN},
  46. {"vin", GSP_REG_VIN},
  47. {"3p3", GSP_REG_3P3},
  48. {"bat", GSP_REG_BAT},
  49. {"5p0", GSP_REG_5P0},
  50. {"core", GSP_REG_CORE},
  51. {"cpu1", GSP_REG_CPU1},
  52. {"cpu2", GSP_REG_CPU2},
  53. {"dram", GSP_REG_DRAM},
  54. {"ext_bat", GSP_REG_EXT_BAT},
  55. {"io1", GSP_REG_IO1},
  56. {"io2", GSP_REG_IO2},
  57. {"pci2", GSP_REG_PCIE},
  58. {"current", GSP_REG_CURRENT},
  59. {"fan_point0", GSP_FAN_0},
  60. {"fan_point1", GSP_FAN_1},
  61. {"fan_point2", GSP_FAN_2},
  62. {"fan_point3", GSP_FAN_3},
  63. {"fan_point4", GSP_FAN_4},
  64. {"fan_point5", GSP_FAN_5},
  65. };
  66. struct gsp_data {
  67. struct device *hwmon_dev;
  68. struct attribute_group attrs;
  69. enum chips type;
  70. };
  71. static int gsp_probe(struct i2c_client *client,
  72. const struct i2c_device_id *id);
  73. static int gsp_remove(struct i2c_client *client);
  74. static const struct i2c_device_id gsp_id[] = {
  75. { "gsp", 0 },
  76. { }
  77. };
  78. MODULE_DEVICE_TABLE(i2c, gsp_id);
  79. static struct i2c_driver gsp_driver = {
  80. .driver = {
  81. .name = "gsp",
  82. },
  83. .probe = gsp_probe,
  84. .remove = gsp_remove,
  85. .id_table = gsp_id,
  86. };
  87. /* All registers are word-sized, except for the configuration registers.
  88. * AD7418 uses a high-byte first convention. Do NOT use those functions to
  89. * access the configuration registers CONF and CONF2, as they are byte-sized.
  90. */
  91. static inline int gsp_read(struct i2c_client *client, u8 reg)
  92. {
  93. unsigned int adc = 0;
  94. if (reg == GSP_REG_TEMP_IN || reg > GSP_REG_CURRENT)
  95. {
  96. adc |= i2c_smbus_read_byte_data(client, reg);
  97. adc |= i2c_smbus_read_byte_data(client, reg + 1) << 8;
  98. return adc;
  99. }
  100. else
  101. {
  102. adc |= i2c_smbus_read_byte_data(client, reg);
  103. adc |= i2c_smbus_read_byte_data(client, reg + 1) << 8;
  104. adc |= i2c_smbus_read_byte_data(client, reg + 2) << 16;
  105. return adc;
  106. }
  107. }
  108. static inline int gsp_write(struct i2c_client *client, u8 reg, u16 value)
  109. {
  110. i2c_smbus_write_byte_data(client, reg, value & 0xff);
  111. i2c_smbus_write_byte_data(client, reg + 1, ((value >> 8) & 0xff));
  112. return 1;
  113. }
  114. static ssize_t show_adc(struct device *dev, struct device_attribute *devattr,
  115. char *buf)
  116. {
  117. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  118. struct i2c_client *client = to_i2c_client(dev);
  119. return sprintf(buf, "%d\n", gsp_read(client, gsp_sensors[attr->index].reg));
  120. }
  121. static ssize_t show_label(struct device *dev,
  122. struct device_attribute *devattr, char *buf)
  123. {
  124. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  125. return sprintf(buf, "%s\n", gsp_sensors[attr->index].name);
  126. }
  127. static ssize_t store_fan(struct device *dev,
  128. struct device_attribute *devattr, const char *buf, size_t count)
  129. {
  130. u16 val;
  131. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  132. struct i2c_client *client = to_i2c_client(dev);
  133. val = simple_strtoul(buf, NULL, 10);
  134. gsp_write(client, gsp_sensors[attr->index].reg, val);
  135. return count;
  136. }
  137. static SENSOR_DEVICE_ATTR(temp0_input, S_IRUGO, show_adc, NULL, 0);
  138. static SENSOR_DEVICE_ATTR(temp0_label, S_IRUGO, show_label, NULL, 0);
  139. static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, show_adc, NULL, 1);
  140. static SENSOR_DEVICE_ATTR(in0_label, S_IRUGO, show_label, NULL, 1);
  141. static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_adc, NULL, 2);
  142. static SENSOR_DEVICE_ATTR(in1_label, S_IRUGO, show_label, NULL, 2);
  143. static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_adc, NULL, 3);
  144. static SENSOR_DEVICE_ATTR(in2_label, S_IRUGO, show_label, NULL, 3);
  145. static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_adc, NULL, 4);
  146. static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL, 4);
  147. static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, show_adc, NULL, 5);
  148. static SENSOR_DEVICE_ATTR(in4_label, S_IRUGO, show_label, NULL, 5);
  149. static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, show_adc, NULL, 6);
  150. static SENSOR_DEVICE_ATTR(in5_label, S_IRUGO, show_label, NULL, 6);
  151. static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, show_adc, NULL, 7);
  152. static SENSOR_DEVICE_ATTR(in6_label, S_IRUGO, show_label, NULL, 7);
  153. static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, show_adc, NULL, 8);
  154. static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, show_label, NULL, 8);
  155. static SENSOR_DEVICE_ATTR(in8_input, S_IRUGO, show_adc, NULL, 9);
  156. static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, show_label, NULL, 9);
  157. static SENSOR_DEVICE_ATTR(in9_input, S_IRUGO, show_adc, NULL, 10);
  158. static SENSOR_DEVICE_ATTR(in9_label, S_IRUGO, show_label, NULL, 10);
  159. static SENSOR_DEVICE_ATTR(in10_input, S_IRUGO, show_adc, NULL, 11);
  160. static SENSOR_DEVICE_ATTR(in10_label, S_IRUGO, show_label, NULL, 11);
  161. static SENSOR_DEVICE_ATTR(in11_input, S_IRUGO, show_adc, NULL, 12);
  162. static SENSOR_DEVICE_ATTR(in11_label, S_IRUGO, show_label, NULL, 12);
  163. static SENSOR_DEVICE_ATTR(in12_input, S_IRUGO, show_adc, NULL, 13);
  164. static SENSOR_DEVICE_ATTR(in12_label, S_IRUGO, show_label, NULL, 13);
  165. static SENSOR_DEVICE_ATTR(fan0_point0, S_IRUGO | S_IWUSR, show_adc, store_fan, 14);
  166. static SENSOR_DEVICE_ATTR(fan0_point1, S_IRUGO | S_IWUSR, show_adc, store_fan, 15);
  167. static SENSOR_DEVICE_ATTR(fan0_point2, S_IRUGO | S_IWUSR, show_adc, store_fan, 16);
  168. static SENSOR_DEVICE_ATTR(fan0_point3, S_IRUGO | S_IWUSR, show_adc, store_fan, 17);
  169. static SENSOR_DEVICE_ATTR(fan0_point4, S_IRUGO | S_IWUSR, show_adc, store_fan, 18);
  170. static SENSOR_DEVICE_ATTR(fan0_point5, S_IRUGO | S_IWUSR, show_adc, store_fan, 19);
  171. static struct attribute *gsp_attributes[] = {
  172. &sensor_dev_attr_temp0_input.dev_attr.attr,
  173. &sensor_dev_attr_in0_input.dev_attr.attr,
  174. &sensor_dev_attr_in1_input.dev_attr.attr,
  175. &sensor_dev_attr_in2_input.dev_attr.attr,
  176. &sensor_dev_attr_in3_input.dev_attr.attr,
  177. &sensor_dev_attr_in4_input.dev_attr.attr,
  178. &sensor_dev_attr_in5_input.dev_attr.attr,
  179. &sensor_dev_attr_in6_input.dev_attr.attr,
  180. &sensor_dev_attr_in7_input.dev_attr.attr,
  181. &sensor_dev_attr_in8_input.dev_attr.attr,
  182. &sensor_dev_attr_in9_input.dev_attr.attr,
  183. &sensor_dev_attr_in10_input.dev_attr.attr,
  184. &sensor_dev_attr_in11_input.dev_attr.attr,
  185. &sensor_dev_attr_in12_input.dev_attr.attr,
  186. &sensor_dev_attr_temp0_label.dev_attr.attr,
  187. &sensor_dev_attr_in0_label.dev_attr.attr,
  188. &sensor_dev_attr_in1_label.dev_attr.attr,
  189. &sensor_dev_attr_in2_label.dev_attr.attr,
  190. &sensor_dev_attr_in3_label.dev_attr.attr,
  191. &sensor_dev_attr_in4_label.dev_attr.attr,
  192. &sensor_dev_attr_in5_label.dev_attr.attr,
  193. &sensor_dev_attr_in6_label.dev_attr.attr,
  194. &sensor_dev_attr_in7_label.dev_attr.attr,
  195. &sensor_dev_attr_in8_label.dev_attr.attr,
  196. &sensor_dev_attr_in9_label.dev_attr.attr,
  197. &sensor_dev_attr_in10_label.dev_attr.attr,
  198. &sensor_dev_attr_in11_label.dev_attr.attr,
  199. &sensor_dev_attr_in12_label.dev_attr.attr,
  200. &sensor_dev_attr_fan0_point0.dev_attr.attr,
  201. &sensor_dev_attr_fan0_point1.dev_attr.attr,
  202. &sensor_dev_attr_fan0_point2.dev_attr.attr,
  203. &sensor_dev_attr_fan0_point3.dev_attr.attr,
  204. &sensor_dev_attr_fan0_point4.dev_attr.attr,
  205. &sensor_dev_attr_fan0_point5.dev_attr.attr,
  206. NULL
  207. };
  208. static int gsp_probe(struct i2c_client *client,
  209. const struct i2c_device_id *id)
  210. {
  211. struct i2c_adapter *adapter = client->adapter;
  212. struct gsp_data *data;
  213. int err;
  214. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
  215. I2C_FUNC_SMBUS_WORD_DATA)) {
  216. err = -EOPNOTSUPP;
  217. goto exit;
  218. }
  219. if (!(data = kzalloc(sizeof(struct gsp_data), GFP_KERNEL))) {
  220. err = -ENOMEM;
  221. goto exit;
  222. }
  223. i2c_set_clientdata(client, data);
  224. data->type = id->driver_data;
  225. switch (data->type) {
  226. case 0:
  227. data->attrs.attrs = gsp_attributes;
  228. break;
  229. }
  230. dev_info(&client->dev, "%s chip found\n", client->name);
  231. /* Register sysfs hooks */
  232. if ((err = sysfs_create_group(&client->dev.kobj, &data->attrs)))
  233. goto exit_free;
  234. data->hwmon_dev = hwmon_device_register(&client->dev);
  235. if (IS_ERR(data->hwmon_dev)) {
  236. err = PTR_ERR(data->hwmon_dev);
  237. goto exit_remove;
  238. }
  239. return 0;
  240. exit_remove:
  241. sysfs_remove_group(&client->dev.kobj, &data->attrs);
  242. exit_free:
  243. kfree(data);
  244. exit:
  245. return err;
  246. }
  247. static int gsp_remove(struct i2c_client *client)
  248. {
  249. struct gsp_data *data = i2c_get_clientdata(client);
  250. hwmon_device_unregister(data->hwmon_dev);
  251. sysfs_remove_group(&client->dev.kobj, &data->attrs);
  252. kfree(data);
  253. return 0;
  254. }
  255. static int __init gsp_init(void)
  256. {
  257. return i2c_add_driver(&gsp_driver);
  258. }
  259. static void __exit gsp_exit(void)
  260. {
  261. i2c_del_driver(&gsp_driver);
  262. }
  263. module_init(gsp_init);
  264. module_exit(gsp_exit);
  265. MODULE_AUTHOR("Chris Lang <clang@gateworks.com>");
  266. MODULE_DESCRIPTION("GSC HWMON driver");
  267. MODULE_LICENSE("GPL");
  268. MODULE_VERSION(DRV_VERSION);