gl518sm.c 23 KB

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