gl518sm.c 23 KB

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