gl518sm.c 21 KB

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