lm63.c 24 KB

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