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