lm63.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  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. /*
  50. * Addresses to scan
  51. * Address is fully defined internally and cannot be changed.
  52. */
  53. static const unsigned short normal_i2c[] = { 0x4c, I2C_CLIENT_END };
  54. /*
  55. * Insmod parameters
  56. */
  57. I2C_CLIENT_INSMOD_1(lm63);
  58. /*
  59. * The LM63 registers
  60. */
  61. #define LM63_REG_CONFIG1 0x03
  62. #define LM63_REG_CONFIG2 0xBF
  63. #define LM63_REG_CONFIG_FAN 0x4A
  64. #define LM63_REG_TACH_COUNT_MSB 0x47
  65. #define LM63_REG_TACH_COUNT_LSB 0x46
  66. #define LM63_REG_TACH_LIMIT_MSB 0x49
  67. #define LM63_REG_TACH_LIMIT_LSB 0x48
  68. #define LM63_REG_PWM_VALUE 0x4C
  69. #define LM63_REG_PWM_FREQ 0x4D
  70. #define LM63_REG_LOCAL_TEMP 0x00
  71. #define LM63_REG_LOCAL_HIGH 0x05
  72. #define LM63_REG_REMOTE_TEMP_MSB 0x01
  73. #define LM63_REG_REMOTE_TEMP_LSB 0x10
  74. #define LM63_REG_REMOTE_OFFSET_MSB 0x11
  75. #define LM63_REG_REMOTE_OFFSET_LSB 0x12
  76. #define LM63_REG_REMOTE_HIGH_MSB 0x07
  77. #define LM63_REG_REMOTE_HIGH_LSB 0x13
  78. #define LM63_REG_REMOTE_LOW_MSB 0x08
  79. #define LM63_REG_REMOTE_LOW_LSB 0x14
  80. #define LM63_REG_REMOTE_TCRIT 0x19
  81. #define LM63_REG_REMOTE_TCRIT_HYST 0x21
  82. #define LM63_REG_ALERT_STATUS 0x02
  83. #define LM63_REG_ALERT_MASK 0x16
  84. #define LM63_REG_MAN_ID 0xFE
  85. #define LM63_REG_CHIP_ID 0xFF
  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. */
  94. #define FAN_FROM_REG(reg) ((reg) == 0xFFFC || (reg) == 0 ? 0 : \
  95. 5400000 / (reg))
  96. #define FAN_TO_REG(val) ((val) <= 82 ? 0xFFFC : \
  97. (5400000 / (val)) & 0xFFFC)
  98. #define TEMP8_FROM_REG(reg) ((reg) * 1000)
  99. #define TEMP8_TO_REG(val) ((val) <= -128000 ? -128 : \
  100. (val) >= 127000 ? 127 : \
  101. (val) < 0 ? ((val) - 500) / 1000 : \
  102. ((val) + 500) / 1000)
  103. #define TEMP11_FROM_REG(reg) ((reg) / 32 * 125)
  104. #define TEMP11_TO_REG(val) ((val) <= -128000 ? 0x8000 : \
  105. (val) >= 127875 ? 0x7FE0 : \
  106. (val) < 0 ? ((val) - 62) / 125 * 32 : \
  107. ((val) + 62) / 125 * 32)
  108. #define HYST_TO_REG(val) ((val) <= 0 ? 0 : \
  109. (val) >= 127000 ? 127 : \
  110. ((val) + 500) / 1000)
  111. /*
  112. * Functions declaration
  113. */
  114. static int lm63_probe(struct i2c_client *client,
  115. const struct i2c_device_id *id);
  116. static int lm63_remove(struct i2c_client *client);
  117. static struct lm63_data *lm63_update_device(struct device *dev);
  118. static int lm63_detect(struct i2c_client *client, struct i2c_board_info *info);
  119. static void lm63_init_client(struct i2c_client *client);
  120. /*
  121. * Driver data (common to all clients)
  122. */
  123. static const struct i2c_device_id lm63_id[] = {
  124. { "lm63", lm63 },
  125. { }
  126. };
  127. MODULE_DEVICE_TABLE(i2c, lm63_id);
  128. static struct i2c_driver lm63_driver = {
  129. .class = I2C_CLASS_HWMON,
  130. .driver = {
  131. .name = "lm63",
  132. },
  133. .probe = lm63_probe,
  134. .remove = lm63_remove,
  135. .id_table = lm63_id,
  136. .detect = lm63_detect,
  137. .address_list = normal_i2c,
  138. };
  139. /*
  140. * Client data (each client gets its own)
  141. */
  142. struct lm63_data {
  143. struct device *hwmon_dev;
  144. struct mutex update_lock;
  145. char valid; /* zero until following fields are valid */
  146. unsigned long last_updated; /* in jiffies */
  147. /* registers values */
  148. u8 config, config_fan;
  149. u16 fan[2]; /* 0: input
  150. 1: low limit */
  151. u8 pwm1_freq;
  152. u8 pwm1_value;
  153. s8 temp8[3]; /* 0: local input
  154. 1: local high limit
  155. 2: remote critical limit */
  156. s16 temp11[3]; /* 0: remote input
  157. 1: remote low limit
  158. 2: remote high limit */
  159. u8 temp2_crit_hyst;
  160. u8 alarms;
  161. };
  162. /*
  163. * Sysfs callback functions and files
  164. */
  165. static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
  166. char *buf)
  167. {
  168. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  169. struct lm63_data *data = lm63_update_device(dev);
  170. return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index]));
  171. }
  172. static ssize_t set_fan(struct device *dev, struct device_attribute *dummy,
  173. const char *buf, size_t count)
  174. {
  175. struct i2c_client *client = to_i2c_client(dev);
  176. struct lm63_data *data = i2c_get_clientdata(client);
  177. unsigned long val = simple_strtoul(buf, NULL, 10);
  178. mutex_lock(&data->update_lock);
  179. data->fan[1] = FAN_TO_REG(val);
  180. i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_LSB,
  181. data->fan[1] & 0xFF);
  182. i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_MSB,
  183. data->fan[1] >> 8);
  184. mutex_unlock(&data->update_lock);
  185. return count;
  186. }
  187. static ssize_t show_pwm1(struct device *dev, struct device_attribute *dummy,
  188. char *buf)
  189. {
  190. struct lm63_data *data = lm63_update_device(dev);
  191. return sprintf(buf, "%d\n", data->pwm1_value >= 2 * data->pwm1_freq ?
  192. 255 : (data->pwm1_value * 255 + data->pwm1_freq) /
  193. (2 * data->pwm1_freq));
  194. }
  195. static ssize_t set_pwm1(struct device *dev, struct device_attribute *dummy,
  196. const char *buf, size_t count)
  197. {
  198. struct i2c_client *client = to_i2c_client(dev);
  199. struct lm63_data *data = i2c_get_clientdata(client);
  200. unsigned long val;
  201. if (!(data->config_fan & 0x20)) /* register is read-only */
  202. return -EPERM;
  203. val = simple_strtoul(buf, NULL, 10);
  204. mutex_lock(&data->update_lock);
  205. data->pwm1_value = val <= 0 ? 0 :
  206. val >= 255 ? 2 * data->pwm1_freq :
  207. (val * data->pwm1_freq * 2 + 127) / 255;
  208. i2c_smbus_write_byte_data(client, LM63_REG_PWM_VALUE, data->pwm1_value);
  209. mutex_unlock(&data->update_lock);
  210. return count;
  211. }
  212. static ssize_t show_pwm1_enable(struct device *dev, struct device_attribute *dummy,
  213. char *buf)
  214. {
  215. struct lm63_data *data = lm63_update_device(dev);
  216. return sprintf(buf, "%d\n", data->config_fan & 0x20 ? 1 : 2);
  217. }
  218. static ssize_t show_temp8(struct device *dev, struct device_attribute *devattr,
  219. char *buf)
  220. {
  221. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  222. struct lm63_data *data = lm63_update_device(dev);
  223. return sprintf(buf, "%d\n", TEMP8_FROM_REG(data->temp8[attr->index]));
  224. }
  225. static ssize_t set_temp8(struct device *dev, struct device_attribute *dummy,
  226. const char *buf, size_t count)
  227. {
  228. struct i2c_client *client = to_i2c_client(dev);
  229. struct lm63_data *data = i2c_get_clientdata(client);
  230. long val = simple_strtol(buf, NULL, 10);
  231. mutex_lock(&data->update_lock);
  232. data->temp8[1] = TEMP8_TO_REG(val);
  233. i2c_smbus_write_byte_data(client, LM63_REG_LOCAL_HIGH, data->temp8[1]);
  234. mutex_unlock(&data->update_lock);
  235. return count;
  236. }
  237. static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
  238. char *buf)
  239. {
  240. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  241. struct lm63_data *data = lm63_update_device(dev);
  242. return sprintf(buf, "%d\n", TEMP11_FROM_REG(data->temp11[attr->index]));
  243. }
  244. static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr,
  245. const char *buf, size_t count)
  246. {
  247. static const u8 reg[4] = {
  248. LM63_REG_REMOTE_LOW_MSB,
  249. LM63_REG_REMOTE_LOW_LSB,
  250. LM63_REG_REMOTE_HIGH_MSB,
  251. LM63_REG_REMOTE_HIGH_LSB,
  252. };
  253. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  254. struct i2c_client *client = to_i2c_client(dev);
  255. struct lm63_data *data = i2c_get_clientdata(client);
  256. long val = simple_strtol(buf, NULL, 10);
  257. int nr = attr->index;
  258. mutex_lock(&data->update_lock);
  259. data->temp11[nr] = TEMP11_TO_REG(val);
  260. i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2],
  261. data->temp11[nr] >> 8);
  262. i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2 + 1],
  263. data->temp11[nr] & 0xff);
  264. mutex_unlock(&data->update_lock);
  265. return count;
  266. }
  267. /* Hysteresis register holds a relative value, while we want to present
  268. an absolute to user-space */
  269. static ssize_t show_temp2_crit_hyst(struct device *dev, struct device_attribute *dummy,
  270. char *buf)
  271. {
  272. struct lm63_data *data = lm63_update_device(dev);
  273. return sprintf(buf, "%d\n", TEMP8_FROM_REG(data->temp8[2])
  274. - TEMP8_FROM_REG(data->temp2_crit_hyst));
  275. }
  276. /* And now the other way around, user-space provides an absolute
  277. hysteresis value and we have to store a relative one */
  278. static ssize_t set_temp2_crit_hyst(struct device *dev, struct device_attribute *dummy,
  279. const char *buf, size_t count)
  280. {
  281. struct i2c_client *client = to_i2c_client(dev);
  282. struct lm63_data *data = i2c_get_clientdata(client);
  283. long val = simple_strtol(buf, NULL, 10);
  284. long hyst;
  285. mutex_lock(&data->update_lock);
  286. hyst = TEMP8_FROM_REG(data->temp8[2]) - val;
  287. i2c_smbus_write_byte_data(client, LM63_REG_REMOTE_TCRIT_HYST,
  288. HYST_TO_REG(hyst));
  289. mutex_unlock(&data->update_lock);
  290. return count;
  291. }
  292. static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
  293. char *buf)
  294. {
  295. struct lm63_data *data = lm63_update_device(dev);
  296. return sprintf(buf, "%u\n", data->alarms);
  297. }
  298. static ssize_t show_alarm(struct device *dev, struct device_attribute *devattr,
  299. char *buf)
  300. {
  301. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  302. struct lm63_data *data = lm63_update_device(dev);
  303. int bitnr = attr->index;
  304. return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
  305. }
  306. static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
  307. static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan,
  308. set_fan, 1);
  309. static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm1, set_pwm1);
  310. static DEVICE_ATTR(pwm1_enable, S_IRUGO, show_pwm1_enable, NULL);
  311. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp8, NULL, 0);
  312. static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp8,
  313. set_temp8, 1);
  314. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 0);
  315. static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp11,
  316. set_temp11, 1);
  317. static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp11,
  318. set_temp11, 2);
  319. static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp8, NULL, 2);
  320. static DEVICE_ATTR(temp2_crit_hyst, S_IWUSR | S_IRUGO, show_temp2_crit_hyst,
  321. set_temp2_crit_hyst);
  322. /* Individual alarm files */
  323. static SENSOR_DEVICE_ATTR(fan1_min_alarm, S_IRUGO, show_alarm, NULL, 0);
  324. static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
  325. static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
  326. static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
  327. static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
  328. static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
  329. /* Raw alarm file for compatibility */
  330. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  331. static struct attribute *lm63_attributes[] = {
  332. &dev_attr_pwm1.attr,
  333. &dev_attr_pwm1_enable.attr,
  334. &sensor_dev_attr_temp1_input.dev_attr.attr,
  335. &sensor_dev_attr_temp2_input.dev_attr.attr,
  336. &sensor_dev_attr_temp2_min.dev_attr.attr,
  337. &sensor_dev_attr_temp1_max.dev_attr.attr,
  338. &sensor_dev_attr_temp2_max.dev_attr.attr,
  339. &sensor_dev_attr_temp2_crit.dev_attr.attr,
  340. &dev_attr_temp2_crit_hyst.attr,
  341. &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
  342. &sensor_dev_attr_temp2_fault.dev_attr.attr,
  343. &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
  344. &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
  345. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  346. &dev_attr_alarms.attr,
  347. NULL
  348. };
  349. static const struct attribute_group lm63_group = {
  350. .attrs = lm63_attributes,
  351. };
  352. static struct attribute *lm63_attributes_fan1[] = {
  353. &sensor_dev_attr_fan1_input.dev_attr.attr,
  354. &sensor_dev_attr_fan1_min.dev_attr.attr,
  355. &sensor_dev_attr_fan1_min_alarm.dev_attr.attr,
  356. NULL
  357. };
  358. static const struct attribute_group lm63_group_fan1 = {
  359. .attrs = lm63_attributes_fan1,
  360. };
  361. /*
  362. * Real code
  363. */
  364. /* Return 0 if detection is successful, -ENODEV otherwise */
  365. static int lm63_detect(struct i2c_client *new_client,
  366. struct i2c_board_info *info)
  367. {
  368. struct i2c_adapter *adapter = new_client->adapter;
  369. u8 man_id, chip_id, reg_config1, reg_config2;
  370. u8 reg_alert_status, reg_alert_mask;
  371. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  372. return -ENODEV;
  373. man_id = i2c_smbus_read_byte_data(new_client, LM63_REG_MAN_ID);
  374. chip_id = i2c_smbus_read_byte_data(new_client, LM63_REG_CHIP_ID);
  375. reg_config1 = i2c_smbus_read_byte_data(new_client,
  376. LM63_REG_CONFIG1);
  377. reg_config2 = i2c_smbus_read_byte_data(new_client,
  378. LM63_REG_CONFIG2);
  379. reg_alert_status = i2c_smbus_read_byte_data(new_client,
  380. LM63_REG_ALERT_STATUS);
  381. reg_alert_mask = i2c_smbus_read_byte_data(new_client,
  382. LM63_REG_ALERT_MASK);
  383. if (man_id != 0x01 /* National Semiconductor */
  384. || chip_id != 0x41 /* LM63 */
  385. || (reg_config1 & 0x18) != 0x00
  386. || (reg_config2 & 0xF8) != 0x00
  387. || (reg_alert_status & 0x20) != 0x00
  388. || (reg_alert_mask & 0xA4) != 0xA4) {
  389. dev_dbg(&adapter->dev,
  390. "Unsupported chip (man_id=0x%02X, chip_id=0x%02X)\n",
  391. man_id, chip_id);
  392. return -ENODEV;
  393. }
  394. strlcpy(info->type, "lm63", I2C_NAME_SIZE);
  395. return 0;
  396. }
  397. static int lm63_probe(struct i2c_client *new_client,
  398. const struct i2c_device_id *id)
  399. {
  400. struct lm63_data *data;
  401. int err;
  402. data = kzalloc(sizeof(struct lm63_data), GFP_KERNEL);
  403. if (!data) {
  404. err = -ENOMEM;
  405. goto exit;
  406. }
  407. i2c_set_clientdata(new_client, data);
  408. data->valid = 0;
  409. mutex_init(&data->update_lock);
  410. /* Initialize the LM63 chip */
  411. lm63_init_client(new_client);
  412. /* Register sysfs hooks */
  413. if ((err = sysfs_create_group(&new_client->dev.kobj,
  414. &lm63_group)))
  415. goto exit_free;
  416. if (data->config & 0x04) { /* tachometer enabled */
  417. if ((err = sysfs_create_group(&new_client->dev.kobj,
  418. &lm63_group_fan1)))
  419. goto exit_remove_files;
  420. }
  421. data->hwmon_dev = hwmon_device_register(&new_client->dev);
  422. if (IS_ERR(data->hwmon_dev)) {
  423. err = PTR_ERR(data->hwmon_dev);
  424. goto exit_remove_files;
  425. }
  426. return 0;
  427. exit_remove_files:
  428. sysfs_remove_group(&new_client->dev.kobj, &lm63_group);
  429. sysfs_remove_group(&new_client->dev.kobj, &lm63_group_fan1);
  430. exit_free:
  431. kfree(data);
  432. exit:
  433. return err;
  434. }
  435. /* Idealy we shouldn't have to initialize anything, since the BIOS
  436. should have taken care of everything */
  437. static void lm63_init_client(struct i2c_client *client)
  438. {
  439. struct lm63_data *data = i2c_get_clientdata(client);
  440. data->config = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG1);
  441. data->config_fan = i2c_smbus_read_byte_data(client,
  442. LM63_REG_CONFIG_FAN);
  443. /* Start converting if needed */
  444. if (data->config & 0x40) { /* standby */
  445. dev_dbg(&client->dev, "Switching to operational mode\n");
  446. data->config &= 0xA7;
  447. i2c_smbus_write_byte_data(client, LM63_REG_CONFIG1,
  448. data->config);
  449. }
  450. /* We may need pwm1_freq before ever updating the client data */
  451. data->pwm1_freq = i2c_smbus_read_byte_data(client, LM63_REG_PWM_FREQ);
  452. if (data->pwm1_freq == 0)
  453. data->pwm1_freq = 1;
  454. /* Show some debug info about the LM63 configuration */
  455. dev_dbg(&client->dev, "Alert/tach pin configured for %s\n",
  456. (data->config & 0x04) ? "tachometer input" :
  457. "alert output");
  458. dev_dbg(&client->dev, "PWM clock %s kHz, output frequency %u Hz\n",
  459. (data->config_fan & 0x08) ? "1.4" : "360",
  460. ((data->config_fan & 0x08) ? 700 : 180000) / data->pwm1_freq);
  461. dev_dbg(&client->dev, "PWM output active %s, %s mode\n",
  462. (data->config_fan & 0x10) ? "low" : "high",
  463. (data->config_fan & 0x20) ? "manual" : "auto");
  464. }
  465. static int lm63_remove(struct i2c_client *client)
  466. {
  467. struct lm63_data *data = i2c_get_clientdata(client);
  468. hwmon_device_unregister(data->hwmon_dev);
  469. sysfs_remove_group(&client->dev.kobj, &lm63_group);
  470. sysfs_remove_group(&client->dev.kobj, &lm63_group_fan1);
  471. kfree(data);
  472. return 0;
  473. }
  474. static struct lm63_data *lm63_update_device(struct device *dev)
  475. {
  476. struct i2c_client *client = to_i2c_client(dev);
  477. struct lm63_data *data = i2c_get_clientdata(client);
  478. mutex_lock(&data->update_lock);
  479. if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
  480. if (data->config & 0x04) { /* tachometer enabled */
  481. /* order matters for fan1_input */
  482. data->fan[0] = i2c_smbus_read_byte_data(client,
  483. LM63_REG_TACH_COUNT_LSB) & 0xFC;
  484. data->fan[0] |= i2c_smbus_read_byte_data(client,
  485. LM63_REG_TACH_COUNT_MSB) << 8;
  486. data->fan[1] = (i2c_smbus_read_byte_data(client,
  487. LM63_REG_TACH_LIMIT_LSB) & 0xFC)
  488. | (i2c_smbus_read_byte_data(client,
  489. LM63_REG_TACH_LIMIT_MSB) << 8);
  490. }
  491. data->pwm1_freq = i2c_smbus_read_byte_data(client,
  492. LM63_REG_PWM_FREQ);
  493. if (data->pwm1_freq == 0)
  494. data->pwm1_freq = 1;
  495. data->pwm1_value = i2c_smbus_read_byte_data(client,
  496. LM63_REG_PWM_VALUE);
  497. data->temp8[0] = i2c_smbus_read_byte_data(client,
  498. LM63_REG_LOCAL_TEMP);
  499. data->temp8[1] = i2c_smbus_read_byte_data(client,
  500. LM63_REG_LOCAL_HIGH);
  501. /* order matters for temp2_input */
  502. data->temp11[0] = i2c_smbus_read_byte_data(client,
  503. LM63_REG_REMOTE_TEMP_MSB) << 8;
  504. data->temp11[0] |= i2c_smbus_read_byte_data(client,
  505. LM63_REG_REMOTE_TEMP_LSB);
  506. data->temp11[1] = (i2c_smbus_read_byte_data(client,
  507. LM63_REG_REMOTE_LOW_MSB) << 8)
  508. | i2c_smbus_read_byte_data(client,
  509. LM63_REG_REMOTE_LOW_LSB);
  510. data->temp11[2] = (i2c_smbus_read_byte_data(client,
  511. LM63_REG_REMOTE_HIGH_MSB) << 8)
  512. | i2c_smbus_read_byte_data(client,
  513. LM63_REG_REMOTE_HIGH_LSB);
  514. data->temp8[2] = i2c_smbus_read_byte_data(client,
  515. LM63_REG_REMOTE_TCRIT);
  516. data->temp2_crit_hyst = i2c_smbus_read_byte_data(client,
  517. LM63_REG_REMOTE_TCRIT_HYST);
  518. data->alarms = i2c_smbus_read_byte_data(client,
  519. LM63_REG_ALERT_STATUS) & 0x7F;
  520. data->last_updated = jiffies;
  521. data->valid = 1;
  522. }
  523. mutex_unlock(&data->update_lock);
  524. return data;
  525. }
  526. static int __init sensors_lm63_init(void)
  527. {
  528. return i2c_add_driver(&lm63_driver);
  529. }
  530. static void __exit sensors_lm63_exit(void)
  531. {
  532. i2c_del_driver(&lm63_driver);
  533. }
  534. MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
  535. MODULE_DESCRIPTION("LM63 driver");
  536. MODULE_LICENSE("GPL");
  537. module_init(sensors_lm63_init);
  538. module_exit(sensors_lm63_exit);