gl520sm.c 22 KB

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