lm63.c 21 KB

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