lm87.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  1. /*
  2. * lm87.c
  3. *
  4. * Copyright (C) 2000 Frodo Looijaard <frodol@dds.nl>
  5. * Philip Edelbrock <phil@netroedge.com>
  6. * Stephen Rousset <stephen.rousset@rocketlogix.com>
  7. * Dan Eaton <dan.eaton@rocketlogix.com>
  8. * Copyright (C) 2004-2008 Jean Delvare <jdelvare@suse.de>
  9. *
  10. * Original port to Linux 2.6 by Jeff Oliver.
  11. *
  12. * The LM87 is a sensor chip made by National Semiconductor. It monitors up
  13. * to 8 voltages (including its own power source), up to three temperatures
  14. * (its own plus up to two external ones) and up to two fans. The default
  15. * configuration is 6 voltages, two temperatures and two fans (see below).
  16. * Voltages are scaled internally with ratios such that the nominal value of
  17. * each voltage correspond to a register value of 192 (which means a
  18. * resolution of about 0.5% of the nominal value). Temperature values are
  19. * reported with a 1 deg resolution and a 3-4 deg accuracy. Complete
  20. * datasheet can be obtained from National's website at:
  21. * http://www.national.com/pf/LM/LM87.html
  22. *
  23. * Some functions share pins, so not all functions are available at the same
  24. * time. Which are depends on the hardware setup. This driver normally
  25. * assumes that firmware configured the chip correctly. Where this is not
  26. * the case, platform code must set the I2C client's platform_data to point
  27. * to a u8 value to be written to the channel register.
  28. * For reference, here is the list of exclusive functions:
  29. * - in0+in5 (default) or temp3
  30. * - fan1 (default) or in6
  31. * - fan2 (default) or in7
  32. * - VID lines (default) or IRQ lines (not handled by this driver)
  33. *
  34. * The LM87 additionally features an analog output, supposedly usable to
  35. * control the speed of a fan. All new chips use pulse width modulation
  36. * instead. The LM87 is the only hardware monitoring chipset I know of
  37. * which uses amplitude modulation. Be careful when using this feature.
  38. *
  39. * This driver also supports the ADM1024, a sensor chip made by Analog
  40. * Devices. That chip is fully compatible with the LM87. Complete
  41. * datasheet can be obtained from Analog's website at:
  42. * http://www.analog.com/en/prod/0,2877,ADM1024,00.html
  43. *
  44. * This program is free software; you can redistribute it and/or modify
  45. * it under the terms of the GNU General Public License as published by
  46. * the Free Software Foundation; either version 2 of the License, or
  47. * (at your option) any later version.
  48. *
  49. * This program is distributed in the hope that it will be useful,
  50. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  51. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  52. * GNU General Public License for more details.
  53. *
  54. * You should have received a copy of the GNU General Public License
  55. * along with this program; if not, write to the Free Software
  56. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  57. */
  58. #include <linux/module.h>
  59. #include <linux/init.h>
  60. #include <linux/slab.h>
  61. #include <linux/jiffies.h>
  62. #include <linux/i2c.h>
  63. #include <linux/hwmon.h>
  64. #include <linux/hwmon-sysfs.h>
  65. #include <linux/hwmon-vid.h>
  66. #include <linux/err.h>
  67. #include <linux/mutex.h>
  68. /*
  69. * Addresses to scan
  70. * LM87 has three possible addresses: 0x2c, 0x2d and 0x2e.
  71. */
  72. static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
  73. enum chips { lm87, adm1024 };
  74. /*
  75. * The LM87 registers
  76. */
  77. /* nr in 0..5 */
  78. #define LM87_REG_IN(nr) (0x20 + (nr))
  79. #define LM87_REG_IN_MAX(nr) (0x2B + (nr) * 2)
  80. #define LM87_REG_IN_MIN(nr) (0x2C + (nr) * 2)
  81. /* nr in 0..1 */
  82. #define LM87_REG_AIN(nr) (0x28 + (nr))
  83. #define LM87_REG_AIN_MIN(nr) (0x1A + (nr))
  84. #define LM87_REG_AIN_MAX(nr) (0x3B + (nr))
  85. static u8 LM87_REG_TEMP[3] = { 0x27, 0x26, 0x20 };
  86. static u8 LM87_REG_TEMP_HIGH[3] = { 0x39, 0x37, 0x2B };
  87. static u8 LM87_REG_TEMP_LOW[3] = { 0x3A, 0x38, 0x2C };
  88. #define LM87_REG_TEMP_HW_INT_LOCK 0x13
  89. #define LM87_REG_TEMP_HW_EXT_LOCK 0x14
  90. #define LM87_REG_TEMP_HW_INT 0x17
  91. #define LM87_REG_TEMP_HW_EXT 0x18
  92. /* nr in 0..1 */
  93. #define LM87_REG_FAN(nr) (0x28 + (nr))
  94. #define LM87_REG_FAN_MIN(nr) (0x3B + (nr))
  95. #define LM87_REG_AOUT 0x19
  96. #define LM87_REG_CONFIG 0x40
  97. #define LM87_REG_CHANNEL_MODE 0x16
  98. #define LM87_REG_VID_FAN_DIV 0x47
  99. #define LM87_REG_VID4 0x49
  100. #define LM87_REG_ALARMS1 0x41
  101. #define LM87_REG_ALARMS2 0x42
  102. #define LM87_REG_COMPANY_ID 0x3E
  103. #define LM87_REG_REVISION 0x3F
  104. /*
  105. * Conversions and various macros
  106. * The LM87 uses signed 8-bit values for temperatures.
  107. */
  108. #define IN_FROM_REG(reg, scale) (((reg) * (scale) + 96) / 192)
  109. #define IN_TO_REG(val, scale) ((val) <= 0 ? 0 : \
  110. (val) * 192 >= (scale) * 255 ? 255 : \
  111. ((val) * 192 + (scale) / 2) / (scale))
  112. #define TEMP_FROM_REG(reg) ((reg) * 1000)
  113. #define TEMP_TO_REG(val) ((val) <= -127500 ? -128 : \
  114. (val) >= 126500 ? 127 : \
  115. (((val) < 0 ? (val) - 500 : \
  116. (val) + 500) / 1000))
  117. #define FAN_FROM_REG(reg, div) ((reg) == 255 || (reg) == 0 ? 0 : \
  118. (1350000 + (reg)*(div) / 2) / ((reg) * (div)))
  119. #define FAN_TO_REG(val, div) ((val) * (div) * 255 <= 1350000 ? 255 : \
  120. (1350000 + (val)*(div) / 2) / ((val) * (div)))
  121. #define FAN_DIV_FROM_REG(reg) (1 << (reg))
  122. /* analog out is 9.80mV/LSB */
  123. #define AOUT_FROM_REG(reg) (((reg) * 98 + 5) / 10)
  124. #define AOUT_TO_REG(val) ((val) <= 0 ? 0 : \
  125. (val) >= 2500 ? 255 : \
  126. ((val) * 10 + 49) / 98)
  127. /* nr in 0..1 */
  128. #define CHAN_NO_FAN(nr) (1 << (nr))
  129. #define CHAN_TEMP3 (1 << 2)
  130. #define CHAN_VCC_5V (1 << 3)
  131. #define CHAN_NO_VID (1 << 7)
  132. /*
  133. * Client data (each client gets its own)
  134. */
  135. struct lm87_data {
  136. struct device *hwmon_dev;
  137. struct mutex update_lock;
  138. char valid; /* zero until following fields are valid */
  139. unsigned long last_updated; /* In jiffies */
  140. u8 channel; /* register value */
  141. u8 config; /* original register value */
  142. u8 in[8]; /* register value */
  143. u8 in_max[8]; /* register value */
  144. u8 in_min[8]; /* register value */
  145. u16 in_scale[8];
  146. s8 temp[3]; /* register value */
  147. s8 temp_high[3]; /* register value */
  148. s8 temp_low[3]; /* register value */
  149. s8 temp_crit_int; /* min of two register values */
  150. s8 temp_crit_ext; /* min of two register values */
  151. u8 fan[2]; /* register value */
  152. u8 fan_min[2]; /* register value */
  153. u8 fan_div[2]; /* register value, shifted right */
  154. u8 aout; /* register value */
  155. u16 alarms; /* register values, combined */
  156. u8 vid; /* register values, combined */
  157. u8 vrm;
  158. };
  159. static inline int lm87_read_value(struct i2c_client *client, u8 reg)
  160. {
  161. return i2c_smbus_read_byte_data(client, reg);
  162. }
  163. static inline int lm87_write_value(struct i2c_client *client, u8 reg, u8 value)
  164. {
  165. return i2c_smbus_write_byte_data(client, reg, value);
  166. }
  167. static struct lm87_data *lm87_update_device(struct device *dev)
  168. {
  169. struct i2c_client *client = to_i2c_client(dev);
  170. struct lm87_data *data = i2c_get_clientdata(client);
  171. mutex_lock(&data->update_lock);
  172. if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
  173. int i, j;
  174. dev_dbg(&client->dev, "Updating data.\n");
  175. i = (data->channel & CHAN_TEMP3) ? 1 : 0;
  176. j = (data->channel & CHAN_TEMP3) ? 5 : 6;
  177. for (; i < j; i++) {
  178. data->in[i] = lm87_read_value(client,
  179. LM87_REG_IN(i));
  180. data->in_min[i] = lm87_read_value(client,
  181. LM87_REG_IN_MIN(i));
  182. data->in_max[i] = lm87_read_value(client,
  183. LM87_REG_IN_MAX(i));
  184. }
  185. for (i = 0; i < 2; i++) {
  186. if (data->channel & CHAN_NO_FAN(i)) {
  187. data->in[6+i] = lm87_read_value(client,
  188. LM87_REG_AIN(i));
  189. data->in_max[6+i] = lm87_read_value(client,
  190. LM87_REG_AIN_MAX(i));
  191. data->in_min[6+i] = lm87_read_value(client,
  192. LM87_REG_AIN_MIN(i));
  193. } else {
  194. data->fan[i] = lm87_read_value(client,
  195. LM87_REG_FAN(i));
  196. data->fan_min[i] = lm87_read_value(client,
  197. LM87_REG_FAN_MIN(i));
  198. }
  199. }
  200. j = (data->channel & CHAN_TEMP3) ? 3 : 2;
  201. for (i = 0 ; i < j; i++) {
  202. data->temp[i] = lm87_read_value(client,
  203. LM87_REG_TEMP[i]);
  204. data->temp_high[i] = lm87_read_value(client,
  205. LM87_REG_TEMP_HIGH[i]);
  206. data->temp_low[i] = lm87_read_value(client,
  207. LM87_REG_TEMP_LOW[i]);
  208. }
  209. i = lm87_read_value(client, LM87_REG_TEMP_HW_INT_LOCK);
  210. j = lm87_read_value(client, LM87_REG_TEMP_HW_INT);
  211. data->temp_crit_int = min(i, j);
  212. i = lm87_read_value(client, LM87_REG_TEMP_HW_EXT_LOCK);
  213. j = lm87_read_value(client, LM87_REG_TEMP_HW_EXT);
  214. data->temp_crit_ext = min(i, j);
  215. i = lm87_read_value(client, LM87_REG_VID_FAN_DIV);
  216. data->fan_div[0] = (i >> 4) & 0x03;
  217. data->fan_div[1] = (i >> 6) & 0x03;
  218. data->vid = (i & 0x0F)
  219. | (lm87_read_value(client, LM87_REG_VID4) & 0x01)
  220. << 4;
  221. data->alarms = lm87_read_value(client, LM87_REG_ALARMS1)
  222. | (lm87_read_value(client, LM87_REG_ALARMS2)
  223. << 8);
  224. data->aout = lm87_read_value(client, LM87_REG_AOUT);
  225. data->last_updated = jiffies;
  226. data->valid = 1;
  227. }
  228. mutex_unlock(&data->update_lock);
  229. return data;
  230. }
  231. /*
  232. * Sysfs stuff
  233. */
  234. static ssize_t show_in_input(struct device *dev, struct device_attribute *attr,
  235. char *buf)
  236. {
  237. struct lm87_data *data = lm87_update_device(dev);
  238. int nr = to_sensor_dev_attr(attr)->index;
  239. return sprintf(buf, "%u\n", IN_FROM_REG(data->in[nr],
  240. data->in_scale[nr]));
  241. }
  242. static ssize_t show_in_min(struct device *dev,
  243. struct device_attribute *attr, char *buf)
  244. {
  245. struct lm87_data *data = lm87_update_device(dev);
  246. int nr = to_sensor_dev_attr(attr)->index;
  247. return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[nr],
  248. data->in_scale[nr]));
  249. }
  250. static ssize_t show_in_max(struct device *dev,
  251. struct device_attribute *attr, char *buf)
  252. {
  253. struct lm87_data *data = lm87_update_device(dev);
  254. int nr = to_sensor_dev_attr(attr)->index;
  255. return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[nr],
  256. data->in_scale[nr]));
  257. }
  258. static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
  259. const char *buf, size_t count)
  260. {
  261. struct i2c_client *client = to_i2c_client(dev);
  262. struct lm87_data *data = i2c_get_clientdata(client);
  263. int nr = to_sensor_dev_attr(attr)->index;
  264. long val;
  265. int err;
  266. err = kstrtol(buf, 10, &val);
  267. if (err)
  268. return err;
  269. mutex_lock(&data->update_lock);
  270. data->in_min[nr] = IN_TO_REG(val, data->in_scale[nr]);
  271. lm87_write_value(client, nr < 6 ? LM87_REG_IN_MIN(nr) :
  272. LM87_REG_AIN_MIN(nr - 6), data->in_min[nr]);
  273. mutex_unlock(&data->update_lock);
  274. return count;
  275. }
  276. static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
  277. const char *buf, size_t count)
  278. {
  279. struct i2c_client *client = to_i2c_client(dev);
  280. struct lm87_data *data = i2c_get_clientdata(client);
  281. int nr = to_sensor_dev_attr(attr)->index;
  282. long val;
  283. int err;
  284. err = kstrtol(buf, 10, &val);
  285. if (err)
  286. return err;
  287. mutex_lock(&data->update_lock);
  288. data->in_max[nr] = IN_TO_REG(val, data->in_scale[nr]);
  289. lm87_write_value(client, nr < 6 ? LM87_REG_IN_MAX(nr) :
  290. LM87_REG_AIN_MAX(nr - 6), data->in_max[nr]);
  291. mutex_unlock(&data->update_lock);
  292. return count;
  293. }
  294. #define set_in(offset) \
  295. static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
  296. show_in_input, NULL, offset); \
  297. static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
  298. show_in_min, set_in_min, offset); \
  299. static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
  300. show_in_max, set_in_max, offset)
  301. set_in(0);
  302. set_in(1);
  303. set_in(2);
  304. set_in(3);
  305. set_in(4);
  306. set_in(5);
  307. set_in(6);
  308. set_in(7);
  309. static ssize_t show_temp_input(struct device *dev,
  310. struct device_attribute *attr, char *buf)
  311. {
  312. struct lm87_data *data = lm87_update_device(dev);
  313. int nr = to_sensor_dev_attr(attr)->index;
  314. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
  315. }
  316. static ssize_t show_temp_low(struct device *dev,
  317. struct device_attribute *attr, char *buf)
  318. {
  319. struct lm87_data *data = lm87_update_device(dev);
  320. int nr = to_sensor_dev_attr(attr)->index;
  321. return sprintf(buf, "%d\n",
  322. TEMP_FROM_REG(data->temp_low[nr]));
  323. }
  324. static ssize_t show_temp_high(struct device *dev,
  325. struct device_attribute *attr, char *buf)
  326. {
  327. struct lm87_data *data = lm87_update_device(dev);
  328. int nr = to_sensor_dev_attr(attr)->index;
  329. return sprintf(buf, "%d\n",
  330. TEMP_FROM_REG(data->temp_high[nr]));
  331. }
  332. static ssize_t set_temp_low(struct device *dev, struct device_attribute *attr,
  333. const char *buf, size_t count)
  334. {
  335. struct i2c_client *client = to_i2c_client(dev);
  336. struct lm87_data *data = i2c_get_clientdata(client);
  337. int nr = to_sensor_dev_attr(attr)->index;
  338. long val;
  339. int err;
  340. err = kstrtol(buf, 10, &val);
  341. if (err)
  342. return err;
  343. mutex_lock(&data->update_lock);
  344. data->temp_low[nr] = TEMP_TO_REG(val);
  345. lm87_write_value(client, LM87_REG_TEMP_LOW[nr], data->temp_low[nr]);
  346. mutex_unlock(&data->update_lock);
  347. return count;
  348. }
  349. static ssize_t set_temp_high(struct device *dev, struct device_attribute *attr,
  350. const char *buf, size_t count)
  351. {
  352. struct i2c_client *client = to_i2c_client(dev);
  353. struct lm87_data *data = i2c_get_clientdata(client);
  354. int nr = to_sensor_dev_attr(attr)->index;
  355. long val;
  356. int err;
  357. err = kstrtol(buf, 10, &val);
  358. if (err)
  359. return err;
  360. mutex_lock(&data->update_lock);
  361. data->temp_high[nr] = TEMP_TO_REG(val);
  362. lm87_write_value(client, LM87_REG_TEMP_HIGH[nr], data->temp_high[nr]);
  363. mutex_unlock(&data->update_lock);
  364. return count;
  365. }
  366. #define set_temp(offset) \
  367. static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
  368. show_temp_input, NULL, offset - 1); \
  369. static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
  370. show_temp_high, set_temp_high, offset - 1); \
  371. static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
  372. show_temp_low, set_temp_low, offset - 1)
  373. set_temp(1);
  374. set_temp(2);
  375. set_temp(3);
  376. static ssize_t show_temp_crit_int(struct device *dev,
  377. struct device_attribute *attr, char *buf)
  378. {
  379. struct lm87_data *data = lm87_update_device(dev);
  380. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit_int));
  381. }
  382. static ssize_t show_temp_crit_ext(struct device *dev,
  383. struct device_attribute *attr, char *buf)
  384. {
  385. struct lm87_data *data = lm87_update_device(dev);
  386. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit_ext));
  387. }
  388. static DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp_crit_int, NULL);
  389. static DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp_crit_ext, NULL);
  390. static DEVICE_ATTR(temp3_crit, S_IRUGO, show_temp_crit_ext, NULL);
  391. static ssize_t show_fan_input(struct device *dev,
  392. struct device_attribute *attr, char *buf)
  393. {
  394. struct lm87_data *data = lm87_update_device(dev);
  395. int nr = to_sensor_dev_attr(attr)->index;
  396. return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
  397. FAN_DIV_FROM_REG(data->fan_div[nr])));
  398. }
  399. static ssize_t show_fan_min(struct device *dev,
  400. struct device_attribute *attr, char *buf)
  401. {
  402. struct lm87_data *data = lm87_update_device(dev);
  403. int nr = to_sensor_dev_attr(attr)->index;
  404. return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
  405. FAN_DIV_FROM_REG(data->fan_div[nr])));
  406. }
  407. static ssize_t show_fan_div(struct device *dev,
  408. struct device_attribute *attr, char *buf)
  409. {
  410. struct lm87_data *data = lm87_update_device(dev);
  411. int nr = to_sensor_dev_attr(attr)->index;
  412. return sprintf(buf, "%d\n",
  413. FAN_DIV_FROM_REG(data->fan_div[nr]));
  414. }
  415. static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
  416. const char *buf, size_t count)
  417. {
  418. struct i2c_client *client = to_i2c_client(dev);
  419. struct lm87_data *data = i2c_get_clientdata(client);
  420. int nr = to_sensor_dev_attr(attr)->index;
  421. long val;
  422. int err;
  423. err = kstrtol(buf, 10, &val);
  424. if (err)
  425. return err;
  426. mutex_lock(&data->update_lock);
  427. data->fan_min[nr] = FAN_TO_REG(val,
  428. FAN_DIV_FROM_REG(data->fan_div[nr]));
  429. lm87_write_value(client, LM87_REG_FAN_MIN(nr), data->fan_min[nr]);
  430. mutex_unlock(&data->update_lock);
  431. return count;
  432. }
  433. /*
  434. * Note: we save and restore the fan minimum here, because its value is
  435. * determined in part by the fan clock divider. This follows the principle
  436. * of least surprise; the user doesn't expect the fan minimum to change just
  437. * because the divider changed.
  438. */
  439. static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
  440. const char *buf, size_t count)
  441. {
  442. struct i2c_client *client = to_i2c_client(dev);
  443. struct lm87_data *data = i2c_get_clientdata(client);
  444. int nr = to_sensor_dev_attr(attr)->index;
  445. long val;
  446. int err;
  447. unsigned long min;
  448. u8 reg;
  449. err = kstrtol(buf, 10, &val);
  450. if (err)
  451. return err;
  452. mutex_lock(&data->update_lock);
  453. min = FAN_FROM_REG(data->fan_min[nr],
  454. FAN_DIV_FROM_REG(data->fan_div[nr]));
  455. switch (val) {
  456. case 1:
  457. data->fan_div[nr] = 0;
  458. break;
  459. case 2:
  460. data->fan_div[nr] = 1;
  461. break;
  462. case 4:
  463. data->fan_div[nr] = 2;
  464. break;
  465. case 8:
  466. data->fan_div[nr] = 3;
  467. break;
  468. default:
  469. mutex_unlock(&data->update_lock);
  470. return -EINVAL;
  471. }
  472. reg = lm87_read_value(client, LM87_REG_VID_FAN_DIV);
  473. switch (nr) {
  474. case 0:
  475. reg = (reg & 0xCF) | (data->fan_div[0] << 4);
  476. break;
  477. case 1:
  478. reg = (reg & 0x3F) | (data->fan_div[1] << 6);
  479. break;
  480. }
  481. lm87_write_value(client, LM87_REG_VID_FAN_DIV, reg);
  482. data->fan_min[nr] = FAN_TO_REG(min, val);
  483. lm87_write_value(client, LM87_REG_FAN_MIN(nr),
  484. data->fan_min[nr]);
  485. mutex_unlock(&data->update_lock);
  486. return count;
  487. }
  488. #define set_fan(offset) \
  489. static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
  490. show_fan_input, NULL, offset - 1); \
  491. static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
  492. show_fan_min, set_fan_min, offset - 1); \
  493. static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
  494. show_fan_div, set_fan_div, offset - 1)
  495. set_fan(1);
  496. set_fan(2);
  497. static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
  498. char *buf)
  499. {
  500. struct lm87_data *data = lm87_update_device(dev);
  501. return sprintf(buf, "%d\n", data->alarms);
  502. }
  503. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  504. static ssize_t show_vid(struct device *dev, struct device_attribute *attr,
  505. char *buf)
  506. {
  507. struct lm87_data *data = lm87_update_device(dev);
  508. return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
  509. }
  510. static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
  511. static ssize_t show_vrm(struct device *dev, struct device_attribute *attr,
  512. char *buf)
  513. {
  514. struct lm87_data *data = dev_get_drvdata(dev);
  515. return sprintf(buf, "%d\n", data->vrm);
  516. }
  517. static ssize_t set_vrm(struct device *dev, struct device_attribute *attr,
  518. const char *buf, size_t count)
  519. {
  520. struct lm87_data *data = dev_get_drvdata(dev);
  521. unsigned long val;
  522. int err;
  523. err = kstrtoul(buf, 10, &val);
  524. if (err)
  525. return err;
  526. if (val > 255)
  527. return -EINVAL;
  528. data->vrm = val;
  529. return count;
  530. }
  531. static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
  532. static ssize_t show_aout(struct device *dev, struct device_attribute *attr,
  533. char *buf)
  534. {
  535. struct lm87_data *data = lm87_update_device(dev);
  536. return sprintf(buf, "%d\n", AOUT_FROM_REG(data->aout));
  537. }
  538. static ssize_t set_aout(struct device *dev, struct device_attribute *attr,
  539. const char *buf, size_t count)
  540. {
  541. struct i2c_client *client = to_i2c_client(dev);
  542. struct lm87_data *data = i2c_get_clientdata(client);
  543. long val;
  544. int err;
  545. err = kstrtol(buf, 10, &val);
  546. if (err)
  547. return err;
  548. mutex_lock(&data->update_lock);
  549. data->aout = AOUT_TO_REG(val);
  550. lm87_write_value(client, LM87_REG_AOUT, data->aout);
  551. mutex_unlock(&data->update_lock);
  552. return count;
  553. }
  554. static DEVICE_ATTR(aout_output, S_IRUGO | S_IWUSR, show_aout, set_aout);
  555. static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
  556. char *buf)
  557. {
  558. struct lm87_data *data = lm87_update_device(dev);
  559. int bitnr = to_sensor_dev_attr(attr)->index;
  560. return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
  561. }
  562. static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
  563. static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
  564. static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
  565. static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
  566. static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
  567. static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 9);
  568. static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 6);
  569. static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 7);
  570. static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
  571. static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
  572. static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 5);
  573. static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6);
  574. static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7);
  575. static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 14);
  576. static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 15);
  577. /*
  578. * Real code
  579. */
  580. static struct attribute *lm87_attributes[] = {
  581. &sensor_dev_attr_in1_input.dev_attr.attr,
  582. &sensor_dev_attr_in1_min.dev_attr.attr,
  583. &sensor_dev_attr_in1_max.dev_attr.attr,
  584. &sensor_dev_attr_in1_alarm.dev_attr.attr,
  585. &sensor_dev_attr_in2_input.dev_attr.attr,
  586. &sensor_dev_attr_in2_min.dev_attr.attr,
  587. &sensor_dev_attr_in2_max.dev_attr.attr,
  588. &sensor_dev_attr_in2_alarm.dev_attr.attr,
  589. &sensor_dev_attr_in3_input.dev_attr.attr,
  590. &sensor_dev_attr_in3_min.dev_attr.attr,
  591. &sensor_dev_attr_in3_max.dev_attr.attr,
  592. &sensor_dev_attr_in3_alarm.dev_attr.attr,
  593. &sensor_dev_attr_in4_input.dev_attr.attr,
  594. &sensor_dev_attr_in4_min.dev_attr.attr,
  595. &sensor_dev_attr_in4_max.dev_attr.attr,
  596. &sensor_dev_attr_in4_alarm.dev_attr.attr,
  597. &sensor_dev_attr_temp1_input.dev_attr.attr,
  598. &sensor_dev_attr_temp1_max.dev_attr.attr,
  599. &sensor_dev_attr_temp1_min.dev_attr.attr,
  600. &dev_attr_temp1_crit.attr,
  601. &sensor_dev_attr_temp1_alarm.dev_attr.attr,
  602. &sensor_dev_attr_temp2_input.dev_attr.attr,
  603. &sensor_dev_attr_temp2_max.dev_attr.attr,
  604. &sensor_dev_attr_temp2_min.dev_attr.attr,
  605. &dev_attr_temp2_crit.attr,
  606. &sensor_dev_attr_temp2_alarm.dev_attr.attr,
  607. &sensor_dev_attr_temp2_fault.dev_attr.attr,
  608. &dev_attr_alarms.attr,
  609. &dev_attr_aout_output.attr,
  610. NULL
  611. };
  612. static const struct attribute_group lm87_group = {
  613. .attrs = lm87_attributes,
  614. };
  615. static struct attribute *lm87_attributes_in6[] = {
  616. &sensor_dev_attr_in6_input.dev_attr.attr,
  617. &sensor_dev_attr_in6_min.dev_attr.attr,
  618. &sensor_dev_attr_in6_max.dev_attr.attr,
  619. &sensor_dev_attr_in6_alarm.dev_attr.attr,
  620. NULL
  621. };
  622. static const struct attribute_group lm87_group_in6 = {
  623. .attrs = lm87_attributes_in6,
  624. };
  625. static struct attribute *lm87_attributes_fan1[] = {
  626. &sensor_dev_attr_fan1_input.dev_attr.attr,
  627. &sensor_dev_attr_fan1_min.dev_attr.attr,
  628. &sensor_dev_attr_fan1_div.dev_attr.attr,
  629. &sensor_dev_attr_fan1_alarm.dev_attr.attr,
  630. NULL
  631. };
  632. static const struct attribute_group lm87_group_fan1 = {
  633. .attrs = lm87_attributes_fan1,
  634. };
  635. static struct attribute *lm87_attributes_in7[] = {
  636. &sensor_dev_attr_in7_input.dev_attr.attr,
  637. &sensor_dev_attr_in7_min.dev_attr.attr,
  638. &sensor_dev_attr_in7_max.dev_attr.attr,
  639. &sensor_dev_attr_in7_alarm.dev_attr.attr,
  640. NULL
  641. };
  642. static const struct attribute_group lm87_group_in7 = {
  643. .attrs = lm87_attributes_in7,
  644. };
  645. static struct attribute *lm87_attributes_fan2[] = {
  646. &sensor_dev_attr_fan2_input.dev_attr.attr,
  647. &sensor_dev_attr_fan2_min.dev_attr.attr,
  648. &sensor_dev_attr_fan2_div.dev_attr.attr,
  649. &sensor_dev_attr_fan2_alarm.dev_attr.attr,
  650. NULL
  651. };
  652. static const struct attribute_group lm87_group_fan2 = {
  653. .attrs = lm87_attributes_fan2,
  654. };
  655. static struct attribute *lm87_attributes_temp3[] = {
  656. &sensor_dev_attr_temp3_input.dev_attr.attr,
  657. &sensor_dev_attr_temp3_max.dev_attr.attr,
  658. &sensor_dev_attr_temp3_min.dev_attr.attr,
  659. &dev_attr_temp3_crit.attr,
  660. &sensor_dev_attr_temp3_alarm.dev_attr.attr,
  661. &sensor_dev_attr_temp3_fault.dev_attr.attr,
  662. NULL
  663. };
  664. static const struct attribute_group lm87_group_temp3 = {
  665. .attrs = lm87_attributes_temp3,
  666. };
  667. static struct attribute *lm87_attributes_in0_5[] = {
  668. &sensor_dev_attr_in0_input.dev_attr.attr,
  669. &sensor_dev_attr_in0_min.dev_attr.attr,
  670. &sensor_dev_attr_in0_max.dev_attr.attr,
  671. &sensor_dev_attr_in0_alarm.dev_attr.attr,
  672. &sensor_dev_attr_in5_input.dev_attr.attr,
  673. &sensor_dev_attr_in5_min.dev_attr.attr,
  674. &sensor_dev_attr_in5_max.dev_attr.attr,
  675. &sensor_dev_attr_in5_alarm.dev_attr.attr,
  676. NULL
  677. };
  678. static const struct attribute_group lm87_group_in0_5 = {
  679. .attrs = lm87_attributes_in0_5,
  680. };
  681. static struct attribute *lm87_attributes_vid[] = {
  682. &dev_attr_cpu0_vid.attr,
  683. &dev_attr_vrm.attr,
  684. NULL
  685. };
  686. static const struct attribute_group lm87_group_vid = {
  687. .attrs = lm87_attributes_vid,
  688. };
  689. /* Return 0 if detection is successful, -ENODEV otherwise */
  690. static int lm87_detect(struct i2c_client *client, struct i2c_board_info *info)
  691. {
  692. struct i2c_adapter *adapter = client->adapter;
  693. const char *name;
  694. u8 cid, rev;
  695. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  696. return -ENODEV;
  697. if (lm87_read_value(client, LM87_REG_CONFIG) & 0x80)
  698. return -ENODEV;
  699. /* Now, we do the remaining detection. */
  700. cid = lm87_read_value(client, LM87_REG_COMPANY_ID);
  701. rev = lm87_read_value(client, LM87_REG_REVISION);
  702. if (cid == 0x02 /* National Semiconductor */
  703. && (rev >= 0x01 && rev <= 0x08))
  704. name = "lm87";
  705. else if (cid == 0x41 /* Analog Devices */
  706. && (rev & 0xf0) == 0x10)
  707. name = "adm1024";
  708. else {
  709. dev_dbg(&adapter->dev, "LM87 detection failed at 0x%02x\n",
  710. client->addr);
  711. return -ENODEV;
  712. }
  713. strlcpy(info->type, name, I2C_NAME_SIZE);
  714. return 0;
  715. }
  716. static void lm87_remove_files(struct i2c_client *client)
  717. {
  718. struct device *dev = &client->dev;
  719. sysfs_remove_group(&dev->kobj, &lm87_group);
  720. sysfs_remove_group(&dev->kobj, &lm87_group_in6);
  721. sysfs_remove_group(&dev->kobj, &lm87_group_fan1);
  722. sysfs_remove_group(&dev->kobj, &lm87_group_in7);
  723. sysfs_remove_group(&dev->kobj, &lm87_group_fan2);
  724. sysfs_remove_group(&dev->kobj, &lm87_group_temp3);
  725. sysfs_remove_group(&dev->kobj, &lm87_group_in0_5);
  726. sysfs_remove_group(&dev->kobj, &lm87_group_vid);
  727. }
  728. static void lm87_init_client(struct i2c_client *client)
  729. {
  730. struct lm87_data *data = i2c_get_clientdata(client);
  731. if (dev_get_platdata(&client->dev)) {
  732. data->channel = *(u8 *)dev_get_platdata(&client->dev);
  733. lm87_write_value(client,
  734. LM87_REG_CHANNEL_MODE, data->channel);
  735. } else {
  736. data->channel = lm87_read_value(client, LM87_REG_CHANNEL_MODE);
  737. }
  738. data->config = lm87_read_value(client, LM87_REG_CONFIG) & 0x6F;
  739. if (!(data->config & 0x01)) {
  740. int i;
  741. /* Limits are left uninitialized after power-up */
  742. for (i = 1; i < 6; i++) {
  743. lm87_write_value(client, LM87_REG_IN_MIN(i), 0x00);
  744. lm87_write_value(client, LM87_REG_IN_MAX(i), 0xFF);
  745. }
  746. for (i = 0; i < 2; i++) {
  747. lm87_write_value(client, LM87_REG_TEMP_HIGH[i], 0x7F);
  748. lm87_write_value(client, LM87_REG_TEMP_LOW[i], 0x00);
  749. lm87_write_value(client, LM87_REG_AIN_MIN(i), 0x00);
  750. lm87_write_value(client, LM87_REG_AIN_MAX(i), 0xFF);
  751. }
  752. if (data->channel & CHAN_TEMP3) {
  753. lm87_write_value(client, LM87_REG_TEMP_HIGH[2], 0x7F);
  754. lm87_write_value(client, LM87_REG_TEMP_LOW[2], 0x00);
  755. } else {
  756. lm87_write_value(client, LM87_REG_IN_MIN(0), 0x00);
  757. lm87_write_value(client, LM87_REG_IN_MAX(0), 0xFF);
  758. }
  759. }
  760. /* Make sure Start is set and INT#_Clear is clear */
  761. if ((data->config & 0x09) != 0x01)
  762. lm87_write_value(client, LM87_REG_CONFIG,
  763. (data->config & 0x77) | 0x01);
  764. }
  765. static int lm87_probe(struct i2c_client *client, const struct i2c_device_id *id)
  766. {
  767. struct lm87_data *data;
  768. int err;
  769. data = devm_kzalloc(&client->dev, sizeof(struct lm87_data), GFP_KERNEL);
  770. if (!data)
  771. return -ENOMEM;
  772. i2c_set_clientdata(client, data);
  773. mutex_init(&data->update_lock);
  774. /* Initialize the LM87 chip */
  775. lm87_init_client(client);
  776. data->in_scale[0] = 2500;
  777. data->in_scale[1] = 2700;
  778. data->in_scale[2] = (data->channel & CHAN_VCC_5V) ? 5000 : 3300;
  779. data->in_scale[3] = 5000;
  780. data->in_scale[4] = 12000;
  781. data->in_scale[5] = 2700;
  782. data->in_scale[6] = 1875;
  783. data->in_scale[7] = 1875;
  784. /* Register sysfs hooks */
  785. err = sysfs_create_group(&client->dev.kobj, &lm87_group);
  786. if (err)
  787. goto exit_stop;
  788. if (data->channel & CHAN_NO_FAN(0)) {
  789. err = sysfs_create_group(&client->dev.kobj, &lm87_group_in6);
  790. if (err)
  791. goto exit_remove;
  792. } else {
  793. err = sysfs_create_group(&client->dev.kobj, &lm87_group_fan1);
  794. if (err)
  795. goto exit_remove;
  796. }
  797. if (data->channel & CHAN_NO_FAN(1)) {
  798. err = sysfs_create_group(&client->dev.kobj, &lm87_group_in7);
  799. if (err)
  800. goto exit_remove;
  801. } else {
  802. err = sysfs_create_group(&client->dev.kobj, &lm87_group_fan2);
  803. if (err)
  804. goto exit_remove;
  805. }
  806. if (data->channel & CHAN_TEMP3) {
  807. err = sysfs_create_group(&client->dev.kobj, &lm87_group_temp3);
  808. if (err)
  809. goto exit_remove;
  810. } else {
  811. err = sysfs_create_group(&client->dev.kobj, &lm87_group_in0_5);
  812. if (err)
  813. goto exit_remove;
  814. }
  815. if (!(data->channel & CHAN_NO_VID)) {
  816. data->vrm = vid_which_vrm();
  817. err = sysfs_create_group(&client->dev.kobj, &lm87_group_vid);
  818. if (err)
  819. goto exit_remove;
  820. }
  821. data->hwmon_dev = hwmon_device_register(&client->dev);
  822. if (IS_ERR(data->hwmon_dev)) {
  823. err = PTR_ERR(data->hwmon_dev);
  824. goto exit_remove;
  825. }
  826. return 0;
  827. exit_remove:
  828. lm87_remove_files(client);
  829. exit_stop:
  830. lm87_write_value(client, LM87_REG_CONFIG, data->config);
  831. return err;
  832. }
  833. static int lm87_remove(struct i2c_client *client)
  834. {
  835. struct lm87_data *data = i2c_get_clientdata(client);
  836. hwmon_device_unregister(data->hwmon_dev);
  837. lm87_remove_files(client);
  838. lm87_write_value(client, LM87_REG_CONFIG, data->config);
  839. return 0;
  840. }
  841. /*
  842. * Driver data (common to all clients)
  843. */
  844. static const struct i2c_device_id lm87_id[] = {
  845. { "lm87", lm87 },
  846. { "adm1024", adm1024 },
  847. { }
  848. };
  849. MODULE_DEVICE_TABLE(i2c, lm87_id);
  850. static struct i2c_driver lm87_driver = {
  851. .class = I2C_CLASS_HWMON,
  852. .driver = {
  853. .name = "lm87",
  854. },
  855. .probe = lm87_probe,
  856. .remove = lm87_remove,
  857. .id_table = lm87_id,
  858. .detect = lm87_detect,
  859. .address_list = normal_i2c,
  860. };
  861. module_i2c_driver(lm87_driver);
  862. MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de> and others");
  863. MODULE_DESCRIPTION("LM87 driver");
  864. MODULE_LICENSE("GPL");