lm63.c 20 KB

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