lm63.c 21 KB

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