lm63.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  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;
  181. int err;
  182. err = kstrtoul(buf, 10, &val);
  183. if (err)
  184. return err;
  185. mutex_lock(&data->update_lock);
  186. data->fan[1] = FAN_TO_REG(val);
  187. i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_LSB,
  188. data->fan[1] & 0xFF);
  189. i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_MSB,
  190. data->fan[1] >> 8);
  191. mutex_unlock(&data->update_lock);
  192. return count;
  193. }
  194. static ssize_t show_pwm1(struct device *dev, struct device_attribute *dummy,
  195. char *buf)
  196. {
  197. struct lm63_data *data = lm63_update_device(dev);
  198. return sprintf(buf, "%d\n", data->pwm1_value >= 2 * data->pwm1_freq ?
  199. 255 : (data->pwm1_value * 255 + data->pwm1_freq) /
  200. (2 * data->pwm1_freq));
  201. }
  202. static ssize_t set_pwm1(struct device *dev, struct device_attribute *dummy,
  203. const char *buf, size_t count)
  204. {
  205. struct i2c_client *client = to_i2c_client(dev);
  206. struct lm63_data *data = i2c_get_clientdata(client);
  207. unsigned long val;
  208. int err;
  209. if (!(data->config_fan & 0x20)) /* register is read-only */
  210. return -EPERM;
  211. err = kstrtoul(buf, 10, &val);
  212. if (err)
  213. return err;
  214. mutex_lock(&data->update_lock);
  215. data->pwm1_value = val <= 0 ? 0 :
  216. val >= 255 ? 2 * data->pwm1_freq :
  217. (val * data->pwm1_freq * 2 + 127) / 255;
  218. i2c_smbus_write_byte_data(client, LM63_REG_PWM_VALUE, data->pwm1_value);
  219. mutex_unlock(&data->update_lock);
  220. return count;
  221. }
  222. static ssize_t show_pwm1_enable(struct device *dev,
  223. struct device_attribute *dummy, char *buf)
  224. {
  225. struct lm63_data *data = lm63_update_device(dev);
  226. return sprintf(buf, "%d\n", data->config_fan & 0x20 ? 1 : 2);
  227. }
  228. /*
  229. * There are 8bit registers for both local(temp1) and remote(temp2) sensor.
  230. * For remote sensor registers temp2_offset has to be considered,
  231. * for local sensor it must not.
  232. * So we need separate 8bit accessors for local and remote sensor.
  233. */
  234. static ssize_t show_local_temp8(struct device *dev,
  235. 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", TEMP8_FROM_REG(data->temp8[attr->index]));
  241. }
  242. static ssize_t show_remote_temp8(struct device *dev,
  243. struct device_attribute *devattr,
  244. char *buf)
  245. {
  246. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  247. struct lm63_data *data = lm63_update_device(dev);
  248. return sprintf(buf, "%d\n", TEMP8_FROM_REG(data->temp8[attr->index])
  249. + data->temp2_offset);
  250. }
  251. static ssize_t set_local_temp8(struct device *dev,
  252. struct device_attribute *dummy,
  253. const char *buf, size_t count)
  254. {
  255. struct i2c_client *client = to_i2c_client(dev);
  256. struct lm63_data *data = i2c_get_clientdata(client);
  257. long val;
  258. int err;
  259. err = kstrtol(buf, 10, &val);
  260. if (err)
  261. return err;
  262. mutex_lock(&data->update_lock);
  263. data->temp8[1] = TEMP8_TO_REG(val);
  264. i2c_smbus_write_byte_data(client, LM63_REG_LOCAL_HIGH, data->temp8[1]);
  265. mutex_unlock(&data->update_lock);
  266. return count;
  267. }
  268. static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
  269. char *buf)
  270. {
  271. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  272. struct lm63_data *data = lm63_update_device(dev);
  273. return sprintf(buf, "%d\n", TEMP11_FROM_REG(data->temp11[attr->index])
  274. + data->temp2_offset);
  275. }
  276. static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr,
  277. const char *buf, size_t count)
  278. {
  279. static const u8 reg[4] = {
  280. LM63_REG_REMOTE_LOW_MSB,
  281. LM63_REG_REMOTE_LOW_LSB,
  282. LM63_REG_REMOTE_HIGH_MSB,
  283. LM63_REG_REMOTE_HIGH_LSB,
  284. };
  285. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  286. struct i2c_client *client = to_i2c_client(dev);
  287. struct lm63_data *data = i2c_get_clientdata(client);
  288. long val;
  289. int err;
  290. int nr = attr->index;
  291. err = kstrtol(buf, 10, &val);
  292. if (err)
  293. return err;
  294. mutex_lock(&data->update_lock);
  295. data->temp11[nr] = TEMP11_TO_REG(val - data->temp2_offset);
  296. i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2],
  297. data->temp11[nr] >> 8);
  298. i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2 + 1],
  299. data->temp11[nr] & 0xff);
  300. mutex_unlock(&data->update_lock);
  301. return count;
  302. }
  303. /*
  304. * Hysteresis register holds a relative value, while we want to present
  305. * an absolute to user-space
  306. */
  307. static ssize_t show_temp2_crit_hyst(struct device *dev,
  308. struct device_attribute *dummy, char *buf)
  309. {
  310. struct lm63_data *data = lm63_update_device(dev);
  311. return sprintf(buf, "%d\n", TEMP8_FROM_REG(data->temp8[2])
  312. + data->temp2_offset
  313. - TEMP8_FROM_REG(data->temp2_crit_hyst));
  314. }
  315. /*
  316. * And now the other way around, user-space provides an absolute
  317. * hysteresis value and we have to store a relative one
  318. */
  319. static ssize_t set_temp2_crit_hyst(struct device *dev,
  320. struct device_attribute *dummy,
  321. const char *buf, size_t count)
  322. {
  323. struct i2c_client *client = to_i2c_client(dev);
  324. struct lm63_data *data = i2c_get_clientdata(client);
  325. long val;
  326. int err;
  327. long hyst;
  328. err = kstrtol(buf, 10, &val);
  329. if (err)
  330. return err;
  331. mutex_lock(&data->update_lock);
  332. hyst = TEMP8_FROM_REG(data->temp8[2]) + data->temp2_offset - val;
  333. i2c_smbus_write_byte_data(client, LM63_REG_REMOTE_TCRIT_HYST,
  334. HYST_TO_REG(hyst));
  335. mutex_unlock(&data->update_lock);
  336. return count;
  337. }
  338. static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
  339. char *buf)
  340. {
  341. struct lm63_data *data = lm63_update_device(dev);
  342. return sprintf(buf, "%u\n", data->alarms);
  343. }
  344. static ssize_t show_alarm(struct device *dev, struct device_attribute *devattr,
  345. char *buf)
  346. {
  347. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  348. struct lm63_data *data = lm63_update_device(dev);
  349. int bitnr = attr->index;
  350. return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
  351. }
  352. static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
  353. static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan,
  354. set_fan, 1);
  355. static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm1, set_pwm1);
  356. static DEVICE_ATTR(pwm1_enable, S_IRUGO, show_pwm1_enable, NULL);
  357. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_local_temp8, NULL, 0);
  358. static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_local_temp8,
  359. set_local_temp8, 1);
  360. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 0);
  361. static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp11,
  362. set_temp11, 1);
  363. static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp11,
  364. set_temp11, 2);
  365. /*
  366. * On LM63, temp2_crit can be set only once, which should be job
  367. * of the bootloader.
  368. */
  369. static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_remote_temp8,
  370. NULL, 2);
  371. static DEVICE_ATTR(temp2_crit_hyst, S_IWUSR | S_IRUGO, show_temp2_crit_hyst,
  372. set_temp2_crit_hyst);
  373. /* Individual alarm files */
  374. static SENSOR_DEVICE_ATTR(fan1_min_alarm, S_IRUGO, show_alarm, NULL, 0);
  375. static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
  376. static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
  377. static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
  378. static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
  379. static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
  380. /* Raw alarm file for compatibility */
  381. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  382. static struct attribute *lm63_attributes[] = {
  383. &dev_attr_pwm1.attr,
  384. &dev_attr_pwm1_enable.attr,
  385. &sensor_dev_attr_temp1_input.dev_attr.attr,
  386. &sensor_dev_attr_temp2_input.dev_attr.attr,
  387. &sensor_dev_attr_temp2_min.dev_attr.attr,
  388. &sensor_dev_attr_temp1_max.dev_attr.attr,
  389. &sensor_dev_attr_temp2_max.dev_attr.attr,
  390. &sensor_dev_attr_temp2_crit.dev_attr.attr,
  391. &dev_attr_temp2_crit_hyst.attr,
  392. &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
  393. &sensor_dev_attr_temp2_fault.dev_attr.attr,
  394. &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
  395. &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
  396. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  397. &dev_attr_alarms.attr,
  398. NULL
  399. };
  400. static const struct attribute_group lm63_group = {
  401. .attrs = lm63_attributes,
  402. };
  403. static struct attribute *lm63_attributes_fan1[] = {
  404. &sensor_dev_attr_fan1_input.dev_attr.attr,
  405. &sensor_dev_attr_fan1_min.dev_attr.attr,
  406. &sensor_dev_attr_fan1_min_alarm.dev_attr.attr,
  407. NULL
  408. };
  409. static const struct attribute_group lm63_group_fan1 = {
  410. .attrs = lm63_attributes_fan1,
  411. };
  412. /*
  413. * Real code
  414. */
  415. /* Return 0 if detection is successful, -ENODEV otherwise */
  416. static int lm63_detect(struct i2c_client *new_client,
  417. struct i2c_board_info *info)
  418. {
  419. struct i2c_adapter *adapter = new_client->adapter;
  420. u8 man_id, chip_id, reg_config1, reg_config2;
  421. u8 reg_alert_status, reg_alert_mask;
  422. int address = new_client->addr;
  423. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  424. return -ENODEV;
  425. man_id = i2c_smbus_read_byte_data(new_client, LM63_REG_MAN_ID);
  426. chip_id = i2c_smbus_read_byte_data(new_client, LM63_REG_CHIP_ID);
  427. reg_config1 = i2c_smbus_read_byte_data(new_client,
  428. LM63_REG_CONFIG1);
  429. reg_config2 = i2c_smbus_read_byte_data(new_client,
  430. LM63_REG_CONFIG2);
  431. reg_alert_status = i2c_smbus_read_byte_data(new_client,
  432. LM63_REG_ALERT_STATUS);
  433. reg_alert_mask = i2c_smbus_read_byte_data(new_client,
  434. LM63_REG_ALERT_MASK);
  435. if (man_id != 0x01 /* National Semiconductor */
  436. || (reg_config1 & 0x18) != 0x00
  437. || (reg_config2 & 0xF8) != 0x00
  438. || (reg_alert_status & 0x20) != 0x00
  439. || (reg_alert_mask & 0xA4) != 0xA4) {
  440. dev_dbg(&adapter->dev,
  441. "Unsupported chip (man_id=0x%02X, chip_id=0x%02X)\n",
  442. man_id, chip_id);
  443. return -ENODEV;
  444. }
  445. if (chip_id == 0x41 && address == 0x4c)
  446. strlcpy(info->type, "lm63", I2C_NAME_SIZE);
  447. else if (chip_id == 0x51 && (address == 0x18 || address == 0x4e))
  448. strlcpy(info->type, "lm64", I2C_NAME_SIZE);
  449. else
  450. return -ENODEV;
  451. return 0;
  452. }
  453. static int lm63_probe(struct i2c_client *new_client,
  454. const struct i2c_device_id *id)
  455. {
  456. struct lm63_data *data;
  457. int err;
  458. data = kzalloc(sizeof(struct lm63_data), GFP_KERNEL);
  459. if (!data) {
  460. err = -ENOMEM;
  461. goto exit;
  462. }
  463. i2c_set_clientdata(new_client, data);
  464. data->valid = 0;
  465. mutex_init(&data->update_lock);
  466. /* Set the device type */
  467. data->kind = id->driver_data;
  468. if (data->kind == lm64)
  469. data->temp2_offset = 16000;
  470. /* Initialize chip */
  471. lm63_init_client(new_client);
  472. /* Register sysfs hooks */
  473. err = sysfs_create_group(&new_client->dev.kobj, &lm63_group);
  474. if (err)
  475. goto exit_free;
  476. if (data->config & 0x04) { /* tachometer enabled */
  477. err = sysfs_create_group(&new_client->dev.kobj,
  478. &lm63_group_fan1);
  479. if (err)
  480. goto exit_remove_files;
  481. }
  482. data->hwmon_dev = hwmon_device_register(&new_client->dev);
  483. if (IS_ERR(data->hwmon_dev)) {
  484. err = PTR_ERR(data->hwmon_dev);
  485. goto exit_remove_files;
  486. }
  487. return 0;
  488. exit_remove_files:
  489. sysfs_remove_group(&new_client->dev.kobj, &lm63_group);
  490. sysfs_remove_group(&new_client->dev.kobj, &lm63_group_fan1);
  491. exit_free:
  492. kfree(data);
  493. exit:
  494. return err;
  495. }
  496. /*
  497. * Ideally we shouldn't have to initialize anything, since the BIOS
  498. * should have taken care of everything
  499. */
  500. static void lm63_init_client(struct i2c_client *client)
  501. {
  502. struct lm63_data *data = i2c_get_clientdata(client);
  503. data->config = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG1);
  504. data->config_fan = i2c_smbus_read_byte_data(client,
  505. LM63_REG_CONFIG_FAN);
  506. /* Start converting if needed */
  507. if (data->config & 0x40) { /* standby */
  508. dev_dbg(&client->dev, "Switching to operational mode\n");
  509. data->config &= 0xA7;
  510. i2c_smbus_write_byte_data(client, LM63_REG_CONFIG1,
  511. data->config);
  512. }
  513. /* We may need pwm1_freq before ever updating the client data */
  514. data->pwm1_freq = i2c_smbus_read_byte_data(client, LM63_REG_PWM_FREQ);
  515. if (data->pwm1_freq == 0)
  516. data->pwm1_freq = 1;
  517. /* Show some debug info about the LM63 configuration */
  518. dev_dbg(&client->dev, "Alert/tach pin configured for %s\n",
  519. (data->config & 0x04) ? "tachometer input" :
  520. "alert output");
  521. dev_dbg(&client->dev, "PWM clock %s kHz, output frequency %u Hz\n",
  522. (data->config_fan & 0x08) ? "1.4" : "360",
  523. ((data->config_fan & 0x08) ? 700 : 180000) / data->pwm1_freq);
  524. dev_dbg(&client->dev, "PWM output active %s, %s mode\n",
  525. (data->config_fan & 0x10) ? "low" : "high",
  526. (data->config_fan & 0x20) ? "manual" : "auto");
  527. }
  528. static int lm63_remove(struct i2c_client *client)
  529. {
  530. struct lm63_data *data = i2c_get_clientdata(client);
  531. hwmon_device_unregister(data->hwmon_dev);
  532. sysfs_remove_group(&client->dev.kobj, &lm63_group);
  533. sysfs_remove_group(&client->dev.kobj, &lm63_group_fan1);
  534. kfree(data);
  535. return 0;
  536. }
  537. static struct lm63_data *lm63_update_device(struct device *dev)
  538. {
  539. struct i2c_client *client = to_i2c_client(dev);
  540. struct lm63_data *data = i2c_get_clientdata(client);
  541. mutex_lock(&data->update_lock);
  542. if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
  543. if (data->config & 0x04) { /* tachometer enabled */
  544. /* order matters for fan1_input */
  545. data->fan[0] = i2c_smbus_read_byte_data(client,
  546. LM63_REG_TACH_COUNT_LSB) & 0xFC;
  547. data->fan[0] |= i2c_smbus_read_byte_data(client,
  548. LM63_REG_TACH_COUNT_MSB) << 8;
  549. data->fan[1] = (i2c_smbus_read_byte_data(client,
  550. LM63_REG_TACH_LIMIT_LSB) & 0xFC)
  551. | (i2c_smbus_read_byte_data(client,
  552. LM63_REG_TACH_LIMIT_MSB) << 8);
  553. }
  554. data->pwm1_freq = i2c_smbus_read_byte_data(client,
  555. LM63_REG_PWM_FREQ);
  556. if (data->pwm1_freq == 0)
  557. data->pwm1_freq = 1;
  558. data->pwm1_value = i2c_smbus_read_byte_data(client,
  559. LM63_REG_PWM_VALUE);
  560. data->temp8[0] = i2c_smbus_read_byte_data(client,
  561. LM63_REG_LOCAL_TEMP);
  562. data->temp8[1] = i2c_smbus_read_byte_data(client,
  563. LM63_REG_LOCAL_HIGH);
  564. /* order matters for temp2_input */
  565. data->temp11[0] = i2c_smbus_read_byte_data(client,
  566. LM63_REG_REMOTE_TEMP_MSB) << 8;
  567. data->temp11[0] |= i2c_smbus_read_byte_data(client,
  568. LM63_REG_REMOTE_TEMP_LSB);
  569. data->temp11[1] = (i2c_smbus_read_byte_data(client,
  570. LM63_REG_REMOTE_LOW_MSB) << 8)
  571. | i2c_smbus_read_byte_data(client,
  572. LM63_REG_REMOTE_LOW_LSB);
  573. data->temp11[2] = (i2c_smbus_read_byte_data(client,
  574. LM63_REG_REMOTE_HIGH_MSB) << 8)
  575. | i2c_smbus_read_byte_data(client,
  576. LM63_REG_REMOTE_HIGH_LSB);
  577. data->temp8[2] = i2c_smbus_read_byte_data(client,
  578. LM63_REG_REMOTE_TCRIT);
  579. data->temp2_crit_hyst = i2c_smbus_read_byte_data(client,
  580. LM63_REG_REMOTE_TCRIT_HYST);
  581. data->alarms = i2c_smbus_read_byte_data(client,
  582. LM63_REG_ALERT_STATUS) & 0x7F;
  583. data->last_updated = jiffies;
  584. data->valid = 1;
  585. }
  586. mutex_unlock(&data->update_lock);
  587. return data;
  588. }
  589. static int __init sensors_lm63_init(void)
  590. {
  591. return i2c_add_driver(&lm63_driver);
  592. }
  593. static void __exit sensors_lm63_exit(void)
  594. {
  595. i2c_del_driver(&lm63_driver);
  596. }
  597. MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
  598. MODULE_DESCRIPTION("LM63 driver");
  599. MODULE_LICENSE("GPL");
  600. module_init(sensors_lm63_init);
  601. module_exit(sensors_lm63_exit);