lm63.c 26 KB

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