gl520sm.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  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. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. *
  22. */
  23. #include <linux/module.h>
  24. #include <linux/init.h>
  25. #include <linux/slab.h>
  26. #include <linux/jiffies.h>
  27. #include <linux/i2c.h>
  28. #include <linux/hwmon.h>
  29. #include <linux/hwmon-sysfs.h>
  30. #include <linux/hwmon-vid.h>
  31. #include <linux/err.h>
  32. #include <linux/mutex.h>
  33. #include <linux/sysfs.h>
  34. /* Type of the extra sensor */
  35. static unsigned short extra_sensor_type;
  36. module_param(extra_sensor_type, ushort, 0);
  37. MODULE_PARM_DESC(extra_sensor_type, "Type of extra sensor (0=autodetect, 1=temperature, 2=voltage)");
  38. /* Addresses to scan */
  39. static const unsigned short normal_i2c[] = { 0x2c, 0x2d, I2C_CLIENT_END };
  40. /*
  41. * Many GL520 constants specified below
  42. * One of the inputs can be configured as either temp or voltage.
  43. * That's why _TEMP2 and _IN4 access the same register
  44. */
  45. /* The GL520 registers */
  46. #define GL520_REG_CHIP_ID 0x00
  47. #define GL520_REG_REVISION 0x01
  48. #define GL520_REG_CONF 0x03
  49. #define GL520_REG_MASK 0x11
  50. #define GL520_REG_VID_INPUT 0x02
  51. static const u8 GL520_REG_IN_INPUT[] = { 0x15, 0x14, 0x13, 0x0d, 0x0e };
  52. static const u8 GL520_REG_IN_LIMIT[] = { 0x0c, 0x09, 0x0a, 0x0b };
  53. static const u8 GL520_REG_IN_MIN[] = { 0x0c, 0x09, 0x0a, 0x0b, 0x18 };
  54. static const u8 GL520_REG_IN_MAX[] = { 0x0c, 0x09, 0x0a, 0x0b, 0x17 };
  55. static const u8 GL520_REG_TEMP_INPUT[] = { 0x04, 0x0e };
  56. static const u8 GL520_REG_TEMP_MAX[] = { 0x05, 0x17 };
  57. static const u8 GL520_REG_TEMP_MAX_HYST[] = { 0x06, 0x18 };
  58. #define GL520_REG_FAN_INPUT 0x07
  59. #define GL520_REG_FAN_MIN 0x08
  60. #define GL520_REG_FAN_DIV 0x0f
  61. #define GL520_REG_FAN_OFF GL520_REG_FAN_DIV
  62. #define GL520_REG_ALARMS 0x12
  63. #define GL520_REG_BEEP_MASK 0x10
  64. #define GL520_REG_BEEP_ENABLE GL520_REG_CONF
  65. /* Client data */
  66. struct gl520_data {
  67. struct i2c_client *client;
  68. const struct attribute_group *groups[3];
  69. struct mutex update_lock;
  70. char valid; /* zero until the following fields are valid */
  71. unsigned long last_updated; /* in jiffies */
  72. u8 vid;
  73. u8 vrm;
  74. u8 in_input[5]; /* [0] = VVD */
  75. u8 in_min[5]; /* [0] = VDD */
  76. u8 in_max[5]; /* [0] = VDD */
  77. u8 fan_input[2];
  78. u8 fan_min[2];
  79. u8 fan_div[2];
  80. u8 fan_off;
  81. u8 temp_input[2];
  82. u8 temp_max[2];
  83. u8 temp_max_hyst[2];
  84. u8 alarms;
  85. u8 beep_enable;
  86. u8 beep_mask;
  87. u8 alarm_mask;
  88. u8 two_temps;
  89. };
  90. /*
  91. * Registers 0x07 to 0x0c are word-sized, others are byte-sized
  92. * GL520 uses a high-byte first convention
  93. */
  94. static int gl520_read_value(struct i2c_client *client, u8 reg)
  95. {
  96. if ((reg >= 0x07) && (reg <= 0x0c))
  97. return i2c_smbus_read_word_swapped(client, reg);
  98. else
  99. return i2c_smbus_read_byte_data(client, reg);
  100. }
  101. static int gl520_write_value(struct i2c_client *client, u8 reg, u16 value)
  102. {
  103. if ((reg >= 0x07) && (reg <= 0x0c))
  104. return i2c_smbus_write_word_swapped(client, reg, value);
  105. else
  106. return i2c_smbus_write_byte_data(client, reg, value);
  107. }
  108. static struct gl520_data *gl520_update_device(struct device *dev)
  109. {
  110. struct gl520_data *data = dev_get_drvdata(dev);
  111. struct i2c_client *client = data->client;
  112. int val, i;
  113. mutex_lock(&data->update_lock);
  114. if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) {
  115. dev_dbg(&client->dev, "Starting gl520sm update\n");
  116. data->alarms = gl520_read_value(client, GL520_REG_ALARMS);
  117. data->beep_mask = gl520_read_value(client, GL520_REG_BEEP_MASK);
  118. data->vid = gl520_read_value(client,
  119. GL520_REG_VID_INPUT) & 0x1f;
  120. for (i = 0; i < 4; i++) {
  121. data->in_input[i] = gl520_read_value(client,
  122. GL520_REG_IN_INPUT[i]);
  123. val = gl520_read_value(client, GL520_REG_IN_LIMIT[i]);
  124. data->in_min[i] = val & 0xff;
  125. data->in_max[i] = (val >> 8) & 0xff;
  126. }
  127. val = gl520_read_value(client, GL520_REG_FAN_INPUT);
  128. data->fan_input[0] = (val >> 8) & 0xff;
  129. data->fan_input[1] = val & 0xff;
  130. val = gl520_read_value(client, GL520_REG_FAN_MIN);
  131. data->fan_min[0] = (val >> 8) & 0xff;
  132. data->fan_min[1] = val & 0xff;
  133. data->temp_input[0] = gl520_read_value(client,
  134. GL520_REG_TEMP_INPUT[0]);
  135. data->temp_max[0] = gl520_read_value(client,
  136. GL520_REG_TEMP_MAX[0]);
  137. data->temp_max_hyst[0] = gl520_read_value(client,
  138. GL520_REG_TEMP_MAX_HYST[0]);
  139. val = gl520_read_value(client, GL520_REG_FAN_DIV);
  140. data->fan_div[0] = (val >> 6) & 0x03;
  141. data->fan_div[1] = (val >> 4) & 0x03;
  142. data->fan_off = (val >> 2) & 0x01;
  143. data->alarms &= data->alarm_mask;
  144. val = gl520_read_value(client, GL520_REG_CONF);
  145. data->beep_enable = !((val >> 2) & 1);
  146. /* Temp1 and Vin4 are the same input */
  147. if (data->two_temps) {
  148. data->temp_input[1] = gl520_read_value(client,
  149. GL520_REG_TEMP_INPUT[1]);
  150. data->temp_max[1] = gl520_read_value(client,
  151. GL520_REG_TEMP_MAX[1]);
  152. data->temp_max_hyst[1] = gl520_read_value(client,
  153. GL520_REG_TEMP_MAX_HYST[1]);
  154. } else {
  155. data->in_input[4] = gl520_read_value(client,
  156. GL520_REG_IN_INPUT[4]);
  157. data->in_min[4] = gl520_read_value(client,
  158. GL520_REG_IN_MIN[4]);
  159. data->in_max[4] = gl520_read_value(client,
  160. GL520_REG_IN_MAX[4]);
  161. }
  162. data->last_updated = jiffies;
  163. data->valid = 1;
  164. }
  165. mutex_unlock(&data->update_lock);
  166. return data;
  167. }
  168. /*
  169. * Sysfs stuff
  170. */
  171. static ssize_t get_cpu_vid(struct device *dev, struct device_attribute *attr,
  172. char *buf)
  173. {
  174. struct gl520_data *data = gl520_update_device(dev);
  175. return sprintf(buf, "%u\n", vid_from_reg(data->vid, data->vrm));
  176. }
  177. static DEVICE_ATTR(cpu0_vid, S_IRUGO, get_cpu_vid, NULL);
  178. #define VDD_FROM_REG(val) (((val) * 95 + 2) / 4)
  179. #define VDD_TO_REG(val) clamp_val((((val) * 4 + 47) / 95), 0, 255)
  180. #define IN_FROM_REG(val) ((val) * 19)
  181. #define IN_TO_REG(val) clamp_val((((val) + 9) / 19), 0, 255)
  182. static ssize_t get_in_input(struct device *dev, struct device_attribute *attr,
  183. char *buf)
  184. {
  185. int n = to_sensor_dev_attr(attr)->index;
  186. struct gl520_data *data = gl520_update_device(dev);
  187. u8 r = data->in_input[n];
  188. if (n == 0)
  189. return sprintf(buf, "%d\n", VDD_FROM_REG(r));
  190. else
  191. return sprintf(buf, "%d\n", IN_FROM_REG(r));
  192. }
  193. static ssize_t get_in_min(struct device *dev, struct device_attribute *attr,
  194. char *buf)
  195. {
  196. int n = to_sensor_dev_attr(attr)->index;
  197. struct gl520_data *data = gl520_update_device(dev);
  198. u8 r = data->in_min[n];
  199. if (n == 0)
  200. return sprintf(buf, "%d\n", VDD_FROM_REG(r));
  201. else
  202. return sprintf(buf, "%d\n", IN_FROM_REG(r));
  203. }
  204. static ssize_t get_in_max(struct device *dev, struct device_attribute *attr,
  205. char *buf)
  206. {
  207. int n = to_sensor_dev_attr(attr)->index;
  208. struct gl520_data *data = gl520_update_device(dev);
  209. u8 r = data->in_max[n];
  210. if (n == 0)
  211. return sprintf(buf, "%d\n", VDD_FROM_REG(r));
  212. else
  213. return sprintf(buf, "%d\n", IN_FROM_REG(r));
  214. }
  215. static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
  216. const char *buf, size_t count)
  217. {
  218. struct gl520_data *data = dev_get_drvdata(dev);
  219. struct i2c_client *client = data->client;
  220. int n = to_sensor_dev_attr(attr)->index;
  221. u8 r;
  222. long v;
  223. int err;
  224. err = kstrtol(buf, 10, &v);
  225. if (err)
  226. return err;
  227. mutex_lock(&data->update_lock);
  228. if (n == 0)
  229. r = VDD_TO_REG(v);
  230. else
  231. r = IN_TO_REG(v);
  232. data->in_min[n] = r;
  233. if (n < 4)
  234. gl520_write_value(client, GL520_REG_IN_MIN[n],
  235. (gl520_read_value(client, GL520_REG_IN_MIN[n])
  236. & ~0xff) | r);
  237. else
  238. gl520_write_value(client, GL520_REG_IN_MIN[n], r);
  239. mutex_unlock(&data->update_lock);
  240. return count;
  241. }
  242. static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
  243. const char *buf, size_t count)
  244. {
  245. struct gl520_data *data = dev_get_drvdata(dev);
  246. struct i2c_client *client = data->client;
  247. int n = to_sensor_dev_attr(attr)->index;
  248. u8 r;
  249. long v;
  250. int err;
  251. err = kstrtol(buf, 10, &v);
  252. if (err)
  253. return err;
  254. if (n == 0)
  255. r = VDD_TO_REG(v);
  256. else
  257. r = IN_TO_REG(v);
  258. mutex_lock(&data->update_lock);
  259. data->in_max[n] = r;
  260. if (n < 4)
  261. gl520_write_value(client, GL520_REG_IN_MAX[n],
  262. (gl520_read_value(client, GL520_REG_IN_MAX[n])
  263. & ~0xff00) | (r << 8));
  264. else
  265. gl520_write_value(client, GL520_REG_IN_MAX[n], r);
  266. mutex_unlock(&data->update_lock);
  267. return count;
  268. }
  269. static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, get_in_input, NULL, 0);
  270. static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, get_in_input, NULL, 1);
  271. static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, get_in_input, NULL, 2);
  272. static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, get_in_input, NULL, 3);
  273. static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, get_in_input, NULL, 4);
  274. static SENSOR_DEVICE_ATTR(in0_min, S_IRUGO | S_IWUSR,
  275. get_in_min, set_in_min, 0);
  276. static SENSOR_DEVICE_ATTR(in1_min, S_IRUGO | S_IWUSR,
  277. get_in_min, set_in_min, 1);
  278. static SENSOR_DEVICE_ATTR(in2_min, S_IRUGO | S_IWUSR,
  279. get_in_min, set_in_min, 2);
  280. static SENSOR_DEVICE_ATTR(in3_min, S_IRUGO | S_IWUSR,
  281. get_in_min, set_in_min, 3);
  282. static SENSOR_DEVICE_ATTR(in4_min, S_IRUGO | S_IWUSR,
  283. get_in_min, set_in_min, 4);
  284. static SENSOR_DEVICE_ATTR(in0_max, S_IRUGO | S_IWUSR,
  285. get_in_max, set_in_max, 0);
  286. static SENSOR_DEVICE_ATTR(in1_max, S_IRUGO | S_IWUSR,
  287. get_in_max, set_in_max, 1);
  288. static SENSOR_DEVICE_ATTR(in2_max, S_IRUGO | S_IWUSR,
  289. get_in_max, set_in_max, 2);
  290. static SENSOR_DEVICE_ATTR(in3_max, S_IRUGO | S_IWUSR,
  291. get_in_max, set_in_max, 3);
  292. static SENSOR_DEVICE_ATTR(in4_max, S_IRUGO | S_IWUSR,
  293. get_in_max, set_in_max, 4);
  294. #define DIV_FROM_REG(val) (1 << (val))
  295. #define FAN_FROM_REG(val, div) ((val) == 0 ? 0 : (480000 / ((val) << (div))))
  296. #define FAN_TO_REG(val, div) ((val) <= 0 ? 0 : \
  297. clamp_val((480000 + ((val) << ((div)-1))) / ((val) << (div)), 1, 255))
  298. static ssize_t get_fan_input(struct device *dev, struct device_attribute *attr,
  299. char *buf)
  300. {
  301. int n = to_sensor_dev_attr(attr)->index;
  302. struct gl520_data *data = gl520_update_device(dev);
  303. return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_input[n],
  304. data->fan_div[n]));
  305. }
  306. static ssize_t get_fan_min(struct device *dev, struct device_attribute *attr,
  307. char *buf)
  308. {
  309. int n = to_sensor_dev_attr(attr)->index;
  310. struct gl520_data *data = gl520_update_device(dev);
  311. return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[n],
  312. data->fan_div[n]));
  313. }
  314. static ssize_t get_fan_div(struct device *dev, struct device_attribute *attr,
  315. char *buf)
  316. {
  317. int n = to_sensor_dev_attr(attr)->index;
  318. struct gl520_data *data = gl520_update_device(dev);
  319. return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[n]));
  320. }
  321. static ssize_t get_fan_off(struct device *dev, struct device_attribute *attr,
  322. char *buf)
  323. {
  324. struct gl520_data *data = gl520_update_device(dev);
  325. return sprintf(buf, "%d\n", data->fan_off);
  326. }
  327. static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
  328. const char *buf, size_t count)
  329. {
  330. struct gl520_data *data = dev_get_drvdata(dev);
  331. struct i2c_client *client = data->client;
  332. int n = to_sensor_dev_attr(attr)->index;
  333. u8 r;
  334. unsigned long v;
  335. int err;
  336. err = kstrtoul(buf, 10, &v);
  337. if (err)
  338. return err;
  339. mutex_lock(&data->update_lock);
  340. r = FAN_TO_REG(v, data->fan_div[n]);
  341. data->fan_min[n] = r;
  342. if (n == 0)
  343. gl520_write_value(client, GL520_REG_FAN_MIN,
  344. (gl520_read_value(client, GL520_REG_FAN_MIN)
  345. & ~0xff00) | (r << 8));
  346. else
  347. gl520_write_value(client, GL520_REG_FAN_MIN,
  348. (gl520_read_value(client, GL520_REG_FAN_MIN)
  349. & ~0xff) | r);
  350. data->beep_mask = gl520_read_value(client, GL520_REG_BEEP_MASK);
  351. if (data->fan_min[n] == 0)
  352. data->alarm_mask &= (n == 0) ? ~0x20 : ~0x40;
  353. else
  354. data->alarm_mask |= (n == 0) ? 0x20 : 0x40;
  355. data->beep_mask &= data->alarm_mask;
  356. gl520_write_value(client, GL520_REG_BEEP_MASK, data->beep_mask);
  357. mutex_unlock(&data->update_lock);
  358. return count;
  359. }
  360. static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
  361. const char *buf, size_t count)
  362. {
  363. struct gl520_data *data = dev_get_drvdata(dev);
  364. struct i2c_client *client = data->client;
  365. int n = to_sensor_dev_attr(attr)->index;
  366. u8 r;
  367. unsigned long v;
  368. int err;
  369. err = kstrtoul(buf, 10, &v);
  370. if (err)
  371. return err;
  372. switch (v) {
  373. case 1:
  374. r = 0;
  375. break;
  376. case 2:
  377. r = 1;
  378. break;
  379. case 4:
  380. r = 2;
  381. break;
  382. case 8:
  383. r = 3;
  384. break;
  385. default:
  386. dev_err(&client->dev,
  387. "fan_div value %ld not supported. Choose one of 1, 2, 4 or 8!\n", v);
  388. return -EINVAL;
  389. }
  390. mutex_lock(&data->update_lock);
  391. data->fan_div[n] = r;
  392. if (n == 0)
  393. gl520_write_value(client, GL520_REG_FAN_DIV,
  394. (gl520_read_value(client, GL520_REG_FAN_DIV)
  395. & ~0xc0) | (r << 6));
  396. else
  397. gl520_write_value(client, GL520_REG_FAN_DIV,
  398. (gl520_read_value(client, GL520_REG_FAN_DIV)
  399. & ~0x30) | (r << 4));
  400. mutex_unlock(&data->update_lock);
  401. return count;
  402. }
  403. static ssize_t set_fan_off(struct device *dev, struct device_attribute *attr,
  404. const char *buf, size_t count)
  405. {
  406. struct gl520_data *data = dev_get_drvdata(dev);
  407. struct i2c_client *client = data->client;
  408. u8 r;
  409. unsigned long v;
  410. int err;
  411. err = kstrtoul(buf, 10, &v);
  412. if (err)
  413. return err;
  414. r = (v ? 1 : 0);
  415. mutex_lock(&data->update_lock);
  416. data->fan_off = r;
  417. gl520_write_value(client, GL520_REG_FAN_OFF,
  418. (gl520_read_value(client, GL520_REG_FAN_OFF)
  419. & ~0x0c) | (r << 2));
  420. mutex_unlock(&data->update_lock);
  421. return count;
  422. }
  423. static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, get_fan_input, NULL, 0);
  424. static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, get_fan_input, NULL, 1);
  425. static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO | S_IWUSR,
  426. get_fan_min, set_fan_min, 0);
  427. static SENSOR_DEVICE_ATTR(fan2_min, S_IRUGO | S_IWUSR,
  428. get_fan_min, set_fan_min, 1);
  429. static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR,
  430. get_fan_div, set_fan_div, 0);
  431. static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR,
  432. get_fan_div, set_fan_div, 1);
  433. static DEVICE_ATTR(fan1_off, S_IRUGO | S_IWUSR,
  434. get_fan_off, set_fan_off);
  435. #define TEMP_FROM_REG(val) (((val) - 130) * 1000)
  436. #define TEMP_TO_REG(val) clamp_val(((((val) < 0 ? \
  437. (val) - 500 : (val) + 500) / 1000) + 130), 0, 255)
  438. static ssize_t get_temp_input(struct device *dev, struct device_attribute *attr,
  439. char *buf)
  440. {
  441. int n = to_sensor_dev_attr(attr)->index;
  442. struct gl520_data *data = gl520_update_device(dev);
  443. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_input[n]));
  444. }
  445. static ssize_t get_temp_max(struct device *dev, struct device_attribute *attr,
  446. char *buf)
  447. {
  448. int n = to_sensor_dev_attr(attr)->index;
  449. struct gl520_data *data = gl520_update_device(dev);
  450. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[n]));
  451. }
  452. static ssize_t get_temp_max_hyst(struct device *dev,
  453. struct device_attribute *attr, char *buf)
  454. {
  455. int n = to_sensor_dev_attr(attr)->index;
  456. struct gl520_data *data = gl520_update_device(dev);
  457. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max_hyst[n]));
  458. }
  459. static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
  460. const char *buf, size_t count)
  461. {
  462. struct gl520_data *data = dev_get_drvdata(dev);
  463. struct i2c_client *client = data->client;
  464. int n = to_sensor_dev_attr(attr)->index;
  465. long v;
  466. int err;
  467. err = kstrtol(buf, 10, &v);
  468. if (err)
  469. return err;
  470. mutex_lock(&data->update_lock);
  471. data->temp_max[n] = TEMP_TO_REG(v);
  472. gl520_write_value(client, GL520_REG_TEMP_MAX[n], data->temp_max[n]);
  473. mutex_unlock(&data->update_lock);
  474. return count;
  475. }
  476. static ssize_t set_temp_max_hyst(struct device *dev, struct device_attribute
  477. *attr, const char *buf, size_t count)
  478. {
  479. struct gl520_data *data = dev_get_drvdata(dev);
  480. struct i2c_client *client = data->client;
  481. int n = to_sensor_dev_attr(attr)->index;
  482. long v;
  483. int err;
  484. err = kstrtol(buf, 10, &v);
  485. if (err)
  486. return err;
  487. mutex_lock(&data->update_lock);
  488. data->temp_max_hyst[n] = TEMP_TO_REG(v);
  489. gl520_write_value(client, GL520_REG_TEMP_MAX_HYST[n],
  490. data->temp_max_hyst[n]);
  491. mutex_unlock(&data->update_lock);
  492. return count;
  493. }
  494. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, get_temp_input, NULL, 0);
  495. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, get_temp_input, NULL, 1);
  496. static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR,
  497. get_temp_max, set_temp_max, 0);
  498. static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO | S_IWUSR,
  499. get_temp_max, set_temp_max, 1);
  500. static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR,
  501. get_temp_max_hyst, set_temp_max_hyst, 0);
  502. static SENSOR_DEVICE_ATTR(temp2_max_hyst, S_IRUGO | S_IWUSR,
  503. get_temp_max_hyst, set_temp_max_hyst, 1);
  504. static ssize_t get_alarms(struct device *dev, struct device_attribute *attr,
  505. char *buf)
  506. {
  507. struct gl520_data *data = gl520_update_device(dev);
  508. return sprintf(buf, "%d\n", data->alarms);
  509. }
  510. static ssize_t get_beep_enable(struct device *dev, struct device_attribute
  511. *attr, char *buf)
  512. {
  513. struct gl520_data *data = gl520_update_device(dev);
  514. return sprintf(buf, "%d\n", data->beep_enable);
  515. }
  516. static ssize_t get_beep_mask(struct device *dev, struct device_attribute *attr,
  517. char *buf)
  518. {
  519. struct gl520_data *data = gl520_update_device(dev);
  520. return sprintf(buf, "%d\n", data->beep_mask);
  521. }
  522. static ssize_t set_beep_enable(struct device *dev, struct device_attribute
  523. *attr, const char *buf, size_t count)
  524. {
  525. struct gl520_data *data = dev_get_drvdata(dev);
  526. struct i2c_client *client = data->client;
  527. u8 r;
  528. unsigned long v;
  529. int err;
  530. err = kstrtoul(buf, 10, &v);
  531. if (err)
  532. return err;
  533. r = (v ? 0 : 1);
  534. mutex_lock(&data->update_lock);
  535. data->beep_enable = !r;
  536. gl520_write_value(client, GL520_REG_BEEP_ENABLE,
  537. (gl520_read_value(client, GL520_REG_BEEP_ENABLE)
  538. & ~0x04) | (r << 2));
  539. mutex_unlock(&data->update_lock);
  540. return count;
  541. }
  542. static ssize_t set_beep_mask(struct device *dev, struct device_attribute *attr,
  543. const char *buf, size_t count)
  544. {
  545. struct gl520_data *data = dev_get_drvdata(dev);
  546. struct i2c_client *client = data->client;
  547. unsigned long r;
  548. int err;
  549. err = kstrtoul(buf, 10, &r);
  550. if (err)
  551. return err;
  552. mutex_lock(&data->update_lock);
  553. r &= data->alarm_mask;
  554. data->beep_mask = r;
  555. gl520_write_value(client, GL520_REG_BEEP_MASK, r);
  556. mutex_unlock(&data->update_lock);
  557. return count;
  558. }
  559. static DEVICE_ATTR(alarms, S_IRUGO, get_alarms, NULL);
  560. static DEVICE_ATTR(beep_enable, S_IRUGO | S_IWUSR,
  561. get_beep_enable, set_beep_enable);
  562. static DEVICE_ATTR(beep_mask, S_IRUGO | S_IWUSR,
  563. get_beep_mask, set_beep_mask);
  564. static ssize_t get_alarm(struct device *dev, struct device_attribute *attr,
  565. char *buf)
  566. {
  567. int bit_nr = to_sensor_dev_attr(attr)->index;
  568. struct gl520_data *data = gl520_update_device(dev);
  569. return sprintf(buf, "%d\n", (data->alarms >> bit_nr) & 1);
  570. }
  571. static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, get_alarm, NULL, 0);
  572. static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, get_alarm, NULL, 1);
  573. static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, get_alarm, NULL, 2);
  574. static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, get_alarm, NULL, 3);
  575. static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, get_alarm, NULL, 4);
  576. static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, get_alarm, NULL, 5);
  577. static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, get_alarm, NULL, 6);
  578. static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, get_alarm, NULL, 7);
  579. static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, get_alarm, NULL, 7);
  580. static ssize_t get_beep(struct device *dev, struct device_attribute *attr,
  581. char *buf)
  582. {
  583. int bitnr = to_sensor_dev_attr(attr)->index;
  584. struct gl520_data *data = gl520_update_device(dev);
  585. return sprintf(buf, "%d\n", (data->beep_mask >> bitnr) & 1);
  586. }
  587. static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
  588. const char *buf, size_t count)
  589. {
  590. struct gl520_data *data = dev_get_drvdata(dev);
  591. struct i2c_client *client = data->client;
  592. int bitnr = to_sensor_dev_attr(attr)->index;
  593. unsigned long bit;
  594. int err;
  595. err = kstrtoul(buf, 10, &bit);
  596. if (err)
  597. return err;
  598. if (bit & ~1)
  599. return -EINVAL;
  600. mutex_lock(&data->update_lock);
  601. data->beep_mask = gl520_read_value(client, GL520_REG_BEEP_MASK);
  602. if (bit)
  603. data->beep_mask |= (1 << bitnr);
  604. else
  605. data->beep_mask &= ~(1 << bitnr);
  606. gl520_write_value(client, GL520_REG_BEEP_MASK, data->beep_mask);
  607. mutex_unlock(&data->update_lock);
  608. return count;
  609. }
  610. static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR, get_beep, set_beep, 0);
  611. static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO | S_IWUSR, get_beep, set_beep, 1);
  612. static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO | S_IWUSR, get_beep, set_beep, 2);
  613. static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO | S_IWUSR, get_beep, set_beep, 3);
  614. static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR, get_beep, set_beep, 4);
  615. static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO | S_IWUSR, get_beep, set_beep, 5);
  616. static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO | S_IWUSR, get_beep, set_beep, 6);
  617. static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO | S_IWUSR, get_beep, set_beep, 7);
  618. static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO | S_IWUSR, get_beep, set_beep, 7);
  619. static struct attribute *gl520_attributes[] = {
  620. &dev_attr_cpu0_vid.attr,
  621. &sensor_dev_attr_in0_input.dev_attr.attr,
  622. &sensor_dev_attr_in0_min.dev_attr.attr,
  623. &sensor_dev_attr_in0_max.dev_attr.attr,
  624. &sensor_dev_attr_in0_alarm.dev_attr.attr,
  625. &sensor_dev_attr_in0_beep.dev_attr.attr,
  626. &sensor_dev_attr_in1_input.dev_attr.attr,
  627. &sensor_dev_attr_in1_min.dev_attr.attr,
  628. &sensor_dev_attr_in1_max.dev_attr.attr,
  629. &sensor_dev_attr_in1_alarm.dev_attr.attr,
  630. &sensor_dev_attr_in1_beep.dev_attr.attr,
  631. &sensor_dev_attr_in2_input.dev_attr.attr,
  632. &sensor_dev_attr_in2_min.dev_attr.attr,
  633. &sensor_dev_attr_in2_max.dev_attr.attr,
  634. &sensor_dev_attr_in2_alarm.dev_attr.attr,
  635. &sensor_dev_attr_in2_beep.dev_attr.attr,
  636. &sensor_dev_attr_in3_input.dev_attr.attr,
  637. &sensor_dev_attr_in3_min.dev_attr.attr,
  638. &sensor_dev_attr_in3_max.dev_attr.attr,
  639. &sensor_dev_attr_in3_alarm.dev_attr.attr,
  640. &sensor_dev_attr_in3_beep.dev_attr.attr,
  641. &sensor_dev_attr_fan1_input.dev_attr.attr,
  642. &sensor_dev_attr_fan1_min.dev_attr.attr,
  643. &sensor_dev_attr_fan1_div.dev_attr.attr,
  644. &sensor_dev_attr_fan1_alarm.dev_attr.attr,
  645. &sensor_dev_attr_fan1_beep.dev_attr.attr,
  646. &dev_attr_fan1_off.attr,
  647. &sensor_dev_attr_fan2_input.dev_attr.attr,
  648. &sensor_dev_attr_fan2_min.dev_attr.attr,
  649. &sensor_dev_attr_fan2_div.dev_attr.attr,
  650. &sensor_dev_attr_fan2_alarm.dev_attr.attr,
  651. &sensor_dev_attr_fan2_beep.dev_attr.attr,
  652. &sensor_dev_attr_temp1_input.dev_attr.attr,
  653. &sensor_dev_attr_temp1_max.dev_attr.attr,
  654. &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
  655. &sensor_dev_attr_temp1_alarm.dev_attr.attr,
  656. &sensor_dev_attr_temp1_beep.dev_attr.attr,
  657. &dev_attr_alarms.attr,
  658. &dev_attr_beep_enable.attr,
  659. &dev_attr_beep_mask.attr,
  660. NULL
  661. };
  662. static const struct attribute_group gl520_group = {
  663. .attrs = gl520_attributes,
  664. };
  665. static struct attribute *gl520_attributes_in4[] = {
  666. &sensor_dev_attr_in4_input.dev_attr.attr,
  667. &sensor_dev_attr_in4_min.dev_attr.attr,
  668. &sensor_dev_attr_in4_max.dev_attr.attr,
  669. &sensor_dev_attr_in4_alarm.dev_attr.attr,
  670. &sensor_dev_attr_in4_beep.dev_attr.attr,
  671. NULL
  672. };
  673. static struct attribute *gl520_attributes_temp2[] = {
  674. &sensor_dev_attr_temp2_input.dev_attr.attr,
  675. &sensor_dev_attr_temp2_max.dev_attr.attr,
  676. &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
  677. &sensor_dev_attr_temp2_alarm.dev_attr.attr,
  678. &sensor_dev_attr_temp2_beep.dev_attr.attr,
  679. NULL
  680. };
  681. static const struct attribute_group gl520_group_in4 = {
  682. .attrs = gl520_attributes_in4,
  683. };
  684. static const struct attribute_group gl520_group_temp2 = {
  685. .attrs = gl520_attributes_temp2,
  686. };
  687. /*
  688. * Real code
  689. */
  690. /* Return 0 if detection is successful, -ENODEV otherwise */
  691. static int gl520_detect(struct i2c_client *client, struct i2c_board_info *info)
  692. {
  693. struct i2c_adapter *adapter = client->adapter;
  694. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
  695. I2C_FUNC_SMBUS_WORD_DATA))
  696. return -ENODEV;
  697. /* Determine the chip type. */
  698. if ((gl520_read_value(client, GL520_REG_CHIP_ID) != 0x20) ||
  699. ((gl520_read_value(client, GL520_REG_REVISION) & 0x7f) != 0x00) ||
  700. ((gl520_read_value(client, GL520_REG_CONF) & 0x80) != 0x00)) {
  701. dev_dbg(&client->dev, "Unknown chip type, skipping\n");
  702. return -ENODEV;
  703. }
  704. strlcpy(info->type, "gl520sm", I2C_NAME_SIZE);
  705. return 0;
  706. }
  707. /* Called when we have found a new GL520SM. */
  708. static void gl520_init_client(struct i2c_client *client)
  709. {
  710. struct gl520_data *data = i2c_get_clientdata(client);
  711. u8 oldconf, conf;
  712. conf = oldconf = gl520_read_value(client, GL520_REG_CONF);
  713. data->alarm_mask = 0xff;
  714. data->vrm = vid_which_vrm();
  715. if (extra_sensor_type == 1)
  716. conf &= ~0x10;
  717. else if (extra_sensor_type == 2)
  718. conf |= 0x10;
  719. data->two_temps = !(conf & 0x10);
  720. /* If IRQ# is disabled, we can safely force comparator mode */
  721. if (!(conf & 0x20))
  722. conf &= 0xf7;
  723. /* Enable monitoring if needed */
  724. conf |= 0x40;
  725. if (conf != oldconf)
  726. gl520_write_value(client, GL520_REG_CONF, conf);
  727. gl520_update_device(&(client->dev));
  728. if (data->fan_min[0] == 0)
  729. data->alarm_mask &= ~0x20;
  730. if (data->fan_min[1] == 0)
  731. data->alarm_mask &= ~0x40;
  732. data->beep_mask &= data->alarm_mask;
  733. gl520_write_value(client, GL520_REG_BEEP_MASK, data->beep_mask);
  734. }
  735. static int gl520_probe(struct i2c_client *client,
  736. const struct i2c_device_id *id)
  737. {
  738. struct device *dev = &client->dev;
  739. struct device *hwmon_dev;
  740. struct gl520_data *data;
  741. data = devm_kzalloc(dev, sizeof(struct gl520_data), GFP_KERNEL);
  742. if (!data)
  743. return -ENOMEM;
  744. i2c_set_clientdata(client, data);
  745. mutex_init(&data->update_lock);
  746. data->client = client;
  747. /* Initialize the GL520SM chip */
  748. gl520_init_client(client);
  749. /* sysfs hooks */
  750. data->groups[0] = &gl520_group;
  751. if (data->two_temps)
  752. data->groups[1] = &gl520_group_temp2;
  753. else
  754. data->groups[1] = &gl520_group_in4;
  755. hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
  756. data, data->groups);
  757. return PTR_ERR_OR_ZERO(hwmon_dev);
  758. }
  759. static const struct i2c_device_id gl520_id[] = {
  760. { "gl520sm", 0 },
  761. { }
  762. };
  763. MODULE_DEVICE_TABLE(i2c, gl520_id);
  764. static struct i2c_driver gl520_driver = {
  765. .class = I2C_CLASS_HWMON,
  766. .driver = {
  767. .name = "gl520sm",
  768. },
  769. .probe = gl520_probe,
  770. .id_table = gl520_id,
  771. .detect = gl520_detect,
  772. .address_list = normal_i2c,
  773. };
  774. module_i2c_driver(gl520_driver);
  775. MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
  776. "Kyösti Mälkki <kmalkki@cc.hut.fi>, "
  777. "Maarten Deprez <maartendeprez@users.sourceforge.net>");
  778. MODULE_DESCRIPTION("GL520SM driver");
  779. MODULE_LICENSE("GPL");