gl518sm.c 20 KB

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