gl518sm.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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/err.h>
  41. #include <linux/mutex.h>
  42. #include <linux/sysfs.h>
  43. /* Addresses to scan */
  44. static unsigned short normal_i2c[] = { 0x2c, 0x2d, I2C_CLIENT_END };
  45. /* Insmod parameters */
  46. I2C_CLIENT_INSMOD_2(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) (SENSORS_LIMIT(((((val)<0? \
  80. (val)-500:(val)+500)/1000)+119),0,255))
  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 = SENSORS_LIMIT(rpm, 1, 1920000) * div;
  88. return SENSORS_LIMIT((960000 + rpmdiv / 2) / rpmdiv, 1, 255);
  89. }
  90. #define FAN_FROM_REG(val,div) ((val)==0 ? 0 : (960000/((val)*(div))))
  91. #define IN_TO_REG(val) (SENSORS_LIMIT((((val)+9)/19),0,255))
  92. #define IN_FROM_REG(val) ((val)*19)
  93. #define VDD_TO_REG(val) (SENSORS_LIMIT((((val)*4+47)/95),0,255))
  94. #define VDD_FROM_REG(val) (((val)*95+2)/4)
  95. #define DIV_TO_REG(val) ((val)==4?2:(val)==2?1:(val)==1?0:3)
  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. #define show_fan(suffix, value, index) \
  147. static ssize_t show_##suffix(struct device *dev, struct device_attribute *attr, char *buf) \
  148. { \
  149. struct gl518_data *data = gl518_update_device(dev); \
  150. return sprintf(buf, "%d\n", FAN_FROM_REG(data->value[index], \
  151. DIV_FROM_REG(data->fan_div[index]))); \
  152. }
  153. show(TEMP, temp_input1, temp_in);
  154. show(TEMP, temp_max1, temp_max);
  155. show(TEMP, temp_hyst1, temp_hyst);
  156. show(BOOL, fan_auto1, fan_auto1);
  157. show_fan(fan_input1, fan_in, 0);
  158. show_fan(fan_input2, fan_in, 1);
  159. show_fan(fan_min1, fan_min, 0);
  160. show_fan(fan_min2, fan_min, 1);
  161. show(DIV, fan_div1, fan_div[0]);
  162. show(DIV, fan_div2, fan_div[1]);
  163. show(VDD, in_input0, voltage_in[0]);
  164. show(IN, in_input1, voltage_in[1]);
  165. show(IN, in_input2, voltage_in[2]);
  166. show(IN, in_input3, voltage_in[3]);
  167. show(VDD, in_min0, voltage_min[0]);
  168. show(IN, in_min1, voltage_min[1]);
  169. show(IN, in_min2, voltage_min[2]);
  170. show(IN, in_min3, voltage_min[3]);
  171. show(VDD, in_max0, voltage_max[0]);
  172. show(IN, in_max1, voltage_max[1]);
  173. show(IN, in_max2, voltage_max[2]);
  174. show(IN, in_max3, voltage_max[3]);
  175. show(RAW, alarms, alarms);
  176. show(BOOL, beep_enable, beep_enable);
  177. show(BEEP_MASK, beep_mask, beep_mask);
  178. #define set(type, suffix, value, reg) \
  179. static ssize_t set_##suffix(struct device *dev, struct device_attribute *attr, const char *buf, \
  180. size_t count) \
  181. { \
  182. struct i2c_client *client = to_i2c_client(dev); \
  183. struct gl518_data *data = i2c_get_clientdata(client); \
  184. long val = simple_strtol(buf, NULL, 10); \
  185. \
  186. mutex_lock(&data->update_lock); \
  187. data->value = type##_TO_REG(val); \
  188. gl518_write_value(client, reg, data->value); \
  189. mutex_unlock(&data->update_lock); \
  190. return count; \
  191. }
  192. #define set_bits(type, suffix, value, reg, mask, shift) \
  193. static ssize_t set_##suffix(struct device *dev, struct device_attribute *attr, const char *buf, \
  194. size_t count) \
  195. { \
  196. struct i2c_client *client = to_i2c_client(dev); \
  197. struct gl518_data *data = i2c_get_clientdata(client); \
  198. int regvalue; \
  199. unsigned long val = simple_strtoul(buf, NULL, 10); \
  200. \
  201. mutex_lock(&data->update_lock); \
  202. regvalue = gl518_read_value(client, reg); \
  203. data->value = type##_TO_REG(val); \
  204. regvalue = (regvalue & ~mask) | (data->value << shift); \
  205. gl518_write_value(client, reg, regvalue); \
  206. mutex_unlock(&data->update_lock); \
  207. return count; \
  208. }
  209. #define set_low(type, suffix, value, reg) \
  210. set_bits(type, suffix, value, reg, 0x00ff, 0)
  211. #define set_high(type, suffix, value, reg) \
  212. set_bits(type, suffix, value, reg, 0xff00, 8)
  213. set(TEMP, temp_max1, temp_max, GL518_REG_TEMP_MAX);
  214. set(TEMP, temp_hyst1, temp_hyst, GL518_REG_TEMP_HYST);
  215. set_bits(BOOL, fan_auto1, fan_auto1, GL518_REG_MISC, 0x08, 3);
  216. set_bits(DIV, fan_div1, fan_div[0], GL518_REG_MISC, 0xc0, 6);
  217. set_bits(DIV, fan_div2, fan_div[1], GL518_REG_MISC, 0x30, 4);
  218. set_low(VDD, in_min0, voltage_min[0], GL518_REG_VDD_LIMIT);
  219. set_low(IN, in_min1, voltage_min[1], GL518_REG_VIN1_LIMIT);
  220. set_low(IN, in_min2, voltage_min[2], GL518_REG_VIN2_LIMIT);
  221. set_low(IN, in_min3, voltage_min[3], GL518_REG_VIN3_LIMIT);
  222. set_high(VDD, in_max0, voltage_max[0], GL518_REG_VDD_LIMIT);
  223. set_high(IN, in_max1, voltage_max[1], GL518_REG_VIN1_LIMIT);
  224. set_high(IN, in_max2, voltage_max[2], GL518_REG_VIN2_LIMIT);
  225. set_high(IN, in_max3, voltage_max[3], GL518_REG_VIN3_LIMIT);
  226. set_bits(BOOL, beep_enable, beep_enable, GL518_REG_CONF, 0x04, 2);
  227. set(BEEP_MASK, beep_mask, beep_mask, GL518_REG_ALARM);
  228. static ssize_t set_fan_min1(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  229. {
  230. struct i2c_client *client = to_i2c_client(dev);
  231. struct gl518_data *data = i2c_get_clientdata(client);
  232. int regvalue;
  233. unsigned long val = simple_strtoul(buf, NULL, 10);
  234. mutex_lock(&data->update_lock);
  235. regvalue = gl518_read_value(client, GL518_REG_FAN_LIMIT);
  236. data->fan_min[0] = FAN_TO_REG(val,
  237. DIV_FROM_REG(data->fan_div[0]));
  238. regvalue = (regvalue & 0x00ff) | (data->fan_min[0] << 8);
  239. gl518_write_value(client, GL518_REG_FAN_LIMIT, regvalue);
  240. data->beep_mask = gl518_read_value(client, GL518_REG_ALARM);
  241. if (data->fan_min[0] == 0)
  242. data->alarm_mask &= ~0x20;
  243. else
  244. data->alarm_mask |= 0x20;
  245. data->beep_mask &= data->alarm_mask;
  246. gl518_write_value(client, GL518_REG_ALARM, data->beep_mask);
  247. mutex_unlock(&data->update_lock);
  248. return count;
  249. }
  250. static ssize_t set_fan_min2(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  251. {
  252. struct i2c_client *client = to_i2c_client(dev);
  253. struct gl518_data *data = i2c_get_clientdata(client);
  254. int regvalue;
  255. unsigned long val = simple_strtoul(buf, NULL, 10);
  256. mutex_lock(&data->update_lock);
  257. regvalue = gl518_read_value(client, GL518_REG_FAN_LIMIT);
  258. data->fan_min[1] = FAN_TO_REG(val,
  259. DIV_FROM_REG(data->fan_div[1]));
  260. regvalue = (regvalue & 0xff00) | data->fan_min[1];
  261. gl518_write_value(client, GL518_REG_FAN_LIMIT, regvalue);
  262. data->beep_mask = gl518_read_value(client, GL518_REG_ALARM);
  263. if (data->fan_min[1] == 0)
  264. data->alarm_mask &= ~0x40;
  265. else
  266. data->alarm_mask |= 0x40;
  267. data->beep_mask &= data->alarm_mask;
  268. gl518_write_value(client, GL518_REG_ALARM, data->beep_mask);
  269. mutex_unlock(&data->update_lock);
  270. return count;
  271. }
  272. static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input1, NULL);
  273. static DEVICE_ATTR(temp1_max, S_IWUSR|S_IRUGO, show_temp_max1, set_temp_max1);
  274. static DEVICE_ATTR(temp1_max_hyst, S_IWUSR|S_IRUGO,
  275. show_temp_hyst1, set_temp_hyst1);
  276. static DEVICE_ATTR(fan1_auto, S_IWUSR|S_IRUGO, show_fan_auto1, set_fan_auto1);
  277. static DEVICE_ATTR(fan1_input, S_IRUGO, show_fan_input1, NULL);
  278. static DEVICE_ATTR(fan2_input, S_IRUGO, show_fan_input2, NULL);
  279. static DEVICE_ATTR(fan1_min, S_IWUSR|S_IRUGO, show_fan_min1, set_fan_min1);
  280. static DEVICE_ATTR(fan2_min, S_IWUSR|S_IRUGO, show_fan_min2, set_fan_min2);
  281. static DEVICE_ATTR(fan1_div, S_IWUSR|S_IRUGO, show_fan_div1, set_fan_div1);
  282. static DEVICE_ATTR(fan2_div, S_IWUSR|S_IRUGO, show_fan_div2, set_fan_div2);
  283. static DEVICE_ATTR(in0_input, S_IRUGO, show_in_input0, NULL);
  284. static DEVICE_ATTR(in1_input, S_IRUGO, show_in_input1, NULL);
  285. static DEVICE_ATTR(in2_input, S_IRUGO, show_in_input2, NULL);
  286. static DEVICE_ATTR(in3_input, S_IRUGO, show_in_input3, NULL);
  287. static DEVICE_ATTR(in0_min, S_IWUSR|S_IRUGO, show_in_min0, set_in_min0);
  288. static DEVICE_ATTR(in1_min, S_IWUSR|S_IRUGO, show_in_min1, set_in_min1);
  289. static DEVICE_ATTR(in2_min, S_IWUSR|S_IRUGO, show_in_min2, set_in_min2);
  290. static DEVICE_ATTR(in3_min, S_IWUSR|S_IRUGO, show_in_min3, set_in_min3);
  291. static DEVICE_ATTR(in0_max, S_IWUSR|S_IRUGO, show_in_max0, set_in_max0);
  292. static DEVICE_ATTR(in1_max, S_IWUSR|S_IRUGO, show_in_max1, set_in_max1);
  293. static DEVICE_ATTR(in2_max, S_IWUSR|S_IRUGO, show_in_max2, set_in_max2);
  294. static DEVICE_ATTR(in3_max, S_IWUSR|S_IRUGO, show_in_max3, set_in_max3);
  295. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  296. static DEVICE_ATTR(beep_enable, S_IWUSR|S_IRUGO,
  297. show_beep_enable, set_beep_enable);
  298. static DEVICE_ATTR(beep_mask, S_IWUSR|S_IRUGO,
  299. show_beep_mask, set_beep_mask);
  300. static struct attribute *gl518_attributes[] = {
  301. &dev_attr_in0_input.attr,
  302. &dev_attr_in1_input.attr,
  303. &dev_attr_in2_input.attr,
  304. &dev_attr_in3_input.attr,
  305. &dev_attr_in0_min.attr,
  306. &dev_attr_in1_min.attr,
  307. &dev_attr_in2_min.attr,
  308. &dev_attr_in3_min.attr,
  309. &dev_attr_in0_max.attr,
  310. &dev_attr_in1_max.attr,
  311. &dev_attr_in2_max.attr,
  312. &dev_attr_in3_max.attr,
  313. &dev_attr_fan1_auto.attr,
  314. &dev_attr_fan1_input.attr,
  315. &dev_attr_fan2_input.attr,
  316. &dev_attr_fan1_min.attr,
  317. &dev_attr_fan2_min.attr,
  318. &dev_attr_fan1_div.attr,
  319. &dev_attr_fan2_div.attr,
  320. &dev_attr_temp1_input.attr,
  321. &dev_attr_temp1_max.attr,
  322. &dev_attr_temp1_max_hyst.attr,
  323. &dev_attr_alarms.attr,
  324. &dev_attr_beep_enable.attr,
  325. &dev_attr_beep_mask.attr,
  326. NULL
  327. };
  328. static const struct attribute_group gl518_group = {
  329. .attrs = gl518_attributes,
  330. };
  331. /*
  332. * Real code
  333. */
  334. static int gl518_attach_adapter(struct i2c_adapter *adapter)
  335. {
  336. if (!(adapter->class & I2C_CLASS_HWMON))
  337. return 0;
  338. return i2c_probe(adapter, &addr_data, gl518_detect);
  339. }
  340. static int gl518_detect(struct i2c_adapter *adapter, int address, int kind)
  341. {
  342. int i;
  343. struct i2c_client *client;
  344. struct gl518_data *data;
  345. int err = 0;
  346. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
  347. I2C_FUNC_SMBUS_WORD_DATA))
  348. goto exit;
  349. /* OK. For now, we presume we have a valid client. We now create the
  350. client structure, even though we cannot fill it completely yet.
  351. But it allows us to access gl518_{read,write}_value. */
  352. if (!(data = kzalloc(sizeof(struct gl518_data), GFP_KERNEL))) {
  353. err = -ENOMEM;
  354. goto exit;
  355. }
  356. client = &data->client;
  357. i2c_set_clientdata(client, data);
  358. client->addr = address;
  359. client->adapter = adapter;
  360. client->driver = &gl518_driver;
  361. /* Now, we do the remaining detection. */
  362. if (kind < 0) {
  363. if ((gl518_read_value(client, GL518_REG_CHIP_ID) != 0x80)
  364. || (gl518_read_value(client, GL518_REG_CONF) & 0x80))
  365. goto exit_free;
  366. }
  367. /* Determine the chip type. */
  368. if (kind <= 0) {
  369. i = gl518_read_value(client, GL518_REG_REVISION);
  370. if (i == 0x00) {
  371. kind = gl518sm_r00;
  372. } else if (i == 0x80) {
  373. kind = gl518sm_r80;
  374. } else {
  375. if (kind <= 0)
  376. dev_info(&adapter->dev,
  377. "Ignoring 'force' parameter for unknown "
  378. "chip at adapter %d, address 0x%02x\n",
  379. i2c_adapter_id(adapter), address);
  380. goto exit_free;
  381. }
  382. }
  383. /* Fill in the remaining client fields */
  384. strlcpy(client->name, "gl518sm", I2C_NAME_SIZE);
  385. data->type = kind;
  386. mutex_init(&data->update_lock);
  387. /* Tell the I2C layer a new client has arrived */
  388. if ((err = i2c_attach_client(client)))
  389. goto exit_free;
  390. /* Initialize the GL518SM chip */
  391. data->alarm_mask = 0xff;
  392. data->voltage_in[0]=data->voltage_in[1]=data->voltage_in[2]=0;
  393. gl518_init_client(client);
  394. /* Register sysfs hooks */
  395. if ((err = sysfs_create_group(&client->dev.kobj, &gl518_group)))
  396. goto exit_detach;
  397. data->hwmon_dev = hwmon_device_register(&client->dev);
  398. if (IS_ERR(data->hwmon_dev)) {
  399. err = PTR_ERR(data->hwmon_dev);
  400. goto exit_remove_files;
  401. }
  402. return 0;
  403. exit_remove_files:
  404. sysfs_remove_group(&client->dev.kobj, &gl518_group);
  405. exit_detach:
  406. i2c_detach_client(client);
  407. exit_free:
  408. kfree(data);
  409. exit:
  410. return err;
  411. }
  412. /* Called when we have found a new GL518SM.
  413. Note that we preserve D4:NoFan2 and D2:beep_enable. */
  414. static void gl518_init_client(struct i2c_client *client)
  415. {
  416. /* Make sure we leave D7:Reset untouched */
  417. u8 regvalue = gl518_read_value(client, GL518_REG_CONF) & 0x7f;
  418. /* Comparator mode (D3=0), standby mode (D6=0) */
  419. gl518_write_value(client, GL518_REG_CONF, (regvalue &= 0x37));
  420. /* Never interrupts */
  421. gl518_write_value(client, GL518_REG_MASK, 0x00);
  422. /* Clear status register (D5=1), start (D6=1) */
  423. gl518_write_value(client, GL518_REG_CONF, 0x20 | regvalue);
  424. gl518_write_value(client, GL518_REG_CONF, 0x40 | regvalue);
  425. }
  426. static int gl518_detach_client(struct i2c_client *client)
  427. {
  428. struct gl518_data *data = i2c_get_clientdata(client);
  429. int err;
  430. hwmon_device_unregister(data->hwmon_dev);
  431. sysfs_remove_group(&client->dev.kobj, &gl518_group);
  432. if ((err = i2c_detach_client(client)))
  433. return err;
  434. kfree(data);
  435. return 0;
  436. }
  437. /* Registers 0x07 to 0x0c are word-sized, others are byte-sized
  438. GL518 uses a high-byte first convention, which is exactly opposite to
  439. the SMBus standard. */
  440. static int gl518_read_value(struct i2c_client *client, u8 reg)
  441. {
  442. if ((reg >= 0x07) && (reg <= 0x0c))
  443. return swab16(i2c_smbus_read_word_data(client, reg));
  444. else
  445. return i2c_smbus_read_byte_data(client, reg);
  446. }
  447. static int gl518_write_value(struct i2c_client *client, u8 reg, u16 value)
  448. {
  449. if ((reg >= 0x07) && (reg <= 0x0c))
  450. return i2c_smbus_write_word_data(client, reg, swab16(value));
  451. else
  452. return i2c_smbus_write_byte_data(client, reg, value);
  453. }
  454. static struct gl518_data *gl518_update_device(struct device *dev)
  455. {
  456. struct i2c_client *client = to_i2c_client(dev);
  457. struct gl518_data *data = i2c_get_clientdata(client);
  458. int val;
  459. mutex_lock(&data->update_lock);
  460. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  461. || !data->valid) {
  462. dev_dbg(&client->dev, "Starting gl518 update\n");
  463. data->alarms = gl518_read_value(client, GL518_REG_INT);
  464. data->beep_mask = gl518_read_value(client, GL518_REG_ALARM);
  465. val = gl518_read_value(client, GL518_REG_VDD_LIMIT);
  466. data->voltage_min[0] = val & 0xff;
  467. data->voltage_max[0] = (val >> 8) & 0xff;
  468. val = gl518_read_value(client, GL518_REG_VIN1_LIMIT);
  469. data->voltage_min[1] = val & 0xff;
  470. data->voltage_max[1] = (val >> 8) & 0xff;
  471. val = gl518_read_value(client, GL518_REG_VIN2_LIMIT);
  472. data->voltage_min[2] = val & 0xff;
  473. data->voltage_max[2] = (val >> 8) & 0xff;
  474. val = gl518_read_value(client, GL518_REG_VIN3_LIMIT);
  475. data->voltage_min[3] = val & 0xff;
  476. data->voltage_max[3] = (val >> 8) & 0xff;
  477. val = gl518_read_value(client, GL518_REG_FAN_COUNT);
  478. data->fan_in[0] = (val >> 8) & 0xff;
  479. data->fan_in[1] = val & 0xff;
  480. val = gl518_read_value(client, GL518_REG_FAN_LIMIT);
  481. data->fan_min[0] = (val >> 8) & 0xff;
  482. data->fan_min[1] = val & 0xff;
  483. data->temp_in = gl518_read_value(client, GL518_REG_TEMP_IN);
  484. data->temp_max =
  485. gl518_read_value(client, GL518_REG_TEMP_MAX);
  486. data->temp_hyst =
  487. gl518_read_value(client, GL518_REG_TEMP_HYST);
  488. val = gl518_read_value(client, GL518_REG_MISC);
  489. data->fan_div[0] = (val >> 6) & 0x03;
  490. data->fan_div[1] = (val >> 4) & 0x03;
  491. data->fan_auto1 = (val >> 3) & 0x01;
  492. data->alarms &= data->alarm_mask;
  493. val = gl518_read_value(client, GL518_REG_CONF);
  494. data->beep_enable = (val >> 2) & 1;
  495. if (data->type != gl518sm_r00) {
  496. data->voltage_in[0] =
  497. gl518_read_value(client, GL518_REG_VDD);
  498. data->voltage_in[1] =
  499. gl518_read_value(client, GL518_REG_VIN1);
  500. data->voltage_in[2] =
  501. gl518_read_value(client, GL518_REG_VIN2);
  502. }
  503. data->voltage_in[3] =
  504. gl518_read_value(client, GL518_REG_VIN3);
  505. data->last_updated = jiffies;
  506. data->valid = 1;
  507. }
  508. mutex_unlock(&data->update_lock);
  509. return data;
  510. }
  511. static int __init sensors_gl518sm_init(void)
  512. {
  513. return i2c_add_driver(&gl518_driver);
  514. }
  515. static void __exit sensors_gl518sm_exit(void)
  516. {
  517. i2c_del_driver(&gl518_driver);
  518. }
  519. MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
  520. "Kyosti Malkki <kmalkki@cc.hut.fi> and "
  521. "Hong-Gunn Chew <hglinux@gunnet.org>");
  522. MODULE_DESCRIPTION("GL518SM driver");
  523. MODULE_LICENSE("GPL");
  524. module_init(sensors_gl518sm_init);
  525. module_exit(sensors_gl518sm_exit);