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