gl520sm.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  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) DIV_ROUND_CLOSEST((val) * 95, 4)
  179. #define VDD_CLAMP(val) clamp_val(val, 0, 255 * 95 / 4)
  180. #define VDD_TO_REG(val) DIV_ROUND_CLOSEST(VDD_CLAMP(val) * 4, 95)
  181. #define IN_FROM_REG(val) ((val) * 19)
  182. #define IN_CLAMP(val) clamp_val(val, 0, 255 * 19)
  183. #define IN_TO_REG(val) DIV_ROUND_CLOSEST(IN_CLAMP(val), 19)
  184. static ssize_t get_in_input(struct device *dev, struct device_attribute *attr,
  185. char *buf)
  186. {
  187. int n = to_sensor_dev_attr(attr)->index;
  188. struct gl520_data *data = gl520_update_device(dev);
  189. u8 r = data->in_input[n];
  190. if (n == 0)
  191. return sprintf(buf, "%d\n", VDD_FROM_REG(r));
  192. else
  193. return sprintf(buf, "%d\n", IN_FROM_REG(r));
  194. }
  195. static ssize_t get_in_min(struct device *dev, struct device_attribute *attr,
  196. char *buf)
  197. {
  198. int n = to_sensor_dev_attr(attr)->index;
  199. struct gl520_data *data = gl520_update_device(dev);
  200. u8 r = data->in_min[n];
  201. if (n == 0)
  202. return sprintf(buf, "%d\n", VDD_FROM_REG(r));
  203. else
  204. return sprintf(buf, "%d\n", IN_FROM_REG(r));
  205. }
  206. static ssize_t get_in_max(struct device *dev, struct device_attribute *attr,
  207. char *buf)
  208. {
  209. int n = to_sensor_dev_attr(attr)->index;
  210. struct gl520_data *data = gl520_update_device(dev);
  211. u8 r = data->in_max[n];
  212. if (n == 0)
  213. return sprintf(buf, "%d\n", VDD_FROM_REG(r));
  214. else
  215. return sprintf(buf, "%d\n", IN_FROM_REG(r));
  216. }
  217. static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
  218. const char *buf, size_t count)
  219. {
  220. struct gl520_data *data = dev_get_drvdata(dev);
  221. struct i2c_client *client = data->client;
  222. int n = to_sensor_dev_attr(attr)->index;
  223. u8 r;
  224. long v;
  225. int err;
  226. err = kstrtol(buf, 10, &v);
  227. if (err)
  228. return err;
  229. mutex_lock(&data->update_lock);
  230. if (n == 0)
  231. r = VDD_TO_REG(v);
  232. else
  233. r = IN_TO_REG(v);
  234. data->in_min[n] = r;
  235. if (n < 4)
  236. gl520_write_value(client, GL520_REG_IN_MIN[n],
  237. (gl520_read_value(client, GL520_REG_IN_MIN[n])
  238. & ~0xff) | r);
  239. else
  240. gl520_write_value(client, GL520_REG_IN_MIN[n], r);
  241. mutex_unlock(&data->update_lock);
  242. return count;
  243. }
  244. static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
  245. const char *buf, size_t count)
  246. {
  247. struct gl520_data *data = dev_get_drvdata(dev);
  248. struct i2c_client *client = data->client;
  249. int n = to_sensor_dev_attr(attr)->index;
  250. u8 r;
  251. long v;
  252. int err;
  253. err = kstrtol(buf, 10, &v);
  254. if (err)
  255. return err;
  256. if (n == 0)
  257. r = VDD_TO_REG(v);
  258. else
  259. r = IN_TO_REG(v);
  260. mutex_lock(&data->update_lock);
  261. data->in_max[n] = r;
  262. if (n < 4)
  263. gl520_write_value(client, GL520_REG_IN_MAX[n],
  264. (gl520_read_value(client, GL520_REG_IN_MAX[n])
  265. & ~0xff00) | (r << 8));
  266. else
  267. gl520_write_value(client, GL520_REG_IN_MAX[n], r);
  268. mutex_unlock(&data->update_lock);
  269. return count;
  270. }
  271. static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, get_in_input, NULL, 0);
  272. static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, get_in_input, NULL, 1);
  273. static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, get_in_input, NULL, 2);
  274. static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, get_in_input, NULL, 3);
  275. static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, get_in_input, NULL, 4);
  276. static SENSOR_DEVICE_ATTR(in0_min, S_IRUGO | S_IWUSR,
  277. get_in_min, set_in_min, 0);
  278. static SENSOR_DEVICE_ATTR(in1_min, S_IRUGO | S_IWUSR,
  279. get_in_min, set_in_min, 1);
  280. static SENSOR_DEVICE_ATTR(in2_min, S_IRUGO | S_IWUSR,
  281. get_in_min, set_in_min, 2);
  282. static SENSOR_DEVICE_ATTR(in3_min, S_IRUGO | S_IWUSR,
  283. get_in_min, set_in_min, 3);
  284. static SENSOR_DEVICE_ATTR(in4_min, S_IRUGO | S_IWUSR,
  285. get_in_min, set_in_min, 4);
  286. static SENSOR_DEVICE_ATTR(in0_max, S_IRUGO | S_IWUSR,
  287. get_in_max, set_in_max, 0);
  288. static SENSOR_DEVICE_ATTR(in1_max, S_IRUGO | S_IWUSR,
  289. get_in_max, set_in_max, 1);
  290. static SENSOR_DEVICE_ATTR(in2_max, S_IRUGO | S_IWUSR,
  291. get_in_max, set_in_max, 2);
  292. static SENSOR_DEVICE_ATTR(in3_max, S_IRUGO | S_IWUSR,
  293. get_in_max, set_in_max, 3);
  294. static SENSOR_DEVICE_ATTR(in4_max, S_IRUGO | S_IWUSR,
  295. get_in_max, set_in_max, 4);
  296. #define DIV_FROM_REG(val) (1 << (val))
  297. #define FAN_FROM_REG(val, div) ((val) == 0 ? 0 : (480000 / ((val) << (div))))
  298. #define FAN_BASE(div) (480000 >> (div))
  299. #define FAN_CLAMP(val, div) clamp_val(val, FAN_BASE(div) / 255, \
  300. FAN_BASE(div))
  301. #define FAN_TO_REG(val, div) ((val) == 0 ? 0 : \
  302. DIV_ROUND_CLOSEST(480000, \
  303. FAN_CLAMP(val, div) << (div)))
  304. static ssize_t get_fan_input(struct device *dev, struct device_attribute *attr,
  305. char *buf)
  306. {
  307. int n = to_sensor_dev_attr(attr)->index;
  308. struct gl520_data *data = gl520_update_device(dev);
  309. return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_input[n],
  310. data->fan_div[n]));
  311. }
  312. static ssize_t get_fan_min(struct device *dev, struct device_attribute *attr,
  313. char *buf)
  314. {
  315. int n = to_sensor_dev_attr(attr)->index;
  316. struct gl520_data *data = gl520_update_device(dev);
  317. return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[n],
  318. data->fan_div[n]));
  319. }
  320. static ssize_t get_fan_div(struct device *dev, struct device_attribute *attr,
  321. char *buf)
  322. {
  323. int n = to_sensor_dev_attr(attr)->index;
  324. struct gl520_data *data = gl520_update_device(dev);
  325. return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[n]));
  326. }
  327. static ssize_t get_fan_off(struct device *dev, struct device_attribute *attr,
  328. char *buf)
  329. {
  330. struct gl520_data *data = gl520_update_device(dev);
  331. return sprintf(buf, "%d\n", data->fan_off);
  332. }
  333. static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
  334. const char *buf, size_t count)
  335. {
  336. struct gl520_data *data = dev_get_drvdata(dev);
  337. struct i2c_client *client = data->client;
  338. int n = to_sensor_dev_attr(attr)->index;
  339. u8 r;
  340. unsigned long v;
  341. int err;
  342. err = kstrtoul(buf, 10, &v);
  343. if (err)
  344. return err;
  345. mutex_lock(&data->update_lock);
  346. r = FAN_TO_REG(v, data->fan_div[n]);
  347. data->fan_min[n] = r;
  348. if (n == 0)
  349. gl520_write_value(client, GL520_REG_FAN_MIN,
  350. (gl520_read_value(client, GL520_REG_FAN_MIN)
  351. & ~0xff00) | (r << 8));
  352. else
  353. gl520_write_value(client, GL520_REG_FAN_MIN,
  354. (gl520_read_value(client, GL520_REG_FAN_MIN)
  355. & ~0xff) | r);
  356. data->beep_mask = gl520_read_value(client, GL520_REG_BEEP_MASK);
  357. if (data->fan_min[n] == 0)
  358. data->alarm_mask &= (n == 0) ? ~0x20 : ~0x40;
  359. else
  360. data->alarm_mask |= (n == 0) ? 0x20 : 0x40;
  361. data->beep_mask &= data->alarm_mask;
  362. gl520_write_value(client, GL520_REG_BEEP_MASK, data->beep_mask);
  363. mutex_unlock(&data->update_lock);
  364. return count;
  365. }
  366. static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
  367. const char *buf, size_t count)
  368. {
  369. struct gl520_data *data = dev_get_drvdata(dev);
  370. struct i2c_client *client = data->client;
  371. int n = to_sensor_dev_attr(attr)->index;
  372. u8 r;
  373. unsigned long v;
  374. int err;
  375. err = kstrtoul(buf, 10, &v);
  376. if (err)
  377. return err;
  378. switch (v) {
  379. case 1:
  380. r = 0;
  381. break;
  382. case 2:
  383. r = 1;
  384. break;
  385. case 4:
  386. r = 2;
  387. break;
  388. case 8:
  389. r = 3;
  390. break;
  391. default:
  392. dev_err(&client->dev,
  393. "fan_div value %ld not supported. Choose one of 1, 2, 4 or 8!\n", v);
  394. return -EINVAL;
  395. }
  396. mutex_lock(&data->update_lock);
  397. data->fan_div[n] = r;
  398. if (n == 0)
  399. gl520_write_value(client, GL520_REG_FAN_DIV,
  400. (gl520_read_value(client, GL520_REG_FAN_DIV)
  401. & ~0xc0) | (r << 6));
  402. else
  403. gl520_write_value(client, GL520_REG_FAN_DIV,
  404. (gl520_read_value(client, GL520_REG_FAN_DIV)
  405. & ~0x30) | (r << 4));
  406. mutex_unlock(&data->update_lock);
  407. return count;
  408. }
  409. static ssize_t set_fan_off(struct device *dev, struct device_attribute *attr,
  410. const char *buf, size_t count)
  411. {
  412. struct gl520_data *data = dev_get_drvdata(dev);
  413. struct i2c_client *client = data->client;
  414. u8 r;
  415. unsigned long v;
  416. int err;
  417. err = kstrtoul(buf, 10, &v);
  418. if (err)
  419. return err;
  420. r = (v ? 1 : 0);
  421. mutex_lock(&data->update_lock);
  422. data->fan_off = r;
  423. gl520_write_value(client, GL520_REG_FAN_OFF,
  424. (gl520_read_value(client, GL520_REG_FAN_OFF)
  425. & ~0x0c) | (r << 2));
  426. mutex_unlock(&data->update_lock);
  427. return count;
  428. }
  429. static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, get_fan_input, NULL, 0);
  430. static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, get_fan_input, NULL, 1);
  431. static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO | S_IWUSR,
  432. get_fan_min, set_fan_min, 0);
  433. static SENSOR_DEVICE_ATTR(fan2_min, S_IRUGO | S_IWUSR,
  434. get_fan_min, set_fan_min, 1);
  435. static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR,
  436. get_fan_div, set_fan_div, 0);
  437. static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR,
  438. get_fan_div, set_fan_div, 1);
  439. static DEVICE_ATTR(fan1_off, S_IRUGO | S_IWUSR,
  440. get_fan_off, set_fan_off);
  441. #define TEMP_FROM_REG(val) (((val) - 130) * 1000)
  442. #define TEMP_CLAMP(val) clamp_val(val, -130000, 125000)
  443. #define TEMP_TO_REG(val) (DIV_ROUND_CLOSEST(TEMP_CLAMP(val), 1000) + 130)
  444. static ssize_t get_temp_input(struct device *dev, struct device_attribute *attr,
  445. char *buf)
  446. {
  447. int n = to_sensor_dev_attr(attr)->index;
  448. struct gl520_data *data = gl520_update_device(dev);
  449. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_input[n]));
  450. }
  451. static ssize_t get_temp_max(struct device *dev, struct device_attribute *attr,
  452. char *buf)
  453. {
  454. int n = to_sensor_dev_attr(attr)->index;
  455. struct gl520_data *data = gl520_update_device(dev);
  456. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[n]));
  457. }
  458. static ssize_t get_temp_max_hyst(struct device *dev,
  459. struct device_attribute *attr, char *buf)
  460. {
  461. int n = to_sensor_dev_attr(attr)->index;
  462. struct gl520_data *data = gl520_update_device(dev);
  463. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max_hyst[n]));
  464. }
  465. static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
  466. const char *buf, size_t count)
  467. {
  468. struct gl520_data *data = dev_get_drvdata(dev);
  469. struct i2c_client *client = data->client;
  470. int n = to_sensor_dev_attr(attr)->index;
  471. long v;
  472. int err;
  473. err = kstrtol(buf, 10, &v);
  474. if (err)
  475. return err;
  476. mutex_lock(&data->update_lock);
  477. data->temp_max[n] = TEMP_TO_REG(v);
  478. gl520_write_value(client, GL520_REG_TEMP_MAX[n], data->temp_max[n]);
  479. mutex_unlock(&data->update_lock);
  480. return count;
  481. }
  482. static ssize_t set_temp_max_hyst(struct device *dev, struct device_attribute
  483. *attr, const char *buf, size_t count)
  484. {
  485. struct gl520_data *data = dev_get_drvdata(dev);
  486. struct i2c_client *client = data->client;
  487. int n = to_sensor_dev_attr(attr)->index;
  488. long v;
  489. int err;
  490. err = kstrtol(buf, 10, &v);
  491. if (err)
  492. return err;
  493. mutex_lock(&data->update_lock);
  494. data->temp_max_hyst[n] = TEMP_TO_REG(v);
  495. gl520_write_value(client, GL520_REG_TEMP_MAX_HYST[n],
  496. data->temp_max_hyst[n]);
  497. mutex_unlock(&data->update_lock);
  498. return count;
  499. }
  500. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, get_temp_input, NULL, 0);
  501. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, get_temp_input, NULL, 1);
  502. static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR,
  503. get_temp_max, set_temp_max, 0);
  504. static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO | S_IWUSR,
  505. get_temp_max, set_temp_max, 1);
  506. static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR,
  507. get_temp_max_hyst, set_temp_max_hyst, 0);
  508. static SENSOR_DEVICE_ATTR(temp2_max_hyst, S_IRUGO | S_IWUSR,
  509. get_temp_max_hyst, set_temp_max_hyst, 1);
  510. static ssize_t get_alarms(struct device *dev, struct device_attribute *attr,
  511. char *buf)
  512. {
  513. struct gl520_data *data = gl520_update_device(dev);
  514. return sprintf(buf, "%d\n", data->alarms);
  515. }
  516. static ssize_t get_beep_enable(struct device *dev, struct device_attribute
  517. *attr, char *buf)
  518. {
  519. struct gl520_data *data = gl520_update_device(dev);
  520. return sprintf(buf, "%d\n", data->beep_enable);
  521. }
  522. static ssize_t get_beep_mask(struct device *dev, struct device_attribute *attr,
  523. char *buf)
  524. {
  525. struct gl520_data *data = gl520_update_device(dev);
  526. return sprintf(buf, "%d\n", data->beep_mask);
  527. }
  528. static ssize_t set_beep_enable(struct device *dev, struct device_attribute
  529. *attr, const char *buf, size_t count)
  530. {
  531. struct gl520_data *data = dev_get_drvdata(dev);
  532. struct i2c_client *client = data->client;
  533. u8 r;
  534. unsigned long v;
  535. int err;
  536. err = kstrtoul(buf, 10, &v);
  537. if (err)
  538. return err;
  539. r = (v ? 0 : 1);
  540. mutex_lock(&data->update_lock);
  541. data->beep_enable = !r;
  542. gl520_write_value(client, GL520_REG_BEEP_ENABLE,
  543. (gl520_read_value(client, GL520_REG_BEEP_ENABLE)
  544. & ~0x04) | (r << 2));
  545. mutex_unlock(&data->update_lock);
  546. return count;
  547. }
  548. static ssize_t set_beep_mask(struct device *dev, struct device_attribute *attr,
  549. const char *buf, size_t count)
  550. {
  551. struct gl520_data *data = dev_get_drvdata(dev);
  552. struct i2c_client *client = data->client;
  553. unsigned long r;
  554. int err;
  555. err = kstrtoul(buf, 10, &r);
  556. if (err)
  557. return err;
  558. mutex_lock(&data->update_lock);
  559. r &= data->alarm_mask;
  560. data->beep_mask = r;
  561. gl520_write_value(client, GL520_REG_BEEP_MASK, r);
  562. mutex_unlock(&data->update_lock);
  563. return count;
  564. }
  565. static DEVICE_ATTR(alarms, S_IRUGO, get_alarms, NULL);
  566. static DEVICE_ATTR(beep_enable, S_IRUGO | S_IWUSR,
  567. get_beep_enable, set_beep_enable);
  568. static DEVICE_ATTR(beep_mask, S_IRUGO | S_IWUSR,
  569. get_beep_mask, set_beep_mask);
  570. static ssize_t get_alarm(struct device *dev, struct device_attribute *attr,
  571. char *buf)
  572. {
  573. int bit_nr = to_sensor_dev_attr(attr)->index;
  574. struct gl520_data *data = gl520_update_device(dev);
  575. return sprintf(buf, "%d\n", (data->alarms >> bit_nr) & 1);
  576. }
  577. static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, get_alarm, NULL, 0);
  578. static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, get_alarm, NULL, 1);
  579. static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, get_alarm, NULL, 2);
  580. static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, get_alarm, NULL, 3);
  581. static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, get_alarm, NULL, 4);
  582. static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, get_alarm, NULL, 5);
  583. static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, get_alarm, NULL, 6);
  584. static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, get_alarm, NULL, 7);
  585. static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, get_alarm, NULL, 7);
  586. static ssize_t get_beep(struct device *dev, struct device_attribute *attr,
  587. char *buf)
  588. {
  589. int bitnr = to_sensor_dev_attr(attr)->index;
  590. struct gl520_data *data = gl520_update_device(dev);
  591. return sprintf(buf, "%d\n", (data->beep_mask >> bitnr) & 1);
  592. }
  593. static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
  594. const char *buf, size_t count)
  595. {
  596. struct gl520_data *data = dev_get_drvdata(dev);
  597. struct i2c_client *client = data->client;
  598. int bitnr = to_sensor_dev_attr(attr)->index;
  599. unsigned long bit;
  600. int err;
  601. err = kstrtoul(buf, 10, &bit);
  602. if (err)
  603. return err;
  604. if (bit & ~1)
  605. return -EINVAL;
  606. mutex_lock(&data->update_lock);
  607. data->beep_mask = gl520_read_value(client, GL520_REG_BEEP_MASK);
  608. if (bit)
  609. data->beep_mask |= (1 << bitnr);
  610. else
  611. data->beep_mask &= ~(1 << bitnr);
  612. gl520_write_value(client, GL520_REG_BEEP_MASK, data->beep_mask);
  613. mutex_unlock(&data->update_lock);
  614. return count;
  615. }
  616. static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR, get_beep, set_beep, 0);
  617. static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO | S_IWUSR, get_beep, set_beep, 1);
  618. static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO | S_IWUSR, get_beep, set_beep, 2);
  619. static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO | S_IWUSR, get_beep, set_beep, 3);
  620. static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR, get_beep, set_beep, 4);
  621. static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO | S_IWUSR, get_beep, set_beep, 5);
  622. static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO | S_IWUSR, get_beep, set_beep, 6);
  623. static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO | S_IWUSR, get_beep, set_beep, 7);
  624. static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO | S_IWUSR, get_beep, set_beep, 7);
  625. static struct attribute *gl520_attributes[] = {
  626. &dev_attr_cpu0_vid.attr,
  627. &sensor_dev_attr_in0_input.dev_attr.attr,
  628. &sensor_dev_attr_in0_min.dev_attr.attr,
  629. &sensor_dev_attr_in0_max.dev_attr.attr,
  630. &sensor_dev_attr_in0_alarm.dev_attr.attr,
  631. &sensor_dev_attr_in0_beep.dev_attr.attr,
  632. &sensor_dev_attr_in1_input.dev_attr.attr,
  633. &sensor_dev_attr_in1_min.dev_attr.attr,
  634. &sensor_dev_attr_in1_max.dev_attr.attr,
  635. &sensor_dev_attr_in1_alarm.dev_attr.attr,
  636. &sensor_dev_attr_in1_beep.dev_attr.attr,
  637. &sensor_dev_attr_in2_input.dev_attr.attr,
  638. &sensor_dev_attr_in2_min.dev_attr.attr,
  639. &sensor_dev_attr_in2_max.dev_attr.attr,
  640. &sensor_dev_attr_in2_alarm.dev_attr.attr,
  641. &sensor_dev_attr_in2_beep.dev_attr.attr,
  642. &sensor_dev_attr_in3_input.dev_attr.attr,
  643. &sensor_dev_attr_in3_min.dev_attr.attr,
  644. &sensor_dev_attr_in3_max.dev_attr.attr,
  645. &sensor_dev_attr_in3_alarm.dev_attr.attr,
  646. &sensor_dev_attr_in3_beep.dev_attr.attr,
  647. &sensor_dev_attr_fan1_input.dev_attr.attr,
  648. &sensor_dev_attr_fan1_min.dev_attr.attr,
  649. &sensor_dev_attr_fan1_div.dev_attr.attr,
  650. &sensor_dev_attr_fan1_alarm.dev_attr.attr,
  651. &sensor_dev_attr_fan1_beep.dev_attr.attr,
  652. &dev_attr_fan1_off.attr,
  653. &sensor_dev_attr_fan2_input.dev_attr.attr,
  654. &sensor_dev_attr_fan2_min.dev_attr.attr,
  655. &sensor_dev_attr_fan2_div.dev_attr.attr,
  656. &sensor_dev_attr_fan2_alarm.dev_attr.attr,
  657. &sensor_dev_attr_fan2_beep.dev_attr.attr,
  658. &sensor_dev_attr_temp1_input.dev_attr.attr,
  659. &sensor_dev_attr_temp1_max.dev_attr.attr,
  660. &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
  661. &sensor_dev_attr_temp1_alarm.dev_attr.attr,
  662. &sensor_dev_attr_temp1_beep.dev_attr.attr,
  663. &dev_attr_alarms.attr,
  664. &dev_attr_beep_enable.attr,
  665. &dev_attr_beep_mask.attr,
  666. NULL
  667. };
  668. static const struct attribute_group gl520_group = {
  669. .attrs = gl520_attributes,
  670. };
  671. static struct attribute *gl520_attributes_in4[] = {
  672. &sensor_dev_attr_in4_input.dev_attr.attr,
  673. &sensor_dev_attr_in4_min.dev_attr.attr,
  674. &sensor_dev_attr_in4_max.dev_attr.attr,
  675. &sensor_dev_attr_in4_alarm.dev_attr.attr,
  676. &sensor_dev_attr_in4_beep.dev_attr.attr,
  677. NULL
  678. };
  679. static struct attribute *gl520_attributes_temp2[] = {
  680. &sensor_dev_attr_temp2_input.dev_attr.attr,
  681. &sensor_dev_attr_temp2_max.dev_attr.attr,
  682. &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
  683. &sensor_dev_attr_temp2_alarm.dev_attr.attr,
  684. &sensor_dev_attr_temp2_beep.dev_attr.attr,
  685. NULL
  686. };
  687. static const struct attribute_group gl520_group_in4 = {
  688. .attrs = gl520_attributes_in4,
  689. };
  690. static const struct attribute_group gl520_group_temp2 = {
  691. .attrs = gl520_attributes_temp2,
  692. };
  693. /*
  694. * Real code
  695. */
  696. /* Return 0 if detection is successful, -ENODEV otherwise */
  697. static int gl520_detect(struct i2c_client *client, struct i2c_board_info *info)
  698. {
  699. struct i2c_adapter *adapter = client->adapter;
  700. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
  701. I2C_FUNC_SMBUS_WORD_DATA))
  702. return -ENODEV;
  703. /* Determine the chip type. */
  704. if ((gl520_read_value(client, GL520_REG_CHIP_ID) != 0x20) ||
  705. ((gl520_read_value(client, GL520_REG_REVISION) & 0x7f) != 0x00) ||
  706. ((gl520_read_value(client, GL520_REG_CONF) & 0x80) != 0x00)) {
  707. dev_dbg(&client->dev, "Unknown chip type, skipping\n");
  708. return -ENODEV;
  709. }
  710. strlcpy(info->type, "gl520sm", I2C_NAME_SIZE);
  711. return 0;
  712. }
  713. /* Called when we have found a new GL520SM. */
  714. static void gl520_init_client(struct i2c_client *client)
  715. {
  716. struct gl520_data *data = i2c_get_clientdata(client);
  717. u8 oldconf, conf;
  718. conf = oldconf = gl520_read_value(client, GL520_REG_CONF);
  719. data->alarm_mask = 0xff;
  720. data->vrm = vid_which_vrm();
  721. if (extra_sensor_type == 1)
  722. conf &= ~0x10;
  723. else if (extra_sensor_type == 2)
  724. conf |= 0x10;
  725. data->two_temps = !(conf & 0x10);
  726. /* If IRQ# is disabled, we can safely force comparator mode */
  727. if (!(conf & 0x20))
  728. conf &= 0xf7;
  729. /* Enable monitoring if needed */
  730. conf |= 0x40;
  731. if (conf != oldconf)
  732. gl520_write_value(client, GL520_REG_CONF, conf);
  733. gl520_update_device(&(client->dev));
  734. if (data->fan_min[0] == 0)
  735. data->alarm_mask &= ~0x20;
  736. if (data->fan_min[1] == 0)
  737. data->alarm_mask &= ~0x40;
  738. data->beep_mask &= data->alarm_mask;
  739. gl520_write_value(client, GL520_REG_BEEP_MASK, data->beep_mask);
  740. }
  741. static int gl520_probe(struct i2c_client *client,
  742. const struct i2c_device_id *id)
  743. {
  744. struct device *dev = &client->dev;
  745. struct device *hwmon_dev;
  746. struct gl520_data *data;
  747. data = devm_kzalloc(dev, sizeof(struct gl520_data), GFP_KERNEL);
  748. if (!data)
  749. return -ENOMEM;
  750. i2c_set_clientdata(client, data);
  751. mutex_init(&data->update_lock);
  752. data->client = client;
  753. /* Initialize the GL520SM chip */
  754. gl520_init_client(client);
  755. /* sysfs hooks */
  756. data->groups[0] = &gl520_group;
  757. if (data->two_temps)
  758. data->groups[1] = &gl520_group_temp2;
  759. else
  760. data->groups[1] = &gl520_group_in4;
  761. hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
  762. data, data->groups);
  763. return PTR_ERR_OR_ZERO(hwmon_dev);
  764. }
  765. static const struct i2c_device_id gl520_id[] = {
  766. { "gl520sm", 0 },
  767. { }
  768. };
  769. MODULE_DEVICE_TABLE(i2c, gl520_id);
  770. static struct i2c_driver gl520_driver = {
  771. .class = I2C_CLASS_HWMON,
  772. .driver = {
  773. .name = "gl520sm",
  774. },
  775. .probe = gl520_probe,
  776. .id_table = gl520_id,
  777. .detect = gl520_detect,
  778. .address_list = normal_i2c,
  779. };
  780. module_i2c_driver(gl520_driver);
  781. MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
  782. "Kyösti Mälkki <kmalkki@cc.hut.fi>, "
  783. "Maarten Deprez <maartendeprez@users.sourceforge.net>");
  784. MODULE_DESCRIPTION("GL520SM driver");
  785. MODULE_LICENSE("GPL");