gl518sm.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  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/i2c-sensor.h>
  44. #include <linux/hwmon.h>
  45. #include <linux/err.h>
  46. /* Addresses to scan */
  47. static unsigned short normal_i2c[] = { 0x2c, 0x2d, I2C_CLIENT_END };
  48. static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
  49. /* Insmod parameters */
  50. SENSORS_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 semaphore 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. .owner = THIS_MODULE,
  137. .name = "gl518sm",
  138. .id = I2C_DRIVERID_GL518,
  139. .flags = I2C_DF_NOTIFY,
  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. down(&data->update_lock); \
  193. data->value = type##_TO_REG(val); \
  194. gl518_write_value(client, reg, data->value); \
  195. up(&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. down(&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. up(&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. down(&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. up(&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. down(&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. up(&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. /*
  307. * Real code
  308. */
  309. static int gl518_attach_adapter(struct i2c_adapter *adapter)
  310. {
  311. if (!(adapter->class & I2C_CLASS_HWMON))
  312. return 0;
  313. return i2c_detect(adapter, &addr_data, gl518_detect);
  314. }
  315. static int gl518_detect(struct i2c_adapter *adapter, int address, int kind)
  316. {
  317. int i;
  318. struct i2c_client *new_client;
  319. struct gl518_data *data;
  320. int err = 0;
  321. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
  322. I2C_FUNC_SMBUS_WORD_DATA))
  323. goto exit;
  324. /* OK. For now, we presume we have a valid client. We now create the
  325. client structure, even though we cannot fill it completely yet.
  326. But it allows us to access gl518_{read,write}_value. */
  327. if (!(data = kmalloc(sizeof(struct gl518_data), GFP_KERNEL))) {
  328. err = -ENOMEM;
  329. goto exit;
  330. }
  331. memset(data, 0, sizeof(struct gl518_data));
  332. new_client = &data->client;
  333. i2c_set_clientdata(new_client, data);
  334. new_client->addr = address;
  335. new_client->adapter = adapter;
  336. new_client->driver = &gl518_driver;
  337. new_client->flags = 0;
  338. /* Now, we do the remaining detection. */
  339. if (kind < 0) {
  340. if ((gl518_read_value(new_client, GL518_REG_CHIP_ID) != 0x80)
  341. || (gl518_read_value(new_client, GL518_REG_CONF) & 0x80))
  342. goto exit_free;
  343. }
  344. /* Determine the chip type. */
  345. if (kind <= 0) {
  346. i = gl518_read_value(new_client, GL518_REG_REVISION);
  347. if (i == 0x00) {
  348. kind = gl518sm_r00;
  349. } else if (i == 0x80) {
  350. kind = gl518sm_r80;
  351. } else {
  352. if (kind <= 0)
  353. dev_info(&adapter->dev,
  354. "Ignoring 'force' parameter for unknown "
  355. "chip at adapter %d, address 0x%02x\n",
  356. i2c_adapter_id(adapter), address);
  357. goto exit_free;
  358. }
  359. }
  360. /* Fill in the remaining client fields */
  361. strlcpy(new_client->name, "gl518sm", I2C_NAME_SIZE);
  362. data->type = kind;
  363. data->valid = 0;
  364. init_MUTEX(&data->update_lock);
  365. /* Tell the I2C layer a new client has arrived */
  366. if ((err = i2c_attach_client(new_client)))
  367. goto exit_free;
  368. /* Initialize the GL518SM chip */
  369. data->alarm_mask = 0xff;
  370. data->voltage_in[0]=data->voltage_in[1]=data->voltage_in[2]=0;
  371. gl518_init_client((struct i2c_client *) new_client);
  372. /* Register sysfs hooks */
  373. data->class_dev = hwmon_device_register(&new_client->dev);
  374. if (IS_ERR(data->class_dev)) {
  375. err = PTR_ERR(data->class_dev);
  376. goto exit_detach;
  377. }
  378. device_create_file(&new_client->dev, &dev_attr_in0_input);
  379. device_create_file(&new_client->dev, &dev_attr_in1_input);
  380. device_create_file(&new_client->dev, &dev_attr_in2_input);
  381. device_create_file(&new_client->dev, &dev_attr_in3_input);
  382. device_create_file(&new_client->dev, &dev_attr_in0_min);
  383. device_create_file(&new_client->dev, &dev_attr_in1_min);
  384. device_create_file(&new_client->dev, &dev_attr_in2_min);
  385. device_create_file(&new_client->dev, &dev_attr_in3_min);
  386. device_create_file(&new_client->dev, &dev_attr_in0_max);
  387. device_create_file(&new_client->dev, &dev_attr_in1_max);
  388. device_create_file(&new_client->dev, &dev_attr_in2_max);
  389. device_create_file(&new_client->dev, &dev_attr_in3_max);
  390. device_create_file(&new_client->dev, &dev_attr_fan1_auto);
  391. device_create_file(&new_client->dev, &dev_attr_fan1_input);
  392. device_create_file(&new_client->dev, &dev_attr_fan2_input);
  393. device_create_file(&new_client->dev, &dev_attr_fan1_min);
  394. device_create_file(&new_client->dev, &dev_attr_fan2_min);
  395. device_create_file(&new_client->dev, &dev_attr_fan1_div);
  396. device_create_file(&new_client->dev, &dev_attr_fan2_div);
  397. device_create_file(&new_client->dev, &dev_attr_temp1_input);
  398. device_create_file(&new_client->dev, &dev_attr_temp1_max);
  399. device_create_file(&new_client->dev, &dev_attr_temp1_max_hyst);
  400. device_create_file(&new_client->dev, &dev_attr_alarms);
  401. device_create_file(&new_client->dev, &dev_attr_beep_enable);
  402. device_create_file(&new_client->dev, &dev_attr_beep_mask);
  403. return 0;
  404. /* OK, this is not exactly good programming practice, usually. But it is
  405. very code-efficient in this case. */
  406. exit_detach:
  407. i2c_detach_client(new_client);
  408. exit_free:
  409. kfree(data);
  410. exit:
  411. return err;
  412. }
  413. /* Called when we have found a new GL518SM.
  414. Note that we preserve D4:NoFan2 and D2:beep_enable. */
  415. static void gl518_init_client(struct i2c_client *client)
  416. {
  417. /* Make sure we leave D7:Reset untouched */
  418. u8 regvalue = gl518_read_value(client, GL518_REG_CONF) & 0x7f;
  419. /* Comparator mode (D3=0), standby mode (D6=0) */
  420. gl518_write_value(client, GL518_REG_CONF, (regvalue &= 0x37));
  421. /* Never interrupts */
  422. gl518_write_value(client, GL518_REG_MASK, 0x00);
  423. /* Clear status register (D5=1), start (D6=1) */
  424. gl518_write_value(client, GL518_REG_CONF, 0x20 | regvalue);
  425. gl518_write_value(client, GL518_REG_CONF, 0x40 | regvalue);
  426. }
  427. static int gl518_detach_client(struct i2c_client *client)
  428. {
  429. struct gl518_data *data = i2c_get_clientdata(client);
  430. int err;
  431. hwmon_device_unregister(data->class_dev);
  432. if ((err = i2c_detach_client(client))) {
  433. dev_err(&client->dev, "Client deregistration failed, "
  434. "client not detached.\n");
  435. return err;
  436. }
  437. kfree(data);
  438. return 0;
  439. }
  440. /* Registers 0x07 to 0x0c are word-sized, others are byte-sized
  441. GL518 uses a high-byte first convention, which is exactly opposite to
  442. the usual practice. */
  443. static int gl518_read_value(struct i2c_client *client, u8 reg)
  444. {
  445. if ((reg >= 0x07) && (reg <= 0x0c))
  446. return swab16(i2c_smbus_read_word_data(client, reg));
  447. else
  448. return i2c_smbus_read_byte_data(client, reg);
  449. }
  450. /* Registers 0x07 to 0x0c are word-sized, others are byte-sized
  451. GL518 uses a high-byte first convention, which is exactly opposite to
  452. the usual practice. */
  453. static int gl518_write_value(struct i2c_client *client, u8 reg, u16 value)
  454. {
  455. if ((reg >= 0x07) && (reg <= 0x0c))
  456. return i2c_smbus_write_word_data(client, reg, swab16(value));
  457. else
  458. return i2c_smbus_write_byte_data(client, reg, value);
  459. }
  460. static struct gl518_data *gl518_update_device(struct device *dev)
  461. {
  462. struct i2c_client *client = to_i2c_client(dev);
  463. struct gl518_data *data = i2c_get_clientdata(client);
  464. int val;
  465. down(&data->update_lock);
  466. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  467. || !data->valid) {
  468. dev_dbg(&client->dev, "Starting gl518 update\n");
  469. data->alarms = gl518_read_value(client, GL518_REG_INT);
  470. data->beep_mask = gl518_read_value(client, GL518_REG_ALARM);
  471. val = gl518_read_value(client, GL518_REG_VDD_LIMIT);
  472. data->voltage_min[0] = val & 0xff;
  473. data->voltage_max[0] = (val >> 8) & 0xff;
  474. val = gl518_read_value(client, GL518_REG_VIN1_LIMIT);
  475. data->voltage_min[1] = val & 0xff;
  476. data->voltage_max[1] = (val >> 8) & 0xff;
  477. val = gl518_read_value(client, GL518_REG_VIN2_LIMIT);
  478. data->voltage_min[2] = val & 0xff;
  479. data->voltage_max[2] = (val >> 8) & 0xff;
  480. val = gl518_read_value(client, GL518_REG_VIN3_LIMIT);
  481. data->voltage_min[3] = val & 0xff;
  482. data->voltage_max[3] = (val >> 8) & 0xff;
  483. val = gl518_read_value(client, GL518_REG_FAN_COUNT);
  484. data->fan_in[0] = (val >> 8) & 0xff;
  485. data->fan_in[1] = val & 0xff;
  486. val = gl518_read_value(client, GL518_REG_FAN_LIMIT);
  487. data->fan_min[0] = (val >> 8) & 0xff;
  488. data->fan_min[1] = val & 0xff;
  489. data->temp_in = gl518_read_value(client, GL518_REG_TEMP_IN);
  490. data->temp_max =
  491. gl518_read_value(client, GL518_REG_TEMP_MAX);
  492. data->temp_hyst =
  493. gl518_read_value(client, GL518_REG_TEMP_HYST);
  494. val = gl518_read_value(client, GL518_REG_MISC);
  495. data->fan_div[0] = (val >> 6) & 0x03;
  496. data->fan_div[1] = (val >> 4) & 0x03;
  497. data->fan_auto1 = (val >> 3) & 0x01;
  498. data->alarms &= data->alarm_mask;
  499. val = gl518_read_value(client, GL518_REG_CONF);
  500. data->beep_enable = (val >> 2) & 1;
  501. if (data->type != gl518sm_r00) {
  502. data->voltage_in[0] =
  503. gl518_read_value(client, GL518_REG_VDD);
  504. data->voltage_in[1] =
  505. gl518_read_value(client, GL518_REG_VIN1);
  506. data->voltage_in[2] =
  507. gl518_read_value(client, GL518_REG_VIN2);
  508. }
  509. data->voltage_in[3] =
  510. gl518_read_value(client, GL518_REG_VIN3);
  511. data->last_updated = jiffies;
  512. data->valid = 1;
  513. }
  514. up(&data->update_lock);
  515. return data;
  516. }
  517. static int __init sensors_gl518sm_init(void)
  518. {
  519. return i2c_add_driver(&gl518_driver);
  520. }
  521. static void __exit sensors_gl518sm_exit(void)
  522. {
  523. i2c_del_driver(&gl518_driver);
  524. }
  525. MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
  526. "Kyosti Malkki <kmalkki@cc.hut.fi> and "
  527. "Hong-Gunn Chew <hglinux@gunnet.org>");
  528. MODULE_DESCRIPTION("GL518SM driver");
  529. MODULE_LICENSE("GPL");
  530. module_init(sensors_gl518sm_init);
  531. module_exit(sensors_gl518sm_exit);