gl518sm.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. /*
  2. * gl518sm.c - Part of lm_sensors, Linux kernel modules for hardware
  3. * monitoring
  4. * Copyright (C) 1998, 1999 Frodo Looijaard <frodol@dds.nl> and
  5. * Kyosti Malkki <kmalkki@cc.hut.fi>
  6. * Copyright (C) 2004 Hong-Gunn Chew <hglinux@gunnet.org> and
  7. * Jean Delvare <khali@linux-fr.org>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. *
  23. * Ported to Linux 2.6 by Hong-Gunn Chew with the help of Jean Delvare
  24. * and advice of Greg Kroah-Hartman.
  25. *
  26. * Notes about the port:
  27. * Release 0x00 of the GL518SM chipset doesn't support reading of in0,
  28. * in1 nor in2. The original driver had an ugly workaround to get them
  29. * anyway (changing limits and watching alarms trigger and wear off).
  30. * We did not keep that part of the original driver in the Linux 2.6
  31. * version, since it was making the driver significantly more complex
  32. * with no real benefit.
  33. */
  34. #include <linux/module.h>
  35. #include <linux/init.h>
  36. #include <linux/slab.h>
  37. #include <linux/jiffies.h>
  38. #include <linux/i2c.h>
  39. #include <linux/hwmon.h>
  40. #include <linux/hwmon-sysfs.h>
  41. #include <linux/err.h>
  42. #include <linux/mutex.h>
  43. #include <linux/sysfs.h>
  44. /* Addresses to scan */
  45. static const unsigned short normal_i2c[] = { 0x2c, 0x2d, I2C_CLIENT_END };
  46. enum chips { gl518sm_r00, gl518sm_r80 };
  47. /* Many GL518 constants specified below */
  48. /* The GL518 registers */
  49. #define GL518_REG_CHIP_ID 0x00
  50. #define GL518_REG_REVISION 0x01
  51. #define GL518_REG_VENDOR_ID 0x02
  52. #define GL518_REG_CONF 0x03
  53. #define GL518_REG_TEMP_IN 0x04
  54. #define GL518_REG_TEMP_MAX 0x05
  55. #define GL518_REG_TEMP_HYST 0x06
  56. #define GL518_REG_FAN_COUNT 0x07
  57. #define GL518_REG_FAN_LIMIT 0x08
  58. #define GL518_REG_VIN1_LIMIT 0x09
  59. #define GL518_REG_VIN2_LIMIT 0x0a
  60. #define GL518_REG_VIN3_LIMIT 0x0b
  61. #define GL518_REG_VDD_LIMIT 0x0c
  62. #define GL518_REG_VIN3 0x0d
  63. #define GL518_REG_MISC 0x0f
  64. #define GL518_REG_ALARM 0x10
  65. #define GL518_REG_MASK 0x11
  66. #define GL518_REG_INT 0x12
  67. #define GL518_REG_VIN2 0x13
  68. #define GL518_REG_VIN1 0x14
  69. #define GL518_REG_VDD 0x15
  70. /*
  71. * Conversions. Rounding and limit checking is only done on the TO_REG
  72. * variants. Note that you should be a bit careful with which arguments
  73. * these macros are called: arguments may be evaluated more than once.
  74. * Fixing this is just not worth it.
  75. */
  76. #define RAW_FROM_REG(val) val
  77. #define BOOL_FROM_REG(val) ((val)?0:1)
  78. #define BOOL_TO_REG(val) ((val)?0:1)
  79. #define TEMP_TO_REG(val) (SENSORS_LIMIT(((((val)<0? \
  80. (val)-500:(val)+500)/1000)+119),0,255))
  81. #define TEMP_FROM_REG(val) (((val) - 119) * 1000)
  82. static inline u8 FAN_TO_REG(long rpm, int div)
  83. {
  84. long rpmdiv;
  85. if (rpm == 0)
  86. return 0;
  87. rpmdiv = SENSORS_LIMIT(rpm, 1, 960000) * div;
  88. return SENSORS_LIMIT((480000 + rpmdiv / 2) / rpmdiv, 1, 255);
  89. }
  90. #define FAN_FROM_REG(val,div) ((val)==0 ? 0 : (480000/((val)*(div))))
  91. #define IN_TO_REG(val) (SENSORS_LIMIT((((val)+9)/19),0,255))
  92. #define IN_FROM_REG(val) ((val)*19)
  93. #define VDD_TO_REG(val) (SENSORS_LIMIT((((val)*4+47)/95),0,255))
  94. #define VDD_FROM_REG(val) (((val)*95+2)/4)
  95. #define DIV_FROM_REG(val) (1 << (val))
  96. #define BEEP_MASK_TO_REG(val) ((val) & 0x7f & data->alarm_mask)
  97. #define BEEP_MASK_FROM_REG(val) ((val) & 0x7f)
  98. /* Each client has this additional data */
  99. struct gl518_data {
  100. struct device *hwmon_dev;
  101. enum chips type;
  102. struct mutex update_lock;
  103. char valid; /* !=0 if following fields are valid */
  104. unsigned long last_updated; /* In jiffies */
  105. u8 voltage_in[4]; /* Register values; [0] = VDD */
  106. u8 voltage_min[4]; /* Register values; [0] = VDD */
  107. u8 voltage_max[4]; /* Register values; [0] = VDD */
  108. u8 fan_in[2];
  109. u8 fan_min[2];
  110. u8 fan_div[2]; /* Register encoding, shifted right */
  111. u8 fan_auto1; /* Boolean */
  112. u8 temp_in; /* Register values */
  113. u8 temp_max; /* Register values */
  114. u8 temp_hyst; /* Register values */
  115. u8 alarms; /* Register value */
  116. u8 alarm_mask;
  117. u8 beep_mask; /* Register value */
  118. u8 beep_enable; /* Boolean */
  119. };
  120. static int gl518_probe(struct i2c_client *client,
  121. const struct i2c_device_id *id);
  122. static int gl518_detect(struct i2c_client *client, struct i2c_board_info *info);
  123. static void gl518_init_client(struct i2c_client *client);
  124. static int gl518_remove(struct i2c_client *client);
  125. static int gl518_read_value(struct i2c_client *client, u8 reg);
  126. static int gl518_write_value(struct i2c_client *client, u8 reg, u16 value);
  127. static struct gl518_data *gl518_update_device(struct device *dev);
  128. static const struct i2c_device_id gl518_id[] = {
  129. { "gl518sm", 0 },
  130. { }
  131. };
  132. MODULE_DEVICE_TABLE(i2c, gl518_id);
  133. /* This is the driver that will be inserted */
  134. static struct i2c_driver gl518_driver = {
  135. .class = I2C_CLASS_HWMON,
  136. .driver = {
  137. .name = "gl518sm",
  138. },
  139. .probe = gl518_probe,
  140. .remove = gl518_remove,
  141. .id_table = gl518_id,
  142. .detect = gl518_detect,
  143. .address_list = normal_i2c,
  144. };
  145. /*
  146. * Sysfs stuff
  147. */
  148. #define show(type, suffix, value) \
  149. static ssize_t show_##suffix(struct device *dev, struct device_attribute *attr, char *buf) \
  150. { \
  151. struct gl518_data *data = gl518_update_device(dev); \
  152. return sprintf(buf, "%d\n", type##_FROM_REG(data->value)); \
  153. }
  154. show(TEMP, temp_input1, temp_in);
  155. show(TEMP, temp_max1, temp_max);
  156. show(TEMP, temp_hyst1, temp_hyst);
  157. show(BOOL, fan_auto1, fan_auto1);
  158. show(VDD, in_input0, voltage_in[0]);
  159. show(IN, in_input1, voltage_in[1]);
  160. show(IN, in_input2, voltage_in[2]);
  161. show(IN, in_input3, voltage_in[3]);
  162. show(VDD, in_min0, voltage_min[0]);
  163. show(IN, in_min1, voltage_min[1]);
  164. show(IN, in_min2, voltage_min[2]);
  165. show(IN, in_min3, voltage_min[3]);
  166. show(VDD, in_max0, voltage_max[0]);
  167. show(IN, in_max1, voltage_max[1]);
  168. show(IN, in_max2, voltage_max[2]);
  169. show(IN, in_max3, voltage_max[3]);
  170. show(RAW, alarms, alarms);
  171. show(BOOL, beep_enable, beep_enable);
  172. show(BEEP_MASK, beep_mask, beep_mask);
  173. static ssize_t show_fan_input(struct device *dev,
  174. struct device_attribute *attr, char *buf)
  175. {
  176. int nr = to_sensor_dev_attr(attr)->index;
  177. struct gl518_data *data = gl518_update_device(dev);
  178. return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_in[nr],
  179. DIV_FROM_REG(data->fan_div[nr])));
  180. }
  181. static ssize_t show_fan_min(struct device *dev,
  182. struct device_attribute *attr, char *buf)
  183. {
  184. int nr = to_sensor_dev_attr(attr)->index;
  185. struct gl518_data *data = gl518_update_device(dev);
  186. return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
  187. DIV_FROM_REG(data->fan_div[nr])));
  188. }
  189. static ssize_t show_fan_div(struct device *dev,
  190. struct device_attribute *attr, char *buf)
  191. {
  192. int nr = to_sensor_dev_attr(attr)->index;
  193. struct gl518_data *data = gl518_update_device(dev);
  194. return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
  195. }
  196. #define set(type, suffix, value, reg) \
  197. static ssize_t set_##suffix(struct device *dev, struct device_attribute *attr, const char *buf, \
  198. size_t count) \
  199. { \
  200. struct i2c_client *client = to_i2c_client(dev); \
  201. struct gl518_data *data = i2c_get_clientdata(client); \
  202. long val = simple_strtol(buf, NULL, 10); \
  203. \
  204. mutex_lock(&data->update_lock); \
  205. data->value = type##_TO_REG(val); \
  206. gl518_write_value(client, reg, data->value); \
  207. mutex_unlock(&data->update_lock); \
  208. return count; \
  209. }
  210. #define set_bits(type, suffix, value, reg, mask, shift) \
  211. static ssize_t set_##suffix(struct device *dev, struct device_attribute *attr, const char *buf, \
  212. size_t count) \
  213. { \
  214. struct i2c_client *client = to_i2c_client(dev); \
  215. struct gl518_data *data = i2c_get_clientdata(client); \
  216. int regvalue; \
  217. unsigned long val = simple_strtoul(buf, NULL, 10); \
  218. \
  219. mutex_lock(&data->update_lock); \
  220. regvalue = gl518_read_value(client, reg); \
  221. data->value = type##_TO_REG(val); \
  222. regvalue = (regvalue & ~mask) | (data->value << shift); \
  223. gl518_write_value(client, reg, regvalue); \
  224. mutex_unlock(&data->update_lock); \
  225. return count; \
  226. }
  227. #define set_low(type, suffix, value, reg) \
  228. set_bits(type, suffix, value, reg, 0x00ff, 0)
  229. #define set_high(type, suffix, value, reg) \
  230. set_bits(type, suffix, value, reg, 0xff00, 8)
  231. set(TEMP, temp_max1, temp_max, GL518_REG_TEMP_MAX);
  232. set(TEMP, temp_hyst1, temp_hyst, GL518_REG_TEMP_HYST);
  233. set_bits(BOOL, fan_auto1, fan_auto1, GL518_REG_MISC, 0x08, 3);
  234. set_low(VDD, in_min0, voltage_min[0], GL518_REG_VDD_LIMIT);
  235. set_low(IN, in_min1, voltage_min[1], GL518_REG_VIN1_LIMIT);
  236. set_low(IN, in_min2, voltage_min[2], GL518_REG_VIN2_LIMIT);
  237. set_low(IN, in_min3, voltage_min[3], GL518_REG_VIN3_LIMIT);
  238. set_high(VDD, in_max0, voltage_max[0], GL518_REG_VDD_LIMIT);
  239. set_high(IN, in_max1, voltage_max[1], GL518_REG_VIN1_LIMIT);
  240. set_high(IN, in_max2, voltage_max[2], GL518_REG_VIN2_LIMIT);
  241. set_high(IN, in_max3, voltage_max[3], GL518_REG_VIN3_LIMIT);
  242. set_bits(BOOL, beep_enable, beep_enable, GL518_REG_CONF, 0x04, 2);
  243. set(BEEP_MASK, beep_mask, beep_mask, GL518_REG_ALARM);
  244. static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
  245. const char *buf, size_t count)
  246. {
  247. struct i2c_client *client = to_i2c_client(dev);
  248. struct gl518_data *data = i2c_get_clientdata(client);
  249. int nr = to_sensor_dev_attr(attr)->index;
  250. int regvalue;
  251. unsigned long val = simple_strtoul(buf, NULL, 10);
  252. mutex_lock(&data->update_lock);
  253. regvalue = gl518_read_value(client, GL518_REG_FAN_LIMIT);
  254. data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
  255. regvalue = (regvalue & (0xff << (8 * nr)))
  256. | (data->fan_min[nr] << (8 * (1 - nr)));
  257. gl518_write_value(client, GL518_REG_FAN_LIMIT, regvalue);
  258. data->beep_mask = gl518_read_value(client, GL518_REG_ALARM);
  259. if (data->fan_min[nr] == 0)
  260. data->alarm_mask &= ~(0x20 << nr);
  261. else
  262. data->alarm_mask |= (0x20 << nr);
  263. data->beep_mask &= data->alarm_mask;
  264. gl518_write_value(client, GL518_REG_ALARM, data->beep_mask);
  265. mutex_unlock(&data->update_lock);
  266. return count;
  267. }
  268. static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
  269. const char *buf, size_t count)
  270. {
  271. struct i2c_client *client = to_i2c_client(dev);
  272. struct gl518_data *data = i2c_get_clientdata(client);
  273. int nr = to_sensor_dev_attr(attr)->index;
  274. int regvalue;
  275. unsigned long val = simple_strtoul(buf, NULL, 10);
  276. switch (val) {
  277. case 1: val = 0; break;
  278. case 2: val = 1; break;
  279. case 4: val = 2; break;
  280. case 8: val = 3; break;
  281. default:
  282. dev_err(dev, "Invalid fan clock divider %lu, choose one "
  283. "of 1, 2, 4 or 8\n", val);
  284. return -EINVAL;
  285. }
  286. mutex_lock(&data->update_lock);
  287. regvalue = gl518_read_value(client, GL518_REG_MISC);
  288. data->fan_div[nr] = val;
  289. regvalue = (regvalue & ~(0xc0 >> (2 * nr)))
  290. | (data->fan_div[nr] << (6 - 2 * nr));
  291. gl518_write_value(client, GL518_REG_MISC, regvalue);
  292. mutex_unlock(&data->update_lock);
  293. return count;
  294. }
  295. static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input1, NULL);
  296. static DEVICE_ATTR(temp1_max, S_IWUSR|S_IRUGO, show_temp_max1, set_temp_max1);
  297. static DEVICE_ATTR(temp1_max_hyst, S_IWUSR|S_IRUGO,
  298. show_temp_hyst1, set_temp_hyst1);
  299. static DEVICE_ATTR(fan1_auto, S_IWUSR|S_IRUGO, show_fan_auto1, set_fan_auto1);
  300. static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan_input, NULL, 0);
  301. static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan_input, NULL, 1);
  302. static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR|S_IRUGO,
  303. show_fan_min, set_fan_min, 0);
  304. static SENSOR_DEVICE_ATTR(fan2_min, S_IWUSR|S_IRUGO,
  305. show_fan_min, set_fan_min, 1);
  306. static SENSOR_DEVICE_ATTR(fan1_div, S_IWUSR|S_IRUGO,
  307. show_fan_div, set_fan_div, 0);
  308. static SENSOR_DEVICE_ATTR(fan2_div, S_IWUSR|S_IRUGO,
  309. show_fan_div, set_fan_div, 1);
  310. static DEVICE_ATTR(in0_input, S_IRUGO, show_in_input0, NULL);
  311. static DEVICE_ATTR(in1_input, S_IRUGO, show_in_input1, NULL);
  312. static DEVICE_ATTR(in2_input, S_IRUGO, show_in_input2, NULL);
  313. static DEVICE_ATTR(in3_input, S_IRUGO, show_in_input3, NULL);
  314. static DEVICE_ATTR(in0_min, S_IWUSR|S_IRUGO, show_in_min0, set_in_min0);
  315. static DEVICE_ATTR(in1_min, S_IWUSR|S_IRUGO, show_in_min1, set_in_min1);
  316. static DEVICE_ATTR(in2_min, S_IWUSR|S_IRUGO, show_in_min2, set_in_min2);
  317. static DEVICE_ATTR(in3_min, S_IWUSR|S_IRUGO, show_in_min3, set_in_min3);
  318. static DEVICE_ATTR(in0_max, S_IWUSR|S_IRUGO, show_in_max0, set_in_max0);
  319. static DEVICE_ATTR(in1_max, S_IWUSR|S_IRUGO, show_in_max1, set_in_max1);
  320. static DEVICE_ATTR(in2_max, S_IWUSR|S_IRUGO, show_in_max2, set_in_max2);
  321. static DEVICE_ATTR(in3_max, S_IWUSR|S_IRUGO, show_in_max3, set_in_max3);
  322. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  323. static DEVICE_ATTR(beep_enable, S_IWUSR|S_IRUGO,
  324. show_beep_enable, set_beep_enable);
  325. static DEVICE_ATTR(beep_mask, S_IWUSR|S_IRUGO,
  326. show_beep_mask, set_beep_mask);
  327. static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
  328. char *buf)
  329. {
  330. int bitnr = to_sensor_dev_attr(attr)->index;
  331. struct gl518_data *data = gl518_update_device(dev);
  332. return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
  333. }
  334. static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
  335. static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
  336. static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
  337. static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
  338. static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
  339. static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 5);
  340. static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 6);
  341. static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
  342. char *buf)
  343. {
  344. int bitnr = to_sensor_dev_attr(attr)->index;
  345. struct gl518_data *data = gl518_update_device(dev);
  346. return sprintf(buf, "%u\n", (data->beep_mask >> bitnr) & 1);
  347. }
  348. static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
  349. const char *buf, size_t count)
  350. {
  351. struct i2c_client *client = to_i2c_client(dev);
  352. struct gl518_data *data = i2c_get_clientdata(client);
  353. int bitnr = to_sensor_dev_attr(attr)->index;
  354. unsigned long bit;
  355. bit = simple_strtoul(buf, NULL, 10);
  356. if (bit & ~1)
  357. return -EINVAL;
  358. mutex_lock(&data->update_lock);
  359. data->beep_mask = gl518_read_value(client, GL518_REG_ALARM);
  360. if (bit)
  361. data->beep_mask |= (1 << bitnr);
  362. else
  363. data->beep_mask &= ~(1 << bitnr);
  364. gl518_write_value(client, GL518_REG_ALARM, data->beep_mask);
  365. mutex_unlock(&data->update_lock);
  366. return count;
  367. }
  368. static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 0);
  369. static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 1);
  370. static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 2);
  371. static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 3);
  372. static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 4);
  373. static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 5);
  374. static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 6);
  375. static struct attribute *gl518_attributes[] = {
  376. &dev_attr_in3_input.attr,
  377. &dev_attr_in0_min.attr,
  378. &dev_attr_in1_min.attr,
  379. &dev_attr_in2_min.attr,
  380. &dev_attr_in3_min.attr,
  381. &dev_attr_in0_max.attr,
  382. &dev_attr_in1_max.attr,
  383. &dev_attr_in2_max.attr,
  384. &dev_attr_in3_max.attr,
  385. &sensor_dev_attr_in0_alarm.dev_attr.attr,
  386. &sensor_dev_attr_in1_alarm.dev_attr.attr,
  387. &sensor_dev_attr_in2_alarm.dev_attr.attr,
  388. &sensor_dev_attr_in3_alarm.dev_attr.attr,
  389. &sensor_dev_attr_in0_beep.dev_attr.attr,
  390. &sensor_dev_attr_in1_beep.dev_attr.attr,
  391. &sensor_dev_attr_in2_beep.dev_attr.attr,
  392. &sensor_dev_attr_in3_beep.dev_attr.attr,
  393. &dev_attr_fan1_auto.attr,
  394. &sensor_dev_attr_fan1_input.dev_attr.attr,
  395. &sensor_dev_attr_fan2_input.dev_attr.attr,
  396. &sensor_dev_attr_fan1_min.dev_attr.attr,
  397. &sensor_dev_attr_fan2_min.dev_attr.attr,
  398. &sensor_dev_attr_fan1_div.dev_attr.attr,
  399. &sensor_dev_attr_fan2_div.dev_attr.attr,
  400. &sensor_dev_attr_fan1_alarm.dev_attr.attr,
  401. &sensor_dev_attr_fan2_alarm.dev_attr.attr,
  402. &sensor_dev_attr_fan1_beep.dev_attr.attr,
  403. &sensor_dev_attr_fan2_beep.dev_attr.attr,
  404. &dev_attr_temp1_input.attr,
  405. &dev_attr_temp1_max.attr,
  406. &dev_attr_temp1_max_hyst.attr,
  407. &sensor_dev_attr_temp1_alarm.dev_attr.attr,
  408. &sensor_dev_attr_temp1_beep.dev_attr.attr,
  409. &dev_attr_alarms.attr,
  410. &dev_attr_beep_enable.attr,
  411. &dev_attr_beep_mask.attr,
  412. NULL
  413. };
  414. static const struct attribute_group gl518_group = {
  415. .attrs = gl518_attributes,
  416. };
  417. static struct attribute *gl518_attributes_r80[] = {
  418. &dev_attr_in0_input.attr,
  419. &dev_attr_in1_input.attr,
  420. &dev_attr_in2_input.attr,
  421. NULL
  422. };
  423. static const struct attribute_group gl518_group_r80 = {
  424. .attrs = gl518_attributes_r80,
  425. };
  426. /*
  427. * Real code
  428. */
  429. /* Return 0 if detection is successful, -ENODEV otherwise */
  430. static int gl518_detect(struct i2c_client *client, struct i2c_board_info *info)
  431. {
  432. struct i2c_adapter *adapter = client->adapter;
  433. int rev;
  434. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
  435. I2C_FUNC_SMBUS_WORD_DATA))
  436. return -ENODEV;
  437. /* Now, we do the remaining detection. */
  438. if ((gl518_read_value(client, GL518_REG_CHIP_ID) != 0x80)
  439. || (gl518_read_value(client, GL518_REG_CONF) & 0x80))
  440. return -ENODEV;
  441. /* Determine the chip type. */
  442. rev = gl518_read_value(client, GL518_REG_REVISION);
  443. if (rev != 0x00 && rev != 0x80)
  444. return -ENODEV;
  445. strlcpy(info->type, "gl518sm", I2C_NAME_SIZE);
  446. return 0;
  447. }
  448. static int gl518_probe(struct i2c_client *client,
  449. const struct i2c_device_id *id)
  450. {
  451. struct gl518_data *data;
  452. int err, revision;
  453. data = kzalloc(sizeof(struct gl518_data), GFP_KERNEL);
  454. if (!data) {
  455. err = -ENOMEM;
  456. goto exit;
  457. }
  458. i2c_set_clientdata(client, data);
  459. revision = gl518_read_value(client, GL518_REG_REVISION);
  460. data->type = revision == 0x80 ? gl518sm_r80 : gl518sm_r00;
  461. mutex_init(&data->update_lock);
  462. /* Initialize the GL518SM chip */
  463. data->alarm_mask = 0xff;
  464. gl518_init_client(client);
  465. /* Register sysfs hooks */
  466. if ((err = sysfs_create_group(&client->dev.kobj, &gl518_group)))
  467. goto exit_free;
  468. if (data->type == gl518sm_r80)
  469. if ((err = sysfs_create_group(&client->dev.kobj,
  470. &gl518_group_r80)))
  471. goto exit_remove_files;
  472. data->hwmon_dev = hwmon_device_register(&client->dev);
  473. if (IS_ERR(data->hwmon_dev)) {
  474. err = PTR_ERR(data->hwmon_dev);
  475. goto exit_remove_files;
  476. }
  477. return 0;
  478. exit_remove_files:
  479. sysfs_remove_group(&client->dev.kobj, &gl518_group);
  480. if (data->type == gl518sm_r80)
  481. sysfs_remove_group(&client->dev.kobj, &gl518_group_r80);
  482. exit_free:
  483. kfree(data);
  484. exit:
  485. return err;
  486. }
  487. /* Called when we have found a new GL518SM.
  488. Note that we preserve D4:NoFan2 and D2:beep_enable. */
  489. static void gl518_init_client(struct i2c_client *client)
  490. {
  491. /* Make sure we leave D7:Reset untouched */
  492. u8 regvalue = gl518_read_value(client, GL518_REG_CONF) & 0x7f;
  493. /* Comparator mode (D3=0), standby mode (D6=0) */
  494. gl518_write_value(client, GL518_REG_CONF, (regvalue &= 0x37));
  495. /* Never interrupts */
  496. gl518_write_value(client, GL518_REG_MASK, 0x00);
  497. /* Clear status register (D5=1), start (D6=1) */
  498. gl518_write_value(client, GL518_REG_CONF, 0x20 | regvalue);
  499. gl518_write_value(client, GL518_REG_CONF, 0x40 | regvalue);
  500. }
  501. static int gl518_remove(struct i2c_client *client)
  502. {
  503. struct gl518_data *data = i2c_get_clientdata(client);
  504. hwmon_device_unregister(data->hwmon_dev);
  505. sysfs_remove_group(&client->dev.kobj, &gl518_group);
  506. if (data->type == gl518sm_r80)
  507. sysfs_remove_group(&client->dev.kobj, &gl518_group_r80);
  508. kfree(data);
  509. return 0;
  510. }
  511. /* Registers 0x07 to 0x0c are word-sized, others are byte-sized
  512. GL518 uses a high-byte first convention, which is exactly opposite to
  513. the SMBus standard. */
  514. static int gl518_read_value(struct i2c_client *client, u8 reg)
  515. {
  516. if ((reg >= 0x07) && (reg <= 0x0c))
  517. return swab16(i2c_smbus_read_word_data(client, reg));
  518. else
  519. return i2c_smbus_read_byte_data(client, reg);
  520. }
  521. static int gl518_write_value(struct i2c_client *client, u8 reg, u16 value)
  522. {
  523. if ((reg >= 0x07) && (reg <= 0x0c))
  524. return i2c_smbus_write_word_data(client, reg, swab16(value));
  525. else
  526. return i2c_smbus_write_byte_data(client, reg, value);
  527. }
  528. static struct gl518_data *gl518_update_device(struct device *dev)
  529. {
  530. struct i2c_client *client = to_i2c_client(dev);
  531. struct gl518_data *data = i2c_get_clientdata(client);
  532. int val;
  533. mutex_lock(&data->update_lock);
  534. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  535. || !data->valid) {
  536. dev_dbg(&client->dev, "Starting gl518 update\n");
  537. data->alarms = gl518_read_value(client, GL518_REG_INT);
  538. data->beep_mask = gl518_read_value(client, GL518_REG_ALARM);
  539. val = gl518_read_value(client, GL518_REG_VDD_LIMIT);
  540. data->voltage_min[0] = val & 0xff;
  541. data->voltage_max[0] = (val >> 8) & 0xff;
  542. val = gl518_read_value(client, GL518_REG_VIN1_LIMIT);
  543. data->voltage_min[1] = val & 0xff;
  544. data->voltage_max[1] = (val >> 8) & 0xff;
  545. val = gl518_read_value(client, GL518_REG_VIN2_LIMIT);
  546. data->voltage_min[2] = val & 0xff;
  547. data->voltage_max[2] = (val >> 8) & 0xff;
  548. val = gl518_read_value(client, GL518_REG_VIN3_LIMIT);
  549. data->voltage_min[3] = val & 0xff;
  550. data->voltage_max[3] = (val >> 8) & 0xff;
  551. val = gl518_read_value(client, GL518_REG_FAN_COUNT);
  552. data->fan_in[0] = (val >> 8) & 0xff;
  553. data->fan_in[1] = val & 0xff;
  554. val = gl518_read_value(client, GL518_REG_FAN_LIMIT);
  555. data->fan_min[0] = (val >> 8) & 0xff;
  556. data->fan_min[1] = val & 0xff;
  557. data->temp_in = gl518_read_value(client, GL518_REG_TEMP_IN);
  558. data->temp_max =
  559. gl518_read_value(client, GL518_REG_TEMP_MAX);
  560. data->temp_hyst =
  561. gl518_read_value(client, GL518_REG_TEMP_HYST);
  562. val = gl518_read_value(client, GL518_REG_MISC);
  563. data->fan_div[0] = (val >> 6) & 0x03;
  564. data->fan_div[1] = (val >> 4) & 0x03;
  565. data->fan_auto1 = (val >> 3) & 0x01;
  566. data->alarms &= data->alarm_mask;
  567. val = gl518_read_value(client, GL518_REG_CONF);
  568. data->beep_enable = (val >> 2) & 1;
  569. if (data->type != gl518sm_r00) {
  570. data->voltage_in[0] =
  571. gl518_read_value(client, GL518_REG_VDD);
  572. data->voltage_in[1] =
  573. gl518_read_value(client, GL518_REG_VIN1);
  574. data->voltage_in[2] =
  575. gl518_read_value(client, GL518_REG_VIN2);
  576. }
  577. data->voltage_in[3] =
  578. gl518_read_value(client, GL518_REG_VIN3);
  579. data->last_updated = jiffies;
  580. data->valid = 1;
  581. }
  582. mutex_unlock(&data->update_lock);
  583. return data;
  584. }
  585. static int __init sensors_gl518sm_init(void)
  586. {
  587. return i2c_add_driver(&gl518_driver);
  588. }
  589. static void __exit sensors_gl518sm_exit(void)
  590. {
  591. i2c_del_driver(&gl518_driver);
  592. }
  593. MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
  594. "Kyosti Malkki <kmalkki@cc.hut.fi> and "
  595. "Hong-Gunn Chew <hglinux@gunnet.org>");
  596. MODULE_DESCRIPTION("GL518SM driver");
  597. MODULE_LICENSE("GPL");
  598. module_init(sensors_gl518sm_init);
  599. module_exit(sensors_gl518sm_exit);