lm63.c 20 KB

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