gl518sm.c 20 KB

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