gl520sm.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  1. /*
  2. gl520sm.c - Part of lm_sensors, Linux kernel modules for hardware
  3. monitoring
  4. Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl>,
  5. Kyösti Mälkki <kmalkki@cc.hut.fi>
  6. Copyright (c) 2005 Maarten Deprez <maartendeprez@users.sourceforge.net>
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include <linux/module.h>
  20. #include <linux/init.h>
  21. #include <linux/slab.h>
  22. #include <linux/jiffies.h>
  23. #include <linux/i2c.h>
  24. #include <linux/hwmon.h>
  25. #include <linux/hwmon-vid.h>
  26. #include <linux/err.h>
  27. #include <linux/mutex.h>
  28. #include <linux/sysfs.h>
  29. /* Type of the extra sensor */
  30. static unsigned short extra_sensor_type;
  31. module_param(extra_sensor_type, ushort, 0);
  32. MODULE_PARM_DESC(extra_sensor_type, "Type of extra sensor (0=autodetect, 1=temperature, 2=voltage)");
  33. /* Addresses to scan */
  34. static unsigned short normal_i2c[] = { 0x2c, 0x2d, I2C_CLIENT_END };
  35. /* Insmod parameters */
  36. I2C_CLIENT_INSMOD_1(gl520sm);
  37. /* Many GL520 constants specified below
  38. One of the inputs can be configured as either temp or voltage.
  39. That's why _TEMP2 and _IN4 access the same register
  40. */
  41. /* The GL520 registers */
  42. #define GL520_REG_CHIP_ID 0x00
  43. #define GL520_REG_REVISION 0x01
  44. #define GL520_REG_CONF 0x03
  45. #define GL520_REG_MASK 0x11
  46. #define GL520_REG_VID_INPUT 0x02
  47. #define GL520_REG_IN0_INPUT 0x15
  48. #define GL520_REG_IN0_LIMIT 0x0c
  49. #define GL520_REG_IN0_MIN GL520_REG_IN0_LIMIT
  50. #define GL520_REG_IN0_MAX GL520_REG_IN0_LIMIT
  51. #define GL520_REG_IN1_INPUT 0x14
  52. #define GL520_REG_IN1_LIMIT 0x09
  53. #define GL520_REG_IN1_MIN GL520_REG_IN1_LIMIT
  54. #define GL520_REG_IN1_MAX GL520_REG_IN1_LIMIT
  55. #define GL520_REG_IN2_INPUT 0x13
  56. #define GL520_REG_IN2_LIMIT 0x0a
  57. #define GL520_REG_IN2_MIN GL520_REG_IN2_LIMIT
  58. #define GL520_REG_IN2_MAX GL520_REG_IN2_LIMIT
  59. #define GL520_REG_IN3_INPUT 0x0d
  60. #define GL520_REG_IN3_LIMIT 0x0b
  61. #define GL520_REG_IN3_MIN GL520_REG_IN3_LIMIT
  62. #define GL520_REG_IN3_MAX GL520_REG_IN3_LIMIT
  63. #define GL520_REG_IN4_INPUT 0x0e
  64. #define GL520_REG_IN4_MAX 0x17
  65. #define GL520_REG_IN4_MIN 0x18
  66. #define GL520_REG_TEMP1_INPUT 0x04
  67. #define GL520_REG_TEMP1_MAX 0x05
  68. #define GL520_REG_TEMP1_MAX_HYST 0x06
  69. #define GL520_REG_TEMP2_INPUT 0x0e
  70. #define GL520_REG_TEMP2_MAX 0x17
  71. #define GL520_REG_TEMP2_MAX_HYST 0x18
  72. #define GL520_REG_FAN_INPUT 0x07
  73. #define GL520_REG_FAN_MIN 0x08
  74. #define GL520_REG_FAN_DIV 0x0f
  75. #define GL520_REG_FAN_OFF GL520_REG_FAN_DIV
  76. #define GL520_REG_ALARMS 0x12
  77. #define GL520_REG_BEEP_MASK 0x10
  78. #define GL520_REG_BEEP_ENABLE GL520_REG_CONF
  79. /*
  80. * Function declarations
  81. */
  82. static int gl520_attach_adapter(struct i2c_adapter *adapter);
  83. static int gl520_detect(struct i2c_adapter *adapter, int address, int kind);
  84. static void gl520_init_client(struct i2c_client *client);
  85. static int gl520_detach_client(struct i2c_client *client);
  86. static int gl520_read_value(struct i2c_client *client, u8 reg);
  87. static int gl520_write_value(struct i2c_client *client, u8 reg, u16 value);
  88. static struct gl520_data *gl520_update_device(struct device *dev);
  89. /* Driver data */
  90. static struct i2c_driver gl520_driver = {
  91. .driver = {
  92. .name = "gl520sm",
  93. },
  94. .id = I2C_DRIVERID_GL520,
  95. .attach_adapter = gl520_attach_adapter,
  96. .detach_client = gl520_detach_client,
  97. };
  98. /* Client data */
  99. struct gl520_data {
  100. struct i2c_client client;
  101. struct class_device *class_dev;
  102. struct mutex update_lock;
  103. char valid; /* zero until the following fields are valid */
  104. unsigned long last_updated; /* in jiffies */
  105. u8 vid;
  106. u8 vrm;
  107. u8 in_input[5]; /* [0] = VVD */
  108. u8 in_min[5]; /* [0] = VDD */
  109. u8 in_max[5]; /* [0] = VDD */
  110. u8 fan_input[2];
  111. u8 fan_min[2];
  112. u8 fan_div[2];
  113. u8 fan_off;
  114. u8 temp_input[2];
  115. u8 temp_max[2];
  116. u8 temp_max_hyst[2];
  117. u8 alarms;
  118. u8 beep_enable;
  119. u8 beep_mask;
  120. u8 alarm_mask;
  121. u8 two_temps;
  122. };
  123. /*
  124. * Sysfs stuff
  125. */
  126. #define sysfs_r(type, n, item, reg) \
  127. static ssize_t get_##type##item (struct gl520_data *, char *, int); \
  128. static ssize_t get_##type##n##item (struct device *, struct device_attribute *attr, char *); \
  129. static ssize_t get_##type##n##item (struct device *dev, struct device_attribute *attr, char *buf) \
  130. { \
  131. struct gl520_data *data = gl520_update_device(dev); \
  132. return get_##type##item(data, buf, (n)); \
  133. }
  134. #define sysfs_w(type, n, item, reg) \
  135. static ssize_t set_##type##item (struct i2c_client *, struct gl520_data *, const char *, size_t, int, int); \
  136. static ssize_t set_##type##n##item (struct device *, struct device_attribute *attr, const char *, size_t); \
  137. static ssize_t set_##type##n##item (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
  138. { \
  139. struct i2c_client *client = to_i2c_client(dev); \
  140. struct gl520_data *data = i2c_get_clientdata(client); \
  141. return set_##type##item(client, data, buf, count, (n), reg); \
  142. }
  143. #define sysfs_rw_n(type, n, item, reg) \
  144. sysfs_r(type, n, item, reg) \
  145. sysfs_w(type, n, item, reg) \
  146. static DEVICE_ATTR(type##n##item, S_IRUGO | S_IWUSR, get_##type##n##item, set_##type##n##item);
  147. #define sysfs_ro_n(type, n, item, reg) \
  148. sysfs_r(type, n, item, reg) \
  149. static DEVICE_ATTR(type##n##item, S_IRUGO, get_##type##n##item, NULL);
  150. #define sysfs_rw(type, item, reg) \
  151. sysfs_r(type, 0, item, reg) \
  152. sysfs_w(type, 0, item, reg) \
  153. static DEVICE_ATTR(type##item, S_IRUGO | S_IWUSR, get_##type##0##item, set_##type##0##item);
  154. #define sysfs_ro(type, item, reg) \
  155. sysfs_r(type, 0, item, reg) \
  156. static DEVICE_ATTR(type##item, S_IRUGO, get_##type##0##item, NULL);
  157. #define sysfs_vid(n) \
  158. sysfs_ro_n(cpu, n, _vid, GL520_REG_VID_INPUT)
  159. #define sysfs_in(n) \
  160. sysfs_ro_n(in, n, _input, GL520_REG_IN##n##INPUT) \
  161. sysfs_rw_n(in, n, _min, GL520_REG_IN##n##_MIN) \
  162. sysfs_rw_n(in, n, _max, GL520_REG_IN##n##_MAX) \
  163. #define sysfs_fan(n) \
  164. sysfs_ro_n(fan, n, _input, GL520_REG_FAN_INPUT) \
  165. sysfs_rw_n(fan, n, _min, GL520_REG_FAN_MIN) \
  166. sysfs_rw_n(fan, n, _div, GL520_REG_FAN_DIV)
  167. #define sysfs_fan_off(n) \
  168. sysfs_rw_n(fan, n, _off, GL520_REG_FAN_OFF) \
  169. #define sysfs_temp(n) \
  170. sysfs_ro_n(temp, n, _input, GL520_REG_TEMP##n##_INPUT) \
  171. sysfs_rw_n(temp, n, _max, GL520_REG_TEMP##n##_MAX) \
  172. sysfs_rw_n(temp, n, _max_hyst, GL520_REG_TEMP##n##_MAX_HYST)
  173. #define sysfs_alarms() \
  174. sysfs_ro(alarms, , GL520_REG_ALARMS) \
  175. sysfs_rw(beep_enable, , GL520_REG_BEEP_ENABLE) \
  176. sysfs_rw(beep_mask, , GL520_REG_BEEP_MASK)
  177. sysfs_vid(0)
  178. sysfs_in(0)
  179. sysfs_in(1)
  180. sysfs_in(2)
  181. sysfs_in(3)
  182. sysfs_in(4)
  183. sysfs_fan(1)
  184. sysfs_fan(2)
  185. sysfs_fan_off(1)
  186. sysfs_temp(1)
  187. sysfs_temp(2)
  188. sysfs_alarms()
  189. static ssize_t get_cpu_vid(struct gl520_data *data, char *buf, int n)
  190. {
  191. return sprintf(buf, "%u\n", vid_from_reg(data->vid, data->vrm));
  192. }
  193. #define VDD_FROM_REG(val) (((val)*95+2)/4)
  194. #define VDD_TO_REG(val) (SENSORS_LIMIT((((val)*4+47)/95),0,255))
  195. #define IN_FROM_REG(val) ((val)*19)
  196. #define IN_TO_REG(val) (SENSORS_LIMIT((((val)+9)/19),0,255))
  197. static ssize_t get_in_input(struct gl520_data *data, char *buf, int n)
  198. {
  199. u8 r = data->in_input[n];
  200. if (n == 0)
  201. return sprintf(buf, "%d\n", VDD_FROM_REG(r));
  202. else
  203. return sprintf(buf, "%d\n", IN_FROM_REG(r));
  204. }
  205. static ssize_t get_in_min(struct gl520_data *data, char *buf, int n)
  206. {
  207. u8 r = data->in_min[n];
  208. if (n == 0)
  209. return sprintf(buf, "%d\n", VDD_FROM_REG(r));
  210. else
  211. return sprintf(buf, "%d\n", IN_FROM_REG(r));
  212. }
  213. static ssize_t get_in_max(struct gl520_data *data, char *buf, int n)
  214. {
  215. u8 r = data->in_max[n];
  216. if (n == 0)
  217. return sprintf(buf, "%d\n", VDD_FROM_REG(r));
  218. else
  219. return sprintf(buf, "%d\n", IN_FROM_REG(r));
  220. }
  221. static ssize_t set_in_min(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
  222. {
  223. long v = simple_strtol(buf, NULL, 10);
  224. u8 r;
  225. mutex_lock(&data->update_lock);
  226. if (n == 0)
  227. r = VDD_TO_REG(v);
  228. else
  229. r = IN_TO_REG(v);
  230. data->in_min[n] = r;
  231. if (n < 4)
  232. gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0xff) | r);
  233. else
  234. gl520_write_value(client, reg, r);
  235. mutex_unlock(&data->update_lock);
  236. return count;
  237. }
  238. static ssize_t set_in_max(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
  239. {
  240. long v = simple_strtol(buf, NULL, 10);
  241. u8 r;
  242. if (n == 0)
  243. r = VDD_TO_REG(v);
  244. else
  245. r = IN_TO_REG(v);
  246. mutex_lock(&data->update_lock);
  247. data->in_max[n] = r;
  248. if (n < 4)
  249. gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0xff00) | (r << 8));
  250. else
  251. gl520_write_value(client, reg, r);
  252. mutex_unlock(&data->update_lock);
  253. return count;
  254. }
  255. #define DIV_FROM_REG(val) (1 << (val))
  256. #define FAN_FROM_REG(val,div) ((val)==0 ? 0 : (480000/((val) << (div))))
  257. #define FAN_TO_REG(val,div) ((val)<=0?0:SENSORS_LIMIT((480000 + ((val) << ((div)-1))) / ((val) << (div)), 1, 255));
  258. static ssize_t get_fan_input(struct gl520_data *data, char *buf, int n)
  259. {
  260. return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_input[n - 1], data->fan_div[n - 1]));
  261. }
  262. static ssize_t get_fan_min(struct gl520_data *data, char *buf, int n)
  263. {
  264. return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[n - 1], data->fan_div[n - 1]));
  265. }
  266. static ssize_t get_fan_div(struct gl520_data *data, char *buf, int n)
  267. {
  268. return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[n - 1]));
  269. }
  270. static ssize_t get_fan_off(struct gl520_data *data, char *buf, int n)
  271. {
  272. return sprintf(buf, "%d\n", data->fan_off);
  273. }
  274. static ssize_t set_fan_min(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
  275. {
  276. unsigned long v = simple_strtoul(buf, NULL, 10);
  277. u8 r;
  278. mutex_lock(&data->update_lock);
  279. r = FAN_TO_REG(v, data->fan_div[n - 1]);
  280. data->fan_min[n - 1] = r;
  281. if (n == 1)
  282. gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0xff00) | (r << 8));
  283. else
  284. gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0xff) | r);
  285. data->beep_mask = gl520_read_value(client, GL520_REG_BEEP_MASK);
  286. if (data->fan_min[n - 1] == 0)
  287. data->alarm_mask &= (n == 1) ? ~0x20 : ~0x40;
  288. else
  289. data->alarm_mask |= (n == 1) ? 0x20 : 0x40;
  290. data->beep_mask &= data->alarm_mask;
  291. gl520_write_value(client, GL520_REG_BEEP_MASK, data->beep_mask);
  292. mutex_unlock(&data->update_lock);
  293. return count;
  294. }
  295. static ssize_t set_fan_div(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
  296. {
  297. unsigned long v = simple_strtoul(buf, NULL, 10);
  298. u8 r;
  299. switch (v) {
  300. case 1: r = 0; break;
  301. case 2: r = 1; break;
  302. case 4: r = 2; break;
  303. case 8: r = 3; break;
  304. default:
  305. dev_err(&client->dev, "fan_div value %ld not supported. Choose one of 1, 2, 4 or 8!\n", v);
  306. return -EINVAL;
  307. }
  308. mutex_lock(&data->update_lock);
  309. data->fan_div[n - 1] = r;
  310. if (n == 1)
  311. gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0xc0) | (r << 6));
  312. else
  313. gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0x30) | (r << 4));
  314. mutex_unlock(&data->update_lock);
  315. return count;
  316. }
  317. static ssize_t set_fan_off(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
  318. {
  319. u8 r = simple_strtoul(buf, NULL, 10)?1:0;
  320. mutex_lock(&data->update_lock);
  321. data->fan_off = r;
  322. gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0x0c) | (r << 2));
  323. mutex_unlock(&data->update_lock);
  324. return count;
  325. }
  326. #define TEMP_FROM_REG(val) (((val) - 130) * 1000)
  327. #define TEMP_TO_REG(val) (SENSORS_LIMIT(((((val)<0?(val)-500:(val)+500) / 1000)+130),0,255))
  328. static ssize_t get_temp_input(struct gl520_data *data, char *buf, int n)
  329. {
  330. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_input[n - 1]));
  331. }
  332. static ssize_t get_temp_max(struct gl520_data *data, char *buf, int n)
  333. {
  334. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[n - 1]));
  335. }
  336. static ssize_t get_temp_max_hyst(struct gl520_data *data, char *buf, int n)
  337. {
  338. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max_hyst[n - 1]));
  339. }
  340. static ssize_t set_temp_max(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
  341. {
  342. long v = simple_strtol(buf, NULL, 10);
  343. mutex_lock(&data->update_lock);
  344. data->temp_max[n - 1] = TEMP_TO_REG(v);
  345. gl520_write_value(client, reg, data->temp_max[n - 1]);
  346. mutex_unlock(&data->update_lock);
  347. return count;
  348. }
  349. static ssize_t set_temp_max_hyst(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
  350. {
  351. long v = simple_strtol(buf, NULL, 10);
  352. mutex_lock(&data->update_lock);
  353. data->temp_max_hyst[n - 1] = TEMP_TO_REG(v);
  354. gl520_write_value(client, reg, data->temp_max_hyst[n - 1]);
  355. mutex_unlock(&data->update_lock);
  356. return count;
  357. }
  358. static ssize_t get_alarms(struct gl520_data *data, char *buf, int n)
  359. {
  360. return sprintf(buf, "%d\n", data->alarms);
  361. }
  362. static ssize_t get_beep_enable(struct gl520_data *data, char *buf, int n)
  363. {
  364. return sprintf(buf, "%d\n", data->beep_enable);
  365. }
  366. static ssize_t get_beep_mask(struct gl520_data *data, char *buf, int n)
  367. {
  368. return sprintf(buf, "%d\n", data->beep_mask);
  369. }
  370. static ssize_t set_beep_enable(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
  371. {
  372. u8 r = simple_strtoul(buf, NULL, 10)?0:1;
  373. mutex_lock(&data->update_lock);
  374. data->beep_enable = !r;
  375. gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0x04) | (r << 2));
  376. mutex_unlock(&data->update_lock);
  377. return count;
  378. }
  379. static ssize_t set_beep_mask(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
  380. {
  381. u8 r = simple_strtoul(buf, NULL, 10);
  382. mutex_lock(&data->update_lock);
  383. r &= data->alarm_mask;
  384. data->beep_mask = r;
  385. gl520_write_value(client, reg, r);
  386. mutex_unlock(&data->update_lock);
  387. return count;
  388. }
  389. static struct attribute *gl520_attributes[] = {
  390. &dev_attr_cpu0_vid.attr,
  391. &dev_attr_in0_input.attr,
  392. &dev_attr_in0_min.attr,
  393. &dev_attr_in0_max.attr,
  394. &dev_attr_in1_input.attr,
  395. &dev_attr_in1_min.attr,
  396. &dev_attr_in1_max.attr,
  397. &dev_attr_in2_input.attr,
  398. &dev_attr_in2_min.attr,
  399. &dev_attr_in2_max.attr,
  400. &dev_attr_in3_input.attr,
  401. &dev_attr_in3_min.attr,
  402. &dev_attr_in3_max.attr,
  403. &dev_attr_fan1_input.attr,
  404. &dev_attr_fan1_min.attr,
  405. &dev_attr_fan1_div.attr,
  406. &dev_attr_fan1_off.attr,
  407. &dev_attr_fan2_input.attr,
  408. &dev_attr_fan2_min.attr,
  409. &dev_attr_fan2_div.attr,
  410. &dev_attr_temp1_input.attr,
  411. &dev_attr_temp1_max.attr,
  412. &dev_attr_temp1_max_hyst.attr,
  413. &dev_attr_alarms.attr,
  414. &dev_attr_beep_enable.attr,
  415. &dev_attr_beep_mask.attr,
  416. NULL
  417. };
  418. static const struct attribute_group gl520_group = {
  419. .attrs = gl520_attributes,
  420. };
  421. static struct attribute *gl520_attributes_opt[] = {
  422. &dev_attr_in4_input.attr,
  423. &dev_attr_in4_min.attr,
  424. &dev_attr_in4_max.attr,
  425. &dev_attr_temp2_input.attr,
  426. &dev_attr_temp2_max.attr,
  427. &dev_attr_temp2_max_hyst.attr,
  428. NULL
  429. };
  430. static const struct attribute_group gl520_group_opt = {
  431. .attrs = gl520_attributes_opt,
  432. };
  433. /*
  434. * Real code
  435. */
  436. static int gl520_attach_adapter(struct i2c_adapter *adapter)
  437. {
  438. if (!(adapter->class & I2C_CLASS_HWMON))
  439. return 0;
  440. return i2c_probe(adapter, &addr_data, gl520_detect);
  441. }
  442. static int gl520_detect(struct i2c_adapter *adapter, int address, int kind)
  443. {
  444. struct i2c_client *new_client;
  445. struct gl520_data *data;
  446. int err = 0;
  447. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
  448. I2C_FUNC_SMBUS_WORD_DATA))
  449. goto exit;
  450. /* OK. For now, we presume we have a valid client. We now create the
  451. client structure, even though we cannot fill it completely yet.
  452. But it allows us to access gl520_{read,write}_value. */
  453. if (!(data = kzalloc(sizeof(struct gl520_data), GFP_KERNEL))) {
  454. err = -ENOMEM;
  455. goto exit;
  456. }
  457. new_client = &data->client;
  458. i2c_set_clientdata(new_client, data);
  459. new_client->addr = address;
  460. new_client->adapter = adapter;
  461. new_client->driver = &gl520_driver;
  462. new_client->flags = 0;
  463. /* Determine the chip type. */
  464. if (kind < 0) {
  465. if ((gl520_read_value(new_client, GL520_REG_CHIP_ID) != 0x20) ||
  466. ((gl520_read_value(new_client, GL520_REG_REVISION) & 0x7f) != 0x00) ||
  467. ((gl520_read_value(new_client, GL520_REG_CONF) & 0x80) != 0x00)) {
  468. dev_dbg(&new_client->dev, "Unknown chip type, skipping\n");
  469. goto exit_free;
  470. }
  471. }
  472. /* Fill in the remaining client fields */
  473. strlcpy(new_client->name, "gl520sm", I2C_NAME_SIZE);
  474. data->valid = 0;
  475. mutex_init(&data->update_lock);
  476. /* Tell the I2C layer a new client has arrived */
  477. if ((err = i2c_attach_client(new_client)))
  478. goto exit_free;
  479. /* Initialize the GL520SM chip */
  480. gl520_init_client(new_client);
  481. /* Register sysfs hooks */
  482. if ((err = sysfs_create_group(&new_client->dev.kobj, &gl520_group)))
  483. goto exit_detach;
  484. if (data->two_temps) {
  485. if ((err = device_create_file(&new_client->dev,
  486. &dev_attr_temp2_input))
  487. || (err = device_create_file(&new_client->dev,
  488. &dev_attr_temp2_max))
  489. || (err = device_create_file(&new_client->dev,
  490. &dev_attr_temp2_max_hyst)))
  491. goto exit_remove_files;
  492. } else {
  493. if ((err = device_create_file(&new_client->dev,
  494. &dev_attr_in4_input))
  495. || (err = device_create_file(&new_client->dev,
  496. &dev_attr_in4_min))
  497. || (err = device_create_file(&new_client->dev,
  498. &dev_attr_in4_max)))
  499. goto exit_remove_files;
  500. }
  501. data->class_dev = hwmon_device_register(&new_client->dev);
  502. if (IS_ERR(data->class_dev)) {
  503. err = PTR_ERR(data->class_dev);
  504. goto exit_remove_files;
  505. }
  506. return 0;
  507. exit_remove_files:
  508. sysfs_remove_group(&new_client->dev.kobj, &gl520_group);
  509. sysfs_remove_group(&new_client->dev.kobj, &gl520_group_opt);
  510. exit_detach:
  511. i2c_detach_client(new_client);
  512. exit_free:
  513. kfree(data);
  514. exit:
  515. return err;
  516. }
  517. /* Called when we have found a new GL520SM. */
  518. static void gl520_init_client(struct i2c_client *client)
  519. {
  520. struct gl520_data *data = i2c_get_clientdata(client);
  521. u8 oldconf, conf;
  522. conf = oldconf = gl520_read_value(client, GL520_REG_CONF);
  523. data->alarm_mask = 0xff;
  524. data->vrm = vid_which_vrm();
  525. if (extra_sensor_type == 1)
  526. conf &= ~0x10;
  527. else if (extra_sensor_type == 2)
  528. conf |= 0x10;
  529. data->two_temps = !(conf & 0x10);
  530. /* If IRQ# is disabled, we can safely force comparator mode */
  531. if (!(conf & 0x20))
  532. conf &= 0xf7;
  533. /* Enable monitoring if needed */
  534. conf |= 0x40;
  535. if (conf != oldconf)
  536. gl520_write_value(client, GL520_REG_CONF, conf);
  537. gl520_update_device(&(client->dev));
  538. if (data->fan_min[0] == 0)
  539. data->alarm_mask &= ~0x20;
  540. if (data->fan_min[1] == 0)
  541. data->alarm_mask &= ~0x40;
  542. data->beep_mask &= data->alarm_mask;
  543. gl520_write_value(client, GL520_REG_BEEP_MASK, data->beep_mask);
  544. }
  545. static int gl520_detach_client(struct i2c_client *client)
  546. {
  547. struct gl520_data *data = i2c_get_clientdata(client);
  548. int err;
  549. hwmon_device_unregister(data->class_dev);
  550. sysfs_remove_group(&client->dev.kobj, &gl520_group);
  551. sysfs_remove_group(&client->dev.kobj, &gl520_group_opt);
  552. if ((err = i2c_detach_client(client)))
  553. return err;
  554. kfree(data);
  555. return 0;
  556. }
  557. /* Registers 0x07 to 0x0c are word-sized, others are byte-sized
  558. GL520 uses a high-byte first convention */
  559. static int gl520_read_value(struct i2c_client *client, u8 reg)
  560. {
  561. if ((reg >= 0x07) && (reg <= 0x0c))
  562. return swab16(i2c_smbus_read_word_data(client, reg));
  563. else
  564. return i2c_smbus_read_byte_data(client, reg);
  565. }
  566. static int gl520_write_value(struct i2c_client *client, u8 reg, u16 value)
  567. {
  568. if ((reg >= 0x07) && (reg <= 0x0c))
  569. return i2c_smbus_write_word_data(client, reg, swab16(value));
  570. else
  571. return i2c_smbus_write_byte_data(client, reg, value);
  572. }
  573. static struct gl520_data *gl520_update_device(struct device *dev)
  574. {
  575. struct i2c_client *client = to_i2c_client(dev);
  576. struct gl520_data *data = i2c_get_clientdata(client);
  577. int val;
  578. mutex_lock(&data->update_lock);
  579. if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) {
  580. dev_dbg(&client->dev, "Starting gl520sm update\n");
  581. data->alarms = gl520_read_value(client, GL520_REG_ALARMS);
  582. data->beep_mask = gl520_read_value(client, GL520_REG_BEEP_MASK);
  583. data->vid = gl520_read_value(client, GL520_REG_VID_INPUT) & 0x1f;
  584. val = gl520_read_value(client, GL520_REG_IN0_LIMIT);
  585. data->in_min[0] = val & 0xff;
  586. data->in_max[0] = (val >> 8) & 0xff;
  587. val = gl520_read_value(client, GL520_REG_IN1_LIMIT);
  588. data->in_min[1] = val & 0xff;
  589. data->in_max[1] = (val >> 8) & 0xff;
  590. val = gl520_read_value(client, GL520_REG_IN2_LIMIT);
  591. data->in_min[2] = val & 0xff;
  592. data->in_max[2] = (val >> 8) & 0xff;
  593. val = gl520_read_value(client, GL520_REG_IN3_LIMIT);
  594. data->in_min[3] = val & 0xff;
  595. data->in_max[3] = (val >> 8) & 0xff;
  596. val = gl520_read_value(client, GL520_REG_FAN_INPUT);
  597. data->fan_input[0] = (val >> 8) & 0xff;
  598. data->fan_input[1] = val & 0xff;
  599. val = gl520_read_value(client, GL520_REG_FAN_MIN);
  600. data->fan_min[0] = (val >> 8) & 0xff;
  601. data->fan_min[1] = val & 0xff;
  602. data->temp_input[0] = gl520_read_value(client, GL520_REG_TEMP1_INPUT);
  603. data->temp_max[0] = gl520_read_value(client, GL520_REG_TEMP1_MAX);
  604. data->temp_max_hyst[0] = gl520_read_value(client, GL520_REG_TEMP1_MAX_HYST);
  605. val = gl520_read_value(client, GL520_REG_FAN_DIV);
  606. data->fan_div[0] = (val >> 6) & 0x03;
  607. data->fan_div[1] = (val >> 4) & 0x03;
  608. data->fan_off = (val >> 2) & 0x01;
  609. data->alarms &= data->alarm_mask;
  610. val = gl520_read_value(client, GL520_REG_CONF);
  611. data->beep_enable = !((val >> 2) & 1);
  612. data->in_input[0] = gl520_read_value(client, GL520_REG_IN0_INPUT);
  613. data->in_input[1] = gl520_read_value(client, GL520_REG_IN1_INPUT);
  614. data->in_input[2] = gl520_read_value(client, GL520_REG_IN2_INPUT);
  615. data->in_input[3] = gl520_read_value(client, GL520_REG_IN3_INPUT);
  616. /* Temp1 and Vin4 are the same input */
  617. if (data->two_temps) {
  618. data->temp_input[1] = gl520_read_value(client, GL520_REG_TEMP2_INPUT);
  619. data->temp_max[1] = gl520_read_value(client, GL520_REG_TEMP2_MAX);
  620. data->temp_max_hyst[1] = gl520_read_value(client, GL520_REG_TEMP2_MAX_HYST);
  621. } else {
  622. data->in_input[4] = gl520_read_value(client, GL520_REG_IN4_INPUT);
  623. data->in_min[4] = gl520_read_value(client, GL520_REG_IN4_MIN);
  624. data->in_max[4] = gl520_read_value(client, GL520_REG_IN4_MAX);
  625. }
  626. data->last_updated = jiffies;
  627. data->valid = 1;
  628. }
  629. mutex_unlock(&data->update_lock);
  630. return data;
  631. }
  632. static int __init sensors_gl520sm_init(void)
  633. {
  634. return i2c_add_driver(&gl520_driver);
  635. }
  636. static void __exit sensors_gl520sm_exit(void)
  637. {
  638. i2c_del_driver(&gl520_driver);
  639. }
  640. MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
  641. "Kyösti Mälkki <kmalkki@cc.hut.fi>, "
  642. "Maarten Deprez <maartendeprez@users.sourceforge.net>");
  643. MODULE_DESCRIPTION("GL520SM driver");
  644. MODULE_LICENSE("GPL");
  645. module_init(sensors_gl520sm_init);
  646. module_exit(sensors_gl520sm_exit);