lm63.c 22 KB

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