gl518sm.c 22 KB

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