lm63.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  1. /*
  2. * lm63.c - driver for the National Semiconductor LM63 temperature sensor
  3. * with integrated fan control
  4. * Copyright (C) 2004-2008 Jean Delvare <khali@linux-fr.org>
  5. * Based on the lm90 driver.
  6. *
  7. * The LM63 is a sensor chip made by National Semiconductor. It measures
  8. * two temperatures (its own and one external one) and the speed of one
  9. * fan, those speed it can additionally control. Complete datasheet can be
  10. * obtained from National's website at:
  11. * http://www.national.com/pf/LM/LM63.html
  12. *
  13. * The LM63 is basically an LM86 with fan speed monitoring and control
  14. * capabilities added. It misses some of the LM86 features though:
  15. * - No low limit for local temperature.
  16. * - No critical limit for local temperature.
  17. * - Critical limit for remote temperature can be changed only once. We
  18. * will consider that the critical limit is read-only.
  19. *
  20. * The datasheet isn't very clear about what the tachometer reading is.
  21. * I had a explanation from National Semiconductor though. The two lower
  22. * bits of the read value have to be masked out. The value is still 16 bit
  23. * in width.
  24. *
  25. * This program is free software; you can redistribute it and/or modify
  26. * it under the terms of the GNU General Public License as published by
  27. * the Free Software Foundation; either version 2 of the License, or
  28. * (at your option) any later version.
  29. *
  30. * This program is distributed in the hope that it will be useful,
  31. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  32. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  33. * GNU General Public License for more details.
  34. *
  35. * You should have received a copy of the GNU General Public License
  36. * along with this program; if not, write to the Free Software
  37. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  38. */
  39. #include <linux/module.h>
  40. #include <linux/init.h>
  41. #include <linux/slab.h>
  42. #include <linux/jiffies.h>
  43. #include <linux/i2c.h>
  44. #include <linux/hwmon-sysfs.h>
  45. #include <linux/hwmon.h>
  46. #include <linux/err.h>
  47. #include <linux/mutex.h>
  48. #include <linux/sysfs.h>
  49. #include <linux/types.h>
  50. /*
  51. * Addresses to scan
  52. * Address is fully defined internally and cannot be changed.
  53. */
  54. static const unsigned short normal_i2c[] = { 0x18, 0x4c, 0x4e, I2C_CLIENT_END };
  55. /*
  56. * The LM63 registers
  57. */
  58. #define LM63_REG_CONFIG1 0x03
  59. #define LM63_REG_CONVRATE 0x04
  60. #define LM63_REG_CONFIG2 0xBF
  61. #define LM63_REG_CONFIG_FAN 0x4A
  62. #define LM63_REG_TACH_COUNT_MSB 0x47
  63. #define LM63_REG_TACH_COUNT_LSB 0x46
  64. #define LM63_REG_TACH_LIMIT_MSB 0x49
  65. #define LM63_REG_TACH_LIMIT_LSB 0x48
  66. #define LM63_REG_PWM_VALUE 0x4C
  67. #define LM63_REG_PWM_FREQ 0x4D
  68. #define LM63_REG_LOCAL_TEMP 0x00
  69. #define LM63_REG_LOCAL_HIGH 0x05
  70. #define LM63_REG_REMOTE_TEMP_MSB 0x01
  71. #define LM63_REG_REMOTE_TEMP_LSB 0x10
  72. #define LM63_REG_REMOTE_OFFSET_MSB 0x11
  73. #define LM63_REG_REMOTE_OFFSET_LSB 0x12
  74. #define LM63_REG_REMOTE_HIGH_MSB 0x07
  75. #define LM63_REG_REMOTE_HIGH_LSB 0x13
  76. #define LM63_REG_REMOTE_LOW_MSB 0x08
  77. #define LM63_REG_REMOTE_LOW_LSB 0x14
  78. #define LM63_REG_REMOTE_TCRIT 0x19
  79. #define LM63_REG_REMOTE_TCRIT_HYST 0x21
  80. #define LM63_REG_ALERT_STATUS 0x02
  81. #define LM63_REG_ALERT_MASK 0x16
  82. #define LM63_REG_MAN_ID 0xFE
  83. #define LM63_REG_CHIP_ID 0xFF
  84. #define LM96163_REG_TRUTHERM 0x30
  85. #define LM96163_REG_REMOTE_TEMP_U_MSB 0x31
  86. #define LM96163_REG_REMOTE_TEMP_U_LSB 0x32
  87. #define LM96163_REG_CONFIG_ENHANCED 0x45
  88. #define LM63_MAX_CONVRATE 9
  89. #define LM63_MAX_CONVRATE_HZ 32
  90. #define LM96163_MAX_CONVRATE_HZ 26
  91. /*
  92. * Conversions and various macros
  93. * For tachometer counts, the LM63 uses 16-bit values.
  94. * For local temperature and high limit, remote critical limit and hysteresis
  95. * value, it uses signed 8-bit values with LSB = 1 degree Celsius.
  96. * For remote temperature, low and high limits, it uses signed 11-bit values
  97. * with LSB = 0.125 degree Celsius, left-justified in 16-bit registers.
  98. * For LM64 the actual remote diode temperature is 16 degree Celsius higher
  99. * than the register reading. Remote temperature setpoints have to be
  100. * adapted accordingly.
  101. */
  102. #define FAN_FROM_REG(reg) ((reg) == 0xFFFC || (reg) == 0 ? 0 : \
  103. 5400000 / (reg))
  104. #define FAN_TO_REG(val) ((val) <= 82 ? 0xFFFC : \
  105. (5400000 / (val)) & 0xFFFC)
  106. #define TEMP8_FROM_REG(reg) ((reg) * 1000)
  107. #define TEMP8_TO_REG(val) ((val) <= -128000 ? -128 : \
  108. (val) >= 127000 ? 127 : \
  109. (val) < 0 ? ((val) - 500) / 1000 : \
  110. ((val) + 500) / 1000)
  111. #define TEMP8U_TO_REG(val) ((val) <= 0 ? 0 : \
  112. (val) >= 255000 ? 255 : \
  113. ((val) + 500) / 1000)
  114. #define TEMP11_FROM_REG(reg) ((reg) / 32 * 125)
  115. #define TEMP11_TO_REG(val) ((val) <= -128000 ? 0x8000 : \
  116. (val) >= 127875 ? 0x7FE0 : \
  117. (val) < 0 ? ((val) - 62) / 125 * 32 : \
  118. ((val) + 62) / 125 * 32)
  119. #define TEMP11U_TO_REG(val) ((val) <= 0 ? 0 : \
  120. (val) >= 255875 ? 0xFFE0 : \
  121. ((val) + 62) / 125 * 32)
  122. #define HYST_TO_REG(val) ((val) <= 0 ? 0 : \
  123. (val) >= 127000 ? 127 : \
  124. ((val) + 500) / 1000)
  125. #define UPDATE_INTERVAL(max, rate) \
  126. ((1000 << (LM63_MAX_CONVRATE - (rate))) / (max))
  127. /*
  128. * Functions declaration
  129. */
  130. static int lm63_probe(struct i2c_client *client,
  131. const struct i2c_device_id *id);
  132. static int lm63_remove(struct i2c_client *client);
  133. static struct lm63_data *lm63_update_device(struct device *dev);
  134. static int lm63_detect(struct i2c_client *client, struct i2c_board_info *info);
  135. static void lm63_init_client(struct i2c_client *client);
  136. enum chips { lm63, lm64, lm96163 };
  137. /*
  138. * Driver data (common to all clients)
  139. */
  140. static const struct i2c_device_id lm63_id[] = {
  141. { "lm63", lm63 },
  142. { "lm64", lm64 },
  143. { "lm96163", lm96163 },
  144. { }
  145. };
  146. MODULE_DEVICE_TABLE(i2c, lm63_id);
  147. static struct i2c_driver lm63_driver = {
  148. .class = I2C_CLASS_HWMON,
  149. .driver = {
  150. .name = "lm63",
  151. },
  152. .probe = lm63_probe,
  153. .remove = lm63_remove,
  154. .id_table = lm63_id,
  155. .detect = lm63_detect,
  156. .address_list = normal_i2c,
  157. };
  158. /*
  159. * Client data (each client gets its own)
  160. */
  161. struct lm63_data {
  162. struct device *hwmon_dev;
  163. struct mutex update_lock;
  164. char valid; /* zero until following fields are valid */
  165. unsigned long last_updated; /* in jiffies */
  166. enum chips kind;
  167. int temp2_offset;
  168. int update_interval; /* in milliseconds */
  169. int max_convrate_hz;
  170. /* registers values */
  171. u8 config, config_fan;
  172. u16 fan[2]; /* 0: input
  173. 1: low limit */
  174. u8 pwm1_freq;
  175. u8 pwm1_value;
  176. s8 temp8[3]; /* 0: local input
  177. 1: local high limit
  178. 2: remote critical limit */
  179. s16 temp11[4]; /* 0: remote input
  180. 1: remote low limit
  181. 2: remote high limit
  182. 3: remote offset */
  183. u16 temp11u; /* remote input (unsigned) */
  184. u8 temp2_crit_hyst;
  185. u8 alarms;
  186. bool pwm_highres;
  187. bool remote_unsigned; /* true if unsigned remote upper limits */
  188. bool trutherm;
  189. };
  190. static inline int temp8_from_reg(struct lm63_data *data, int nr)
  191. {
  192. if (data->remote_unsigned)
  193. return TEMP8_FROM_REG((u8)data->temp8[nr]);
  194. return TEMP8_FROM_REG(data->temp8[nr]);
  195. }
  196. /*
  197. * Sysfs callback functions and files
  198. */
  199. static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
  200. char *buf)
  201. {
  202. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  203. struct lm63_data *data = lm63_update_device(dev);
  204. return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index]));
  205. }
  206. static ssize_t set_fan(struct device *dev, struct device_attribute *dummy,
  207. const char *buf, size_t count)
  208. {
  209. struct i2c_client *client = to_i2c_client(dev);
  210. struct lm63_data *data = i2c_get_clientdata(client);
  211. unsigned long val;
  212. int err;
  213. err = kstrtoul(buf, 10, &val);
  214. if (err)
  215. return err;
  216. mutex_lock(&data->update_lock);
  217. data->fan[1] = FAN_TO_REG(val);
  218. i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_LSB,
  219. data->fan[1] & 0xFF);
  220. i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_MSB,
  221. data->fan[1] >> 8);
  222. mutex_unlock(&data->update_lock);
  223. return count;
  224. }
  225. static ssize_t show_pwm1(struct device *dev, struct device_attribute *dummy,
  226. char *buf)
  227. {
  228. struct lm63_data *data = lm63_update_device(dev);
  229. int pwm;
  230. if (data->pwm_highres)
  231. pwm = data->pwm1_value;
  232. else
  233. pwm = data->pwm1_value >= 2 * data->pwm1_freq ?
  234. 255 : (data->pwm1_value * 255 + data->pwm1_freq) /
  235. (2 * data->pwm1_freq);
  236. return sprintf(buf, "%d\n", pwm);
  237. }
  238. static ssize_t set_pwm1(struct device *dev, struct device_attribute *dummy,
  239. const char *buf, size_t count)
  240. {
  241. struct i2c_client *client = to_i2c_client(dev);
  242. struct lm63_data *data = i2c_get_clientdata(client);
  243. unsigned long val;
  244. int err;
  245. if (!(data->config_fan & 0x20)) /* register is read-only */
  246. return -EPERM;
  247. err = kstrtoul(buf, 10, &val);
  248. if (err)
  249. return err;
  250. val = SENSORS_LIMIT(val, 0, 255);
  251. mutex_lock(&data->update_lock);
  252. data->pwm1_value = data->pwm_highres ? val :
  253. (val * data->pwm1_freq * 2 + 127) / 255;
  254. i2c_smbus_write_byte_data(client, LM63_REG_PWM_VALUE, data->pwm1_value);
  255. mutex_unlock(&data->update_lock);
  256. return count;
  257. }
  258. static ssize_t show_pwm1_enable(struct device *dev,
  259. struct device_attribute *dummy, char *buf)
  260. {
  261. struct lm63_data *data = lm63_update_device(dev);
  262. return sprintf(buf, "%d\n", data->config_fan & 0x20 ? 1 : 2);
  263. }
  264. /*
  265. * There are 8bit registers for both local(temp1) and remote(temp2) sensor.
  266. * For remote sensor registers temp2_offset has to be considered,
  267. * for local sensor it must not.
  268. * So we need separate 8bit accessors for local and remote sensor.
  269. */
  270. static ssize_t show_local_temp8(struct device *dev,
  271. struct device_attribute *devattr,
  272. char *buf)
  273. {
  274. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  275. struct lm63_data *data = lm63_update_device(dev);
  276. return sprintf(buf, "%d\n", TEMP8_FROM_REG(data->temp8[attr->index]));
  277. }
  278. static ssize_t show_remote_temp8(struct device *dev,
  279. struct device_attribute *devattr,
  280. char *buf)
  281. {
  282. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  283. struct lm63_data *data = lm63_update_device(dev);
  284. return sprintf(buf, "%d\n", temp8_from_reg(data, attr->index)
  285. + data->temp2_offset);
  286. }
  287. static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr,
  288. const char *buf, size_t count)
  289. {
  290. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  291. struct i2c_client *client = to_i2c_client(dev);
  292. struct lm63_data *data = i2c_get_clientdata(client);
  293. int nr = attr->index;
  294. int reg = nr == 2 ? LM63_REG_REMOTE_TCRIT : LM63_REG_LOCAL_HIGH;
  295. long val;
  296. int err;
  297. int temp;
  298. err = kstrtol(buf, 10, &val);
  299. if (err)
  300. return err;
  301. mutex_lock(&data->update_lock);
  302. if (nr == 2) {
  303. if (data->remote_unsigned)
  304. temp = TEMP8U_TO_REG(val - data->temp2_offset);
  305. else
  306. temp = TEMP8_TO_REG(val - data->temp2_offset);
  307. } else {
  308. temp = TEMP8_TO_REG(val);
  309. }
  310. data->temp8[nr] = temp;
  311. i2c_smbus_write_byte_data(client, reg, temp);
  312. mutex_unlock(&data->update_lock);
  313. return count;
  314. }
  315. static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
  316. char *buf)
  317. {
  318. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  319. struct lm63_data *data = lm63_update_device(dev);
  320. int nr = attr->index;
  321. int temp;
  322. if (!nr) {
  323. /*
  324. * Use unsigned temperature unless its value is zero.
  325. * If it is zero, use signed temperature.
  326. */
  327. if (data->temp11u)
  328. temp = TEMP11_FROM_REG(data->temp11u);
  329. else
  330. temp = TEMP11_FROM_REG(data->temp11[nr]);
  331. } else {
  332. if (data->remote_unsigned && nr == 2)
  333. temp = TEMP11_FROM_REG((u16)data->temp11[nr]);
  334. else
  335. temp = TEMP11_FROM_REG(data->temp11[nr]);
  336. }
  337. return sprintf(buf, "%d\n", temp + data->temp2_offset);
  338. }
  339. static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr,
  340. const char *buf, size_t count)
  341. {
  342. static const u8 reg[6] = {
  343. LM63_REG_REMOTE_LOW_MSB,
  344. LM63_REG_REMOTE_LOW_LSB,
  345. LM63_REG_REMOTE_HIGH_MSB,
  346. LM63_REG_REMOTE_HIGH_LSB,
  347. LM63_REG_REMOTE_OFFSET_MSB,
  348. LM63_REG_REMOTE_OFFSET_LSB,
  349. };
  350. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  351. struct i2c_client *client = to_i2c_client(dev);
  352. struct lm63_data *data = i2c_get_clientdata(client);
  353. long val;
  354. int err;
  355. int nr = attr->index;
  356. err = kstrtol(buf, 10, &val);
  357. if (err)
  358. return err;
  359. mutex_lock(&data->update_lock);
  360. if (data->remote_unsigned && nr == 2)
  361. data->temp11[nr] = TEMP11U_TO_REG(val - data->temp2_offset);
  362. else
  363. data->temp11[nr] = TEMP11_TO_REG(val - data->temp2_offset);
  364. i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2],
  365. data->temp11[nr] >> 8);
  366. i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2 + 1],
  367. data->temp11[nr] & 0xff);
  368. mutex_unlock(&data->update_lock);
  369. return count;
  370. }
  371. /*
  372. * Hysteresis register holds a relative value, while we want to present
  373. * an absolute to user-space
  374. */
  375. static ssize_t show_temp2_crit_hyst(struct device *dev,
  376. struct device_attribute *dummy, char *buf)
  377. {
  378. struct lm63_data *data = lm63_update_device(dev);
  379. return sprintf(buf, "%d\n", temp8_from_reg(data, 2)
  380. + data->temp2_offset
  381. - TEMP8_FROM_REG(data->temp2_crit_hyst));
  382. }
  383. /*
  384. * And now the other way around, user-space provides an absolute
  385. * hysteresis value and we have to store a relative one
  386. */
  387. static ssize_t set_temp2_crit_hyst(struct device *dev,
  388. struct device_attribute *dummy,
  389. const char *buf, size_t count)
  390. {
  391. struct i2c_client *client = to_i2c_client(dev);
  392. struct lm63_data *data = i2c_get_clientdata(client);
  393. long val;
  394. int err;
  395. long hyst;
  396. err = kstrtol(buf, 10, &val);
  397. if (err)
  398. return err;
  399. mutex_lock(&data->update_lock);
  400. hyst = temp8_from_reg(data, 2) + data->temp2_offset - val;
  401. i2c_smbus_write_byte_data(client, LM63_REG_REMOTE_TCRIT_HYST,
  402. HYST_TO_REG(hyst));
  403. mutex_unlock(&data->update_lock);
  404. return count;
  405. }
  406. /*
  407. * Set conversion rate.
  408. * client->update_lock must be held when calling this function.
  409. */
  410. static void lm63_set_convrate(struct i2c_client *client, struct lm63_data *data,
  411. unsigned int interval)
  412. {
  413. int i;
  414. unsigned int update_interval;
  415. /* Shift calculations to avoid rounding errors */
  416. interval <<= 6;
  417. /* find the nearest update rate */
  418. update_interval = (1 << (LM63_MAX_CONVRATE + 6)) * 1000
  419. / data->max_convrate_hz;
  420. for (i = 0; i < LM63_MAX_CONVRATE; i++, update_interval >>= 1)
  421. if (interval >= update_interval * 3 / 4)
  422. break;
  423. i2c_smbus_write_byte_data(client, LM63_REG_CONVRATE, i);
  424. data->update_interval = UPDATE_INTERVAL(data->max_convrate_hz, i);
  425. }
  426. static ssize_t show_update_interval(struct device *dev,
  427. struct device_attribute *attr, char *buf)
  428. {
  429. struct lm63_data *data = dev_get_drvdata(dev);
  430. return sprintf(buf, "%u\n", data->update_interval);
  431. }
  432. static ssize_t set_update_interval(struct device *dev,
  433. struct device_attribute *attr,
  434. const char *buf, size_t count)
  435. {
  436. struct i2c_client *client = to_i2c_client(dev);
  437. struct lm63_data *data = i2c_get_clientdata(client);
  438. unsigned long val;
  439. int err;
  440. err = kstrtoul(buf, 10, &val);
  441. if (err)
  442. return err;
  443. mutex_lock(&data->update_lock);
  444. lm63_set_convrate(client, data, SENSORS_LIMIT(val, 0, 100000));
  445. mutex_unlock(&data->update_lock);
  446. return count;
  447. }
  448. static ssize_t show_type(struct device *dev, struct device_attribute *attr,
  449. char *buf)
  450. {
  451. struct i2c_client *client = to_i2c_client(dev);
  452. struct lm63_data *data = i2c_get_clientdata(client);
  453. return sprintf(buf, data->trutherm ? "1\n" : "2\n");
  454. }
  455. static ssize_t set_type(struct device *dev, struct device_attribute *attr,
  456. const char *buf, size_t count)
  457. {
  458. struct i2c_client *client = to_i2c_client(dev);
  459. struct lm63_data *data = i2c_get_clientdata(client);
  460. unsigned long val;
  461. int ret;
  462. u8 reg;
  463. ret = kstrtoul(buf, 10, &val);
  464. if (ret < 0)
  465. return ret;
  466. if (val != 1 && val != 2)
  467. return -EINVAL;
  468. mutex_lock(&data->update_lock);
  469. data->trutherm = val == 1;
  470. reg = i2c_smbus_read_byte_data(client, LM96163_REG_TRUTHERM) & ~0x02;
  471. i2c_smbus_write_byte_data(client, LM96163_REG_TRUTHERM,
  472. reg | (data->trutherm ? 0x02 : 0x00));
  473. data->valid = 0;
  474. mutex_unlock(&data->update_lock);
  475. return count;
  476. }
  477. static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
  478. char *buf)
  479. {
  480. struct lm63_data *data = lm63_update_device(dev);
  481. return sprintf(buf, "%u\n", data->alarms);
  482. }
  483. static ssize_t show_alarm(struct device *dev, struct device_attribute *devattr,
  484. char *buf)
  485. {
  486. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  487. struct lm63_data *data = lm63_update_device(dev);
  488. int bitnr = attr->index;
  489. return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
  490. }
  491. static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
  492. static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan,
  493. set_fan, 1);
  494. static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm1, set_pwm1);
  495. static DEVICE_ATTR(pwm1_enable, S_IRUGO, show_pwm1_enable, NULL);
  496. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_local_temp8, NULL, 0);
  497. static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_local_temp8,
  498. set_temp8, 1);
  499. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 0);
  500. static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp11,
  501. set_temp11, 1);
  502. static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp11,
  503. set_temp11, 2);
  504. static SENSOR_DEVICE_ATTR(temp2_offset, S_IWUSR | S_IRUGO, show_temp11,
  505. set_temp11, 3);
  506. static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_remote_temp8,
  507. set_temp8, 2);
  508. static DEVICE_ATTR(temp2_crit_hyst, S_IWUSR | S_IRUGO, show_temp2_crit_hyst,
  509. set_temp2_crit_hyst);
  510. static DEVICE_ATTR(temp2_type, S_IWUSR | S_IRUGO, show_type, set_type);
  511. /* Individual alarm files */
  512. static SENSOR_DEVICE_ATTR(fan1_min_alarm, S_IRUGO, show_alarm, NULL, 0);
  513. static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
  514. static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
  515. static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
  516. static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
  517. static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
  518. /* Raw alarm file for compatibility */
  519. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  520. static DEVICE_ATTR(update_interval, S_IRUGO | S_IWUSR, show_update_interval,
  521. set_update_interval);
  522. static struct attribute *lm63_attributes[] = {
  523. &dev_attr_pwm1.attr,
  524. &dev_attr_pwm1_enable.attr,
  525. &sensor_dev_attr_temp1_input.dev_attr.attr,
  526. &sensor_dev_attr_temp2_input.dev_attr.attr,
  527. &sensor_dev_attr_temp2_min.dev_attr.attr,
  528. &sensor_dev_attr_temp1_max.dev_attr.attr,
  529. &sensor_dev_attr_temp2_max.dev_attr.attr,
  530. &sensor_dev_attr_temp2_offset.dev_attr.attr,
  531. &sensor_dev_attr_temp2_crit.dev_attr.attr,
  532. &dev_attr_temp2_crit_hyst.attr,
  533. &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
  534. &sensor_dev_attr_temp2_fault.dev_attr.attr,
  535. &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
  536. &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
  537. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  538. &dev_attr_alarms.attr,
  539. &dev_attr_update_interval.attr,
  540. NULL
  541. };
  542. /*
  543. * On LM63, temp2_crit can be set only once, which should be job
  544. * of the bootloader.
  545. * On LM64, temp2_crit can always be set.
  546. * On LM96163, temp2_crit can be set if bit 1 of the configuration
  547. * register is true.
  548. */
  549. static umode_t lm63_attribute_mode(struct kobject *kobj,
  550. struct attribute *attr, int index)
  551. {
  552. struct device *dev = container_of(kobj, struct device, kobj);
  553. struct i2c_client *client = to_i2c_client(dev);
  554. struct lm63_data *data = i2c_get_clientdata(client);
  555. if (attr == &sensor_dev_attr_temp2_crit.dev_attr.attr
  556. && (data->kind == lm64 ||
  557. (data->kind == lm96163 && (data->config & 0x02))))
  558. return attr->mode | S_IWUSR;
  559. return attr->mode;
  560. }
  561. static const struct attribute_group lm63_group = {
  562. .is_visible = lm63_attribute_mode,
  563. .attrs = lm63_attributes,
  564. };
  565. static struct attribute *lm63_attributes_fan1[] = {
  566. &sensor_dev_attr_fan1_input.dev_attr.attr,
  567. &sensor_dev_attr_fan1_min.dev_attr.attr,
  568. &sensor_dev_attr_fan1_min_alarm.dev_attr.attr,
  569. NULL
  570. };
  571. static const struct attribute_group lm63_group_fan1 = {
  572. .attrs = lm63_attributes_fan1,
  573. };
  574. /*
  575. * Real code
  576. */
  577. /* Return 0 if detection is successful, -ENODEV otherwise */
  578. static int lm63_detect(struct i2c_client *new_client,
  579. struct i2c_board_info *info)
  580. {
  581. struct i2c_adapter *adapter = new_client->adapter;
  582. u8 man_id, chip_id, reg_config1, reg_config2;
  583. u8 reg_alert_status, reg_alert_mask;
  584. int address = new_client->addr;
  585. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  586. return -ENODEV;
  587. man_id = i2c_smbus_read_byte_data(new_client, LM63_REG_MAN_ID);
  588. chip_id = i2c_smbus_read_byte_data(new_client, LM63_REG_CHIP_ID);
  589. reg_config1 = i2c_smbus_read_byte_data(new_client,
  590. LM63_REG_CONFIG1);
  591. reg_config2 = i2c_smbus_read_byte_data(new_client,
  592. LM63_REG_CONFIG2);
  593. reg_alert_status = i2c_smbus_read_byte_data(new_client,
  594. LM63_REG_ALERT_STATUS);
  595. reg_alert_mask = i2c_smbus_read_byte_data(new_client,
  596. LM63_REG_ALERT_MASK);
  597. if (man_id != 0x01 /* National Semiconductor */
  598. || (reg_config1 & 0x18) != 0x00
  599. || (reg_config2 & 0xF8) != 0x00
  600. || (reg_alert_status & 0x20) != 0x00
  601. || (reg_alert_mask & 0xA4) != 0xA4) {
  602. dev_dbg(&adapter->dev,
  603. "Unsupported chip (man_id=0x%02X, chip_id=0x%02X)\n",
  604. man_id, chip_id);
  605. return -ENODEV;
  606. }
  607. if (chip_id == 0x41 && address == 0x4c)
  608. strlcpy(info->type, "lm63", I2C_NAME_SIZE);
  609. else if (chip_id == 0x51 && (address == 0x18 || address == 0x4e))
  610. strlcpy(info->type, "lm64", I2C_NAME_SIZE);
  611. else if (chip_id == 0x49 && address == 0x4c)
  612. strlcpy(info->type, "lm96163", I2C_NAME_SIZE);
  613. else
  614. return -ENODEV;
  615. return 0;
  616. }
  617. static int lm63_probe(struct i2c_client *new_client,
  618. const struct i2c_device_id *id)
  619. {
  620. struct lm63_data *data;
  621. int err;
  622. data = kzalloc(sizeof(struct lm63_data), GFP_KERNEL);
  623. if (!data) {
  624. err = -ENOMEM;
  625. goto exit;
  626. }
  627. i2c_set_clientdata(new_client, data);
  628. data->valid = 0;
  629. mutex_init(&data->update_lock);
  630. /* Set the device type */
  631. data->kind = id->driver_data;
  632. if (data->kind == lm64)
  633. data->temp2_offset = 16000;
  634. /* Initialize chip */
  635. lm63_init_client(new_client);
  636. /* Register sysfs hooks */
  637. err = sysfs_create_group(&new_client->dev.kobj, &lm63_group);
  638. if (err)
  639. goto exit_free;
  640. if (data->config & 0x04) { /* tachometer enabled */
  641. err = sysfs_create_group(&new_client->dev.kobj,
  642. &lm63_group_fan1);
  643. if (err)
  644. goto exit_remove_files;
  645. }
  646. if (data->kind == lm96163) {
  647. err = device_create_file(&new_client->dev,
  648. &dev_attr_temp2_type);
  649. if (err)
  650. goto exit_remove_files;
  651. }
  652. data->hwmon_dev = hwmon_device_register(&new_client->dev);
  653. if (IS_ERR(data->hwmon_dev)) {
  654. err = PTR_ERR(data->hwmon_dev);
  655. goto exit_remove_files;
  656. }
  657. return 0;
  658. exit_remove_files:
  659. device_remove_file(&new_client->dev, &dev_attr_temp2_type);
  660. sysfs_remove_group(&new_client->dev.kobj, &lm63_group);
  661. sysfs_remove_group(&new_client->dev.kobj, &lm63_group_fan1);
  662. exit_free:
  663. kfree(data);
  664. exit:
  665. return err;
  666. }
  667. /*
  668. * Ideally we shouldn't have to initialize anything, since the BIOS
  669. * should have taken care of everything
  670. */
  671. static void lm63_init_client(struct i2c_client *client)
  672. {
  673. struct lm63_data *data = i2c_get_clientdata(client);
  674. u8 convrate;
  675. data->config = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG1);
  676. data->config_fan = i2c_smbus_read_byte_data(client,
  677. LM63_REG_CONFIG_FAN);
  678. /* Start converting if needed */
  679. if (data->config & 0x40) { /* standby */
  680. dev_dbg(&client->dev, "Switching to operational mode\n");
  681. data->config &= 0xA7;
  682. i2c_smbus_write_byte_data(client, LM63_REG_CONFIG1,
  683. data->config);
  684. }
  685. /* Tachometer is always enabled on LM64 */
  686. if (data->kind == lm64)
  687. data->config |= 0x04;
  688. /* We may need pwm1_freq before ever updating the client data */
  689. data->pwm1_freq = i2c_smbus_read_byte_data(client, LM63_REG_PWM_FREQ);
  690. if (data->pwm1_freq == 0)
  691. data->pwm1_freq = 1;
  692. switch (data->kind) {
  693. case lm63:
  694. case lm64:
  695. data->max_convrate_hz = LM63_MAX_CONVRATE_HZ;
  696. break;
  697. case lm96163:
  698. data->max_convrate_hz = LM96163_MAX_CONVRATE_HZ;
  699. data->trutherm
  700. = i2c_smbus_read_byte_data(client,
  701. LM96163_REG_TRUTHERM) & 0x02;
  702. break;
  703. }
  704. convrate = i2c_smbus_read_byte_data(client, LM63_REG_CONVRATE);
  705. if (unlikely(convrate > LM63_MAX_CONVRATE))
  706. convrate = LM63_MAX_CONVRATE;
  707. data->update_interval = UPDATE_INTERVAL(data->max_convrate_hz,
  708. convrate);
  709. /*
  710. * For LM96163, check if high resolution PWM
  711. * and unsigned temperature format is enabled.
  712. */
  713. if (data->kind == lm96163) {
  714. u8 config_enhanced
  715. = i2c_smbus_read_byte_data(client,
  716. LM96163_REG_CONFIG_ENHANCED);
  717. if ((config_enhanced & 0x10)
  718. && !(data->config_fan & 0x08) && data->pwm1_freq == 8)
  719. data->pwm_highres = true;
  720. if (config_enhanced & 0x08)
  721. data->remote_unsigned = true;
  722. }
  723. /* Show some debug info about the LM63 configuration */
  724. if (data->kind == lm63)
  725. dev_dbg(&client->dev, "Alert/tach pin configured for %s\n",
  726. (data->config & 0x04) ? "tachometer input" :
  727. "alert output");
  728. dev_dbg(&client->dev, "PWM clock %s kHz, output frequency %u Hz\n",
  729. (data->config_fan & 0x08) ? "1.4" : "360",
  730. ((data->config_fan & 0x08) ? 700 : 180000) / data->pwm1_freq);
  731. dev_dbg(&client->dev, "PWM output active %s, %s mode\n",
  732. (data->config_fan & 0x10) ? "low" : "high",
  733. (data->config_fan & 0x20) ? "manual" : "auto");
  734. }
  735. static int lm63_remove(struct i2c_client *client)
  736. {
  737. struct lm63_data *data = i2c_get_clientdata(client);
  738. hwmon_device_unregister(data->hwmon_dev);
  739. device_remove_file(&client->dev, &dev_attr_temp2_type);
  740. sysfs_remove_group(&client->dev.kobj, &lm63_group);
  741. sysfs_remove_group(&client->dev.kobj, &lm63_group_fan1);
  742. kfree(data);
  743. return 0;
  744. }
  745. static struct lm63_data *lm63_update_device(struct device *dev)
  746. {
  747. struct i2c_client *client = to_i2c_client(dev);
  748. struct lm63_data *data = i2c_get_clientdata(client);
  749. unsigned long next_update;
  750. mutex_lock(&data->update_lock);
  751. next_update = data->last_updated
  752. + msecs_to_jiffies(data->update_interval) + 1;
  753. if (time_after(jiffies, next_update) || !data->valid) {
  754. if (data->config & 0x04) { /* tachometer enabled */
  755. /* order matters for fan1_input */
  756. data->fan[0] = i2c_smbus_read_byte_data(client,
  757. LM63_REG_TACH_COUNT_LSB) & 0xFC;
  758. data->fan[0] |= i2c_smbus_read_byte_data(client,
  759. LM63_REG_TACH_COUNT_MSB) << 8;
  760. data->fan[1] = (i2c_smbus_read_byte_data(client,
  761. LM63_REG_TACH_LIMIT_LSB) & 0xFC)
  762. | (i2c_smbus_read_byte_data(client,
  763. LM63_REG_TACH_LIMIT_MSB) << 8);
  764. }
  765. data->pwm1_freq = i2c_smbus_read_byte_data(client,
  766. LM63_REG_PWM_FREQ);
  767. if (data->pwm1_freq == 0)
  768. data->pwm1_freq = 1;
  769. data->pwm1_value = i2c_smbus_read_byte_data(client,
  770. LM63_REG_PWM_VALUE);
  771. data->temp8[0] = i2c_smbus_read_byte_data(client,
  772. LM63_REG_LOCAL_TEMP);
  773. data->temp8[1] = i2c_smbus_read_byte_data(client,
  774. LM63_REG_LOCAL_HIGH);
  775. /* order matters for temp2_input */
  776. data->temp11[0] = i2c_smbus_read_byte_data(client,
  777. LM63_REG_REMOTE_TEMP_MSB) << 8;
  778. data->temp11[0] |= i2c_smbus_read_byte_data(client,
  779. LM63_REG_REMOTE_TEMP_LSB);
  780. data->temp11[1] = (i2c_smbus_read_byte_data(client,
  781. LM63_REG_REMOTE_LOW_MSB) << 8)
  782. | i2c_smbus_read_byte_data(client,
  783. LM63_REG_REMOTE_LOW_LSB);
  784. data->temp11[2] = (i2c_smbus_read_byte_data(client,
  785. LM63_REG_REMOTE_HIGH_MSB) << 8)
  786. | i2c_smbus_read_byte_data(client,
  787. LM63_REG_REMOTE_HIGH_LSB);
  788. data->temp11[3] = (i2c_smbus_read_byte_data(client,
  789. LM63_REG_REMOTE_OFFSET_MSB) << 8)
  790. | i2c_smbus_read_byte_data(client,
  791. LM63_REG_REMOTE_OFFSET_LSB);
  792. if (data->kind == lm96163)
  793. data->temp11u = (i2c_smbus_read_byte_data(client,
  794. LM96163_REG_REMOTE_TEMP_U_MSB) << 8)
  795. | i2c_smbus_read_byte_data(client,
  796. LM96163_REG_REMOTE_TEMP_U_LSB);
  797. data->temp8[2] = i2c_smbus_read_byte_data(client,
  798. LM63_REG_REMOTE_TCRIT);
  799. data->temp2_crit_hyst = i2c_smbus_read_byte_data(client,
  800. LM63_REG_REMOTE_TCRIT_HYST);
  801. data->alarms = i2c_smbus_read_byte_data(client,
  802. LM63_REG_ALERT_STATUS) & 0x7F;
  803. data->last_updated = jiffies;
  804. data->valid = 1;
  805. }
  806. mutex_unlock(&data->update_lock);
  807. return data;
  808. }
  809. static int __init sensors_lm63_init(void)
  810. {
  811. return i2c_add_driver(&lm63_driver);
  812. }
  813. static void __exit sensors_lm63_exit(void)
  814. {
  815. i2c_del_driver(&lm63_driver);
  816. }
  817. MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
  818. MODULE_DESCRIPTION("LM63 driver");
  819. MODULE_LICENSE("GPL");
  820. module_init(sensors_lm63_init);
  821. module_exit(sensors_lm63_exit);