lm63.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  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 except for
  53. * LM64 which has one pin dedicated to address selection.
  54. * LM63 and LM96163 have address 0x4c.
  55. * LM64 can have address 0x18 or 0x4e.
  56. */
  57. static const unsigned short normal_i2c[] = { 0x18, 0x4c, 0x4e, I2C_CLIENT_END };
  58. /*
  59. * The LM63 registers
  60. */
  61. #define LM63_REG_CONFIG1 0x03
  62. #define LM63_REG_CONVRATE 0x04
  63. #define LM63_REG_CONFIG2 0xBF
  64. #define LM63_REG_CONFIG_FAN 0x4A
  65. #define LM63_REG_TACH_COUNT_MSB 0x47
  66. #define LM63_REG_TACH_COUNT_LSB 0x46
  67. #define LM63_REG_TACH_LIMIT_MSB 0x49
  68. #define LM63_REG_TACH_LIMIT_LSB 0x48
  69. #define LM63_REG_PWM_VALUE 0x4C
  70. #define LM63_REG_PWM_FREQ 0x4D
  71. #define LM63_REG_LUT_TEMP_HYST 0x4F
  72. #define LM63_REG_LUT_TEMP(nr) (0x50 + 2 * (nr))
  73. #define LM63_REG_LUT_PWM(nr) (0x51 + 2 * (nr))
  74. #define LM63_REG_LOCAL_TEMP 0x00
  75. #define LM63_REG_LOCAL_HIGH 0x05
  76. #define LM63_REG_REMOTE_TEMP_MSB 0x01
  77. #define LM63_REG_REMOTE_TEMP_LSB 0x10
  78. #define LM63_REG_REMOTE_OFFSET_MSB 0x11
  79. #define LM63_REG_REMOTE_OFFSET_LSB 0x12
  80. #define LM63_REG_REMOTE_HIGH_MSB 0x07
  81. #define LM63_REG_REMOTE_HIGH_LSB 0x13
  82. #define LM63_REG_REMOTE_LOW_MSB 0x08
  83. #define LM63_REG_REMOTE_LOW_LSB 0x14
  84. #define LM63_REG_REMOTE_TCRIT 0x19
  85. #define LM63_REG_REMOTE_TCRIT_HYST 0x21
  86. #define LM63_REG_ALERT_STATUS 0x02
  87. #define LM63_REG_ALERT_MASK 0x16
  88. #define LM63_REG_MAN_ID 0xFE
  89. #define LM63_REG_CHIP_ID 0xFF
  90. #define LM96163_REG_TRUTHERM 0x30
  91. #define LM96163_REG_REMOTE_TEMP_U_MSB 0x31
  92. #define LM96163_REG_REMOTE_TEMP_U_LSB 0x32
  93. #define LM96163_REG_CONFIG_ENHANCED 0x45
  94. #define LM63_MAX_CONVRATE 9
  95. #define LM63_MAX_CONVRATE_HZ 32
  96. #define LM96163_MAX_CONVRATE_HZ 26
  97. /*
  98. * Conversions and various macros
  99. * For tachometer counts, the LM63 uses 16-bit values.
  100. * For local temperature and high limit, remote critical limit and hysteresis
  101. * value, it uses signed 8-bit values with LSB = 1 degree Celsius.
  102. * For remote temperature, low and high limits, it uses signed 11-bit values
  103. * with LSB = 0.125 degree Celsius, left-justified in 16-bit registers.
  104. * For LM64 the actual remote diode temperature is 16 degree Celsius higher
  105. * than the register reading. Remote temperature setpoints have to be
  106. * adapted accordingly.
  107. */
  108. #define FAN_FROM_REG(reg) ((reg) == 0xFFFC || (reg) == 0 ? 0 : \
  109. 5400000 / (reg))
  110. #define FAN_TO_REG(val) ((val) <= 82 ? 0xFFFC : \
  111. (5400000 / (val)) & 0xFFFC)
  112. #define TEMP8_FROM_REG(reg) ((reg) * 1000)
  113. #define TEMP8_TO_REG(val) ((val) <= -128000 ? -128 : \
  114. (val) >= 127000 ? 127 : \
  115. (val) < 0 ? ((val) - 500) / 1000 : \
  116. ((val) + 500) / 1000)
  117. #define TEMP8U_TO_REG(val) ((val) <= 0 ? 0 : \
  118. (val) >= 255000 ? 255 : \
  119. ((val) + 500) / 1000)
  120. #define TEMP11_FROM_REG(reg) ((reg) / 32 * 125)
  121. #define TEMP11_TO_REG(val) ((val) <= -128000 ? 0x8000 : \
  122. (val) >= 127875 ? 0x7FE0 : \
  123. (val) < 0 ? ((val) - 62) / 125 * 32 : \
  124. ((val) + 62) / 125 * 32)
  125. #define TEMP11U_TO_REG(val) ((val) <= 0 ? 0 : \
  126. (val) >= 255875 ? 0xFFE0 : \
  127. ((val) + 62) / 125 * 32)
  128. #define HYST_TO_REG(val) ((val) <= 0 ? 0 : \
  129. (val) >= 127000 ? 127 : \
  130. ((val) + 500) / 1000)
  131. #define UPDATE_INTERVAL(max, rate) \
  132. ((1000 << (LM63_MAX_CONVRATE - (rate))) / (max))
  133. /*
  134. * Functions declaration
  135. */
  136. static int lm63_probe(struct i2c_client *client,
  137. const struct i2c_device_id *id);
  138. static int lm63_remove(struct i2c_client *client);
  139. static struct lm63_data *lm63_update_device(struct device *dev);
  140. static int lm63_detect(struct i2c_client *client, struct i2c_board_info *info);
  141. static void lm63_init_client(struct i2c_client *client);
  142. enum chips { lm63, lm64, lm96163 };
  143. /*
  144. * Driver data (common to all clients)
  145. */
  146. static const struct i2c_device_id lm63_id[] = {
  147. { "lm63", lm63 },
  148. { "lm64", lm64 },
  149. { "lm96163", lm96163 },
  150. { }
  151. };
  152. MODULE_DEVICE_TABLE(i2c, lm63_id);
  153. static struct i2c_driver lm63_driver = {
  154. .class = I2C_CLASS_HWMON,
  155. .driver = {
  156. .name = "lm63",
  157. },
  158. .probe = lm63_probe,
  159. .remove = lm63_remove,
  160. .id_table = lm63_id,
  161. .detect = lm63_detect,
  162. .address_list = normal_i2c,
  163. };
  164. /*
  165. * Client data (each client gets its own)
  166. */
  167. struct lm63_data {
  168. struct device *hwmon_dev;
  169. struct mutex update_lock;
  170. char valid; /* zero until following fields are valid */
  171. char lut_valid; /* zero until lut fields are valid */
  172. unsigned long last_updated; /* in jiffies */
  173. unsigned long lut_last_updated; /* in jiffies */
  174. enum chips kind;
  175. int temp2_offset;
  176. int update_interval; /* in milliseconds */
  177. int max_convrate_hz;
  178. int lut_size; /* 8 or 12 */
  179. /* registers values */
  180. u8 config, config_fan;
  181. u16 fan[2]; /* 0: input
  182. 1: low limit */
  183. u8 pwm1_freq;
  184. u8 pwm1[13]; /* 0: current output
  185. 1-12: lookup table */
  186. s8 temp8[15]; /* 0: local input
  187. 1: local high limit
  188. 2: remote critical limit
  189. 3-14: lookup table */
  190. s16 temp11[4]; /* 0: remote input
  191. 1: remote low limit
  192. 2: remote high limit
  193. 3: remote offset */
  194. u16 temp11u; /* remote input (unsigned) */
  195. u8 temp2_crit_hyst;
  196. u8 lut_temp_hyst;
  197. u8 alarms;
  198. bool pwm_highres;
  199. bool lut_temp_highres;
  200. bool remote_unsigned; /* true if unsigned remote upper limits */
  201. bool trutherm;
  202. };
  203. static inline int temp8_from_reg(struct lm63_data *data, int nr)
  204. {
  205. if (data->remote_unsigned)
  206. return TEMP8_FROM_REG((u8)data->temp8[nr]);
  207. return TEMP8_FROM_REG(data->temp8[nr]);
  208. }
  209. static inline int lut_temp_from_reg(struct lm63_data *data, int nr)
  210. {
  211. return data->temp8[nr] * (data->lut_temp_highres ? 500 : 1000);
  212. }
  213. /*
  214. * Sysfs callback functions and files
  215. */
  216. static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
  217. char *buf)
  218. {
  219. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  220. struct lm63_data *data = lm63_update_device(dev);
  221. return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index]));
  222. }
  223. static ssize_t set_fan(struct device *dev, struct device_attribute *dummy,
  224. const char *buf, size_t count)
  225. {
  226. struct i2c_client *client = to_i2c_client(dev);
  227. struct lm63_data *data = i2c_get_clientdata(client);
  228. unsigned long val;
  229. int err;
  230. err = kstrtoul(buf, 10, &val);
  231. if (err)
  232. return err;
  233. mutex_lock(&data->update_lock);
  234. data->fan[1] = FAN_TO_REG(val);
  235. i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_LSB,
  236. data->fan[1] & 0xFF);
  237. i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_MSB,
  238. data->fan[1] >> 8);
  239. mutex_unlock(&data->update_lock);
  240. return count;
  241. }
  242. static ssize_t show_pwm1(struct device *dev, struct device_attribute *devattr,
  243. char *buf)
  244. {
  245. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  246. struct lm63_data *data = lm63_update_device(dev);
  247. int nr = attr->index;
  248. int pwm;
  249. if (data->pwm_highres)
  250. pwm = data->pwm1[nr];
  251. else
  252. pwm = data->pwm1[nr] >= 2 * data->pwm1_freq ?
  253. 255 : (data->pwm1[nr] * 255 + data->pwm1_freq) /
  254. (2 * data->pwm1_freq);
  255. return sprintf(buf, "%d\n", pwm);
  256. }
  257. static ssize_t set_pwm1(struct device *dev, struct device_attribute *dummy,
  258. const char *buf, size_t count)
  259. {
  260. struct i2c_client *client = to_i2c_client(dev);
  261. struct lm63_data *data = i2c_get_clientdata(client);
  262. unsigned long val;
  263. int err;
  264. if (!(data->config_fan & 0x20)) /* register is read-only */
  265. return -EPERM;
  266. err = kstrtoul(buf, 10, &val);
  267. if (err)
  268. return err;
  269. val = SENSORS_LIMIT(val, 0, 255);
  270. mutex_lock(&data->update_lock);
  271. data->pwm1[0] = data->pwm_highres ? val :
  272. (val * data->pwm1_freq * 2 + 127) / 255;
  273. i2c_smbus_write_byte_data(client, LM63_REG_PWM_VALUE, data->pwm1[0]);
  274. mutex_unlock(&data->update_lock);
  275. return count;
  276. }
  277. static ssize_t show_pwm1_enable(struct device *dev,
  278. struct device_attribute *dummy, char *buf)
  279. {
  280. struct lm63_data *data = lm63_update_device(dev);
  281. return sprintf(buf, "%d\n", data->config_fan & 0x20 ? 1 : 2);
  282. }
  283. /*
  284. * There are 8bit registers for both local(temp1) and remote(temp2) sensor.
  285. * For remote sensor registers temp2_offset has to be considered,
  286. * for local sensor it must not.
  287. * So we need separate 8bit accessors for local and remote sensor.
  288. */
  289. static ssize_t show_local_temp8(struct device *dev,
  290. struct device_attribute *devattr,
  291. char *buf)
  292. {
  293. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  294. struct lm63_data *data = lm63_update_device(dev);
  295. return sprintf(buf, "%d\n", TEMP8_FROM_REG(data->temp8[attr->index]));
  296. }
  297. static ssize_t show_remote_temp8(struct device *dev,
  298. struct device_attribute *devattr,
  299. char *buf)
  300. {
  301. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  302. struct lm63_data *data = lm63_update_device(dev);
  303. return sprintf(buf, "%d\n", temp8_from_reg(data, attr->index)
  304. + data->temp2_offset);
  305. }
  306. static ssize_t show_lut_temp(struct device *dev,
  307. struct device_attribute *devattr,
  308. char *buf)
  309. {
  310. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  311. struct lm63_data *data = lm63_update_device(dev);
  312. return sprintf(buf, "%d\n", lut_temp_from_reg(data, attr->index)
  313. + data->temp2_offset);
  314. }
  315. static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr,
  316. const char *buf, size_t count)
  317. {
  318. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  319. struct i2c_client *client = to_i2c_client(dev);
  320. struct lm63_data *data = i2c_get_clientdata(client);
  321. int nr = attr->index;
  322. int reg = nr == 2 ? LM63_REG_REMOTE_TCRIT : LM63_REG_LOCAL_HIGH;
  323. long val;
  324. int err;
  325. int temp;
  326. err = kstrtol(buf, 10, &val);
  327. if (err)
  328. return err;
  329. mutex_lock(&data->update_lock);
  330. if (nr == 2) {
  331. if (data->remote_unsigned)
  332. temp = TEMP8U_TO_REG(val - data->temp2_offset);
  333. else
  334. temp = TEMP8_TO_REG(val - data->temp2_offset);
  335. } else {
  336. temp = TEMP8_TO_REG(val);
  337. }
  338. data->temp8[nr] = temp;
  339. i2c_smbus_write_byte_data(client, reg, temp);
  340. mutex_unlock(&data->update_lock);
  341. return count;
  342. }
  343. static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
  344. char *buf)
  345. {
  346. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  347. struct lm63_data *data = lm63_update_device(dev);
  348. int nr = attr->index;
  349. int temp;
  350. if (!nr) {
  351. /*
  352. * Use unsigned temperature unless its value is zero.
  353. * If it is zero, use signed temperature.
  354. */
  355. if (data->temp11u)
  356. temp = TEMP11_FROM_REG(data->temp11u);
  357. else
  358. temp = TEMP11_FROM_REG(data->temp11[nr]);
  359. } else {
  360. if (data->remote_unsigned && nr == 2)
  361. temp = TEMP11_FROM_REG((u16)data->temp11[nr]);
  362. else
  363. temp = TEMP11_FROM_REG(data->temp11[nr]);
  364. }
  365. return sprintf(buf, "%d\n", temp + data->temp2_offset);
  366. }
  367. static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr,
  368. const char *buf, size_t count)
  369. {
  370. static const u8 reg[6] = {
  371. LM63_REG_REMOTE_LOW_MSB,
  372. LM63_REG_REMOTE_LOW_LSB,
  373. LM63_REG_REMOTE_HIGH_MSB,
  374. LM63_REG_REMOTE_HIGH_LSB,
  375. LM63_REG_REMOTE_OFFSET_MSB,
  376. LM63_REG_REMOTE_OFFSET_LSB,
  377. };
  378. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  379. struct i2c_client *client = to_i2c_client(dev);
  380. struct lm63_data *data = i2c_get_clientdata(client);
  381. long val;
  382. int err;
  383. int nr = attr->index;
  384. err = kstrtol(buf, 10, &val);
  385. if (err)
  386. return err;
  387. mutex_lock(&data->update_lock);
  388. if (data->remote_unsigned && nr == 2)
  389. data->temp11[nr] = TEMP11U_TO_REG(val - data->temp2_offset);
  390. else
  391. data->temp11[nr] = TEMP11_TO_REG(val - data->temp2_offset);
  392. i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2],
  393. data->temp11[nr] >> 8);
  394. i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2 + 1],
  395. data->temp11[nr] & 0xff);
  396. mutex_unlock(&data->update_lock);
  397. return count;
  398. }
  399. /*
  400. * Hysteresis register holds a relative value, while we want to present
  401. * an absolute to user-space
  402. */
  403. static ssize_t show_temp2_crit_hyst(struct device *dev,
  404. struct device_attribute *dummy, char *buf)
  405. {
  406. struct lm63_data *data = lm63_update_device(dev);
  407. return sprintf(buf, "%d\n", temp8_from_reg(data, 2)
  408. + data->temp2_offset
  409. - TEMP8_FROM_REG(data->temp2_crit_hyst));
  410. }
  411. static ssize_t show_lut_temp_hyst(struct device *dev,
  412. struct device_attribute *devattr, char *buf)
  413. {
  414. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  415. struct lm63_data *data = lm63_update_device(dev);
  416. return sprintf(buf, "%d\n", lut_temp_from_reg(data, attr->index)
  417. + data->temp2_offset
  418. - TEMP8_FROM_REG(data->lut_temp_hyst));
  419. }
  420. /*
  421. * And now the other way around, user-space provides an absolute
  422. * hysteresis value and we have to store a relative one
  423. */
  424. static ssize_t set_temp2_crit_hyst(struct device *dev,
  425. struct device_attribute *dummy,
  426. const char *buf, size_t count)
  427. {
  428. struct i2c_client *client = to_i2c_client(dev);
  429. struct lm63_data *data = i2c_get_clientdata(client);
  430. long val;
  431. int err;
  432. long hyst;
  433. err = kstrtol(buf, 10, &val);
  434. if (err)
  435. return err;
  436. mutex_lock(&data->update_lock);
  437. hyst = temp8_from_reg(data, 2) + data->temp2_offset - val;
  438. i2c_smbus_write_byte_data(client, LM63_REG_REMOTE_TCRIT_HYST,
  439. HYST_TO_REG(hyst));
  440. mutex_unlock(&data->update_lock);
  441. return count;
  442. }
  443. /*
  444. * Set conversion rate.
  445. * client->update_lock must be held when calling this function.
  446. */
  447. static void lm63_set_convrate(struct i2c_client *client, struct lm63_data *data,
  448. unsigned int interval)
  449. {
  450. int i;
  451. unsigned int update_interval;
  452. /* Shift calculations to avoid rounding errors */
  453. interval <<= 6;
  454. /* find the nearest update rate */
  455. update_interval = (1 << (LM63_MAX_CONVRATE + 6)) * 1000
  456. / data->max_convrate_hz;
  457. for (i = 0; i < LM63_MAX_CONVRATE; i++, update_interval >>= 1)
  458. if (interval >= update_interval * 3 / 4)
  459. break;
  460. i2c_smbus_write_byte_data(client, LM63_REG_CONVRATE, i);
  461. data->update_interval = UPDATE_INTERVAL(data->max_convrate_hz, i);
  462. }
  463. static ssize_t show_update_interval(struct device *dev,
  464. struct device_attribute *attr, char *buf)
  465. {
  466. struct lm63_data *data = dev_get_drvdata(dev);
  467. return sprintf(buf, "%u\n", data->update_interval);
  468. }
  469. static ssize_t set_update_interval(struct device *dev,
  470. struct device_attribute *attr,
  471. const char *buf, size_t count)
  472. {
  473. struct i2c_client *client = to_i2c_client(dev);
  474. struct lm63_data *data = i2c_get_clientdata(client);
  475. unsigned long val;
  476. int err;
  477. err = kstrtoul(buf, 10, &val);
  478. if (err)
  479. return err;
  480. mutex_lock(&data->update_lock);
  481. lm63_set_convrate(client, data, SENSORS_LIMIT(val, 0, 100000));
  482. mutex_unlock(&data->update_lock);
  483. return count;
  484. }
  485. static ssize_t show_type(struct device *dev, struct device_attribute *attr,
  486. char *buf)
  487. {
  488. struct i2c_client *client = to_i2c_client(dev);
  489. struct lm63_data *data = i2c_get_clientdata(client);
  490. return sprintf(buf, data->trutherm ? "1\n" : "2\n");
  491. }
  492. static ssize_t set_type(struct device *dev, struct device_attribute *attr,
  493. const char *buf, size_t count)
  494. {
  495. struct i2c_client *client = to_i2c_client(dev);
  496. struct lm63_data *data = i2c_get_clientdata(client);
  497. unsigned long val;
  498. int ret;
  499. u8 reg;
  500. ret = kstrtoul(buf, 10, &val);
  501. if (ret < 0)
  502. return ret;
  503. if (val != 1 && val != 2)
  504. return -EINVAL;
  505. mutex_lock(&data->update_lock);
  506. data->trutherm = val == 1;
  507. reg = i2c_smbus_read_byte_data(client, LM96163_REG_TRUTHERM) & ~0x02;
  508. i2c_smbus_write_byte_data(client, LM96163_REG_TRUTHERM,
  509. reg | (data->trutherm ? 0x02 : 0x00));
  510. data->valid = 0;
  511. mutex_unlock(&data->update_lock);
  512. return count;
  513. }
  514. static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
  515. char *buf)
  516. {
  517. struct lm63_data *data = lm63_update_device(dev);
  518. return sprintf(buf, "%u\n", data->alarms);
  519. }
  520. static ssize_t show_alarm(struct device *dev, struct device_attribute *devattr,
  521. char *buf)
  522. {
  523. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  524. struct lm63_data *data = lm63_update_device(dev);
  525. int bitnr = attr->index;
  526. return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
  527. }
  528. static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
  529. static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan,
  530. set_fan, 1);
  531. static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm1, set_pwm1, 0);
  532. static DEVICE_ATTR(pwm1_enable, S_IRUGO, show_pwm1_enable, NULL);
  533. static SENSOR_DEVICE_ATTR(pwm1_auto_point1_pwm, S_IRUGO, show_pwm1, NULL, 1);
  534. static SENSOR_DEVICE_ATTR(pwm1_auto_point1_temp, S_IRUGO,
  535. show_lut_temp, NULL, 3);
  536. static SENSOR_DEVICE_ATTR(pwm1_auto_point1_temp_hyst, S_IRUGO,
  537. show_lut_temp_hyst, NULL, 3);
  538. static SENSOR_DEVICE_ATTR(pwm1_auto_point2_pwm, S_IRUGO, show_pwm1, NULL, 2);
  539. static SENSOR_DEVICE_ATTR(pwm1_auto_point2_temp, S_IRUGO,
  540. show_lut_temp, NULL, 4);
  541. static SENSOR_DEVICE_ATTR(pwm1_auto_point2_temp_hyst, S_IRUGO,
  542. show_lut_temp_hyst, NULL, 4);
  543. static SENSOR_DEVICE_ATTR(pwm1_auto_point3_pwm, S_IRUGO, show_pwm1, NULL, 3);
  544. static SENSOR_DEVICE_ATTR(pwm1_auto_point3_temp, S_IRUGO,
  545. show_lut_temp, NULL, 5);
  546. static SENSOR_DEVICE_ATTR(pwm1_auto_point3_temp_hyst, S_IRUGO,
  547. show_lut_temp_hyst, NULL, 5);
  548. static SENSOR_DEVICE_ATTR(pwm1_auto_point4_pwm, S_IRUGO, show_pwm1, NULL, 4);
  549. static SENSOR_DEVICE_ATTR(pwm1_auto_point4_temp, S_IRUGO,
  550. show_lut_temp, NULL, 6);
  551. static SENSOR_DEVICE_ATTR(pwm1_auto_point4_temp_hyst, S_IRUGO,
  552. show_lut_temp_hyst, NULL, 6);
  553. static SENSOR_DEVICE_ATTR(pwm1_auto_point5_pwm, S_IRUGO, show_pwm1, NULL, 5);
  554. static SENSOR_DEVICE_ATTR(pwm1_auto_point5_temp, S_IRUGO,
  555. show_lut_temp, NULL, 7);
  556. static SENSOR_DEVICE_ATTR(pwm1_auto_point5_temp_hyst, S_IRUGO,
  557. show_lut_temp_hyst, NULL, 7);
  558. static SENSOR_DEVICE_ATTR(pwm1_auto_point6_pwm, S_IRUGO, show_pwm1, NULL, 6);
  559. static SENSOR_DEVICE_ATTR(pwm1_auto_point6_temp, S_IRUGO,
  560. show_lut_temp, NULL, 8);
  561. static SENSOR_DEVICE_ATTR(pwm1_auto_point6_temp_hyst, S_IRUGO,
  562. show_lut_temp_hyst, NULL, 8);
  563. static SENSOR_DEVICE_ATTR(pwm1_auto_point7_pwm, S_IRUGO, show_pwm1, NULL, 7);
  564. static SENSOR_DEVICE_ATTR(pwm1_auto_point7_temp, S_IRUGO,
  565. show_lut_temp, NULL, 9);
  566. static SENSOR_DEVICE_ATTR(pwm1_auto_point7_temp_hyst, S_IRUGO,
  567. show_lut_temp_hyst, NULL, 9);
  568. static SENSOR_DEVICE_ATTR(pwm1_auto_point8_pwm, S_IRUGO, show_pwm1, NULL, 8);
  569. static SENSOR_DEVICE_ATTR(pwm1_auto_point8_temp, S_IRUGO,
  570. show_lut_temp, NULL, 10);
  571. static SENSOR_DEVICE_ATTR(pwm1_auto_point8_temp_hyst, S_IRUGO,
  572. show_lut_temp_hyst, NULL, 10);
  573. static SENSOR_DEVICE_ATTR(pwm1_auto_point9_pwm, S_IRUGO, show_pwm1, NULL, 9);
  574. static SENSOR_DEVICE_ATTR(pwm1_auto_point9_temp, S_IRUGO,
  575. show_lut_temp, NULL, 11);
  576. static SENSOR_DEVICE_ATTR(pwm1_auto_point9_temp_hyst, S_IRUGO,
  577. show_lut_temp_hyst, NULL, 11);
  578. static SENSOR_DEVICE_ATTR(pwm1_auto_point10_pwm, S_IRUGO, show_pwm1, NULL, 10);
  579. static SENSOR_DEVICE_ATTR(pwm1_auto_point10_temp, S_IRUGO,
  580. show_lut_temp, NULL, 12);
  581. static SENSOR_DEVICE_ATTR(pwm1_auto_point10_temp_hyst, S_IRUGO,
  582. show_lut_temp_hyst, NULL, 12);
  583. static SENSOR_DEVICE_ATTR(pwm1_auto_point11_pwm, S_IRUGO, show_pwm1, NULL, 11);
  584. static SENSOR_DEVICE_ATTR(pwm1_auto_point11_temp, S_IRUGO,
  585. show_lut_temp, NULL, 13);
  586. static SENSOR_DEVICE_ATTR(pwm1_auto_point11_temp_hyst, S_IRUGO,
  587. show_lut_temp_hyst, NULL, 13);
  588. static SENSOR_DEVICE_ATTR(pwm1_auto_point12_pwm, S_IRUGO, show_pwm1, NULL, 12);
  589. static SENSOR_DEVICE_ATTR(pwm1_auto_point12_temp, S_IRUGO,
  590. show_lut_temp, NULL, 14);
  591. static SENSOR_DEVICE_ATTR(pwm1_auto_point12_temp_hyst, S_IRUGO,
  592. show_lut_temp_hyst, NULL, 14);
  593. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_local_temp8, NULL, 0);
  594. static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_local_temp8,
  595. set_temp8, 1);
  596. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 0);
  597. static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp11,
  598. set_temp11, 1);
  599. static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp11,
  600. set_temp11, 2);
  601. static SENSOR_DEVICE_ATTR(temp2_offset, S_IWUSR | S_IRUGO, show_temp11,
  602. set_temp11, 3);
  603. static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_remote_temp8,
  604. set_temp8, 2);
  605. static DEVICE_ATTR(temp2_crit_hyst, S_IWUSR | S_IRUGO, show_temp2_crit_hyst,
  606. set_temp2_crit_hyst);
  607. static DEVICE_ATTR(temp2_type, S_IWUSR | S_IRUGO, show_type, set_type);
  608. /* Individual alarm files */
  609. static SENSOR_DEVICE_ATTR(fan1_min_alarm, S_IRUGO, show_alarm, NULL, 0);
  610. static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
  611. static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
  612. static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
  613. static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
  614. static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
  615. /* Raw alarm file for compatibility */
  616. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  617. static DEVICE_ATTR(update_interval, S_IRUGO | S_IWUSR, show_update_interval,
  618. set_update_interval);
  619. static struct attribute *lm63_attributes[] = {
  620. &sensor_dev_attr_pwm1.dev_attr.attr,
  621. &dev_attr_pwm1_enable.attr,
  622. &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
  623. &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
  624. &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
  625. &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
  626. &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
  627. &sensor_dev_attr_pwm1_auto_point2_temp_hyst.dev_attr.attr,
  628. &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
  629. &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
  630. &sensor_dev_attr_pwm1_auto_point3_temp_hyst.dev_attr.attr,
  631. &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
  632. &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
  633. &sensor_dev_attr_pwm1_auto_point4_temp_hyst.dev_attr.attr,
  634. &sensor_dev_attr_pwm1_auto_point5_pwm.dev_attr.attr,
  635. &sensor_dev_attr_pwm1_auto_point5_temp.dev_attr.attr,
  636. &sensor_dev_attr_pwm1_auto_point5_temp_hyst.dev_attr.attr,
  637. &sensor_dev_attr_pwm1_auto_point6_pwm.dev_attr.attr,
  638. &sensor_dev_attr_pwm1_auto_point6_temp.dev_attr.attr,
  639. &sensor_dev_attr_pwm1_auto_point6_temp_hyst.dev_attr.attr,
  640. &sensor_dev_attr_pwm1_auto_point7_pwm.dev_attr.attr,
  641. &sensor_dev_attr_pwm1_auto_point7_temp.dev_attr.attr,
  642. &sensor_dev_attr_pwm1_auto_point7_temp_hyst.dev_attr.attr,
  643. &sensor_dev_attr_pwm1_auto_point8_pwm.dev_attr.attr,
  644. &sensor_dev_attr_pwm1_auto_point8_temp.dev_attr.attr,
  645. &sensor_dev_attr_pwm1_auto_point8_temp_hyst.dev_attr.attr,
  646. &sensor_dev_attr_temp1_input.dev_attr.attr,
  647. &sensor_dev_attr_temp2_input.dev_attr.attr,
  648. &sensor_dev_attr_temp2_min.dev_attr.attr,
  649. &sensor_dev_attr_temp1_max.dev_attr.attr,
  650. &sensor_dev_attr_temp2_max.dev_attr.attr,
  651. &sensor_dev_attr_temp2_offset.dev_attr.attr,
  652. &sensor_dev_attr_temp2_crit.dev_attr.attr,
  653. &dev_attr_temp2_crit_hyst.attr,
  654. &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
  655. &sensor_dev_attr_temp2_fault.dev_attr.attr,
  656. &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
  657. &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
  658. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  659. &dev_attr_alarms.attr,
  660. &dev_attr_update_interval.attr,
  661. NULL
  662. };
  663. static struct attribute *lm63_attributes_extra_lut[] = {
  664. &sensor_dev_attr_pwm1_auto_point9_pwm.dev_attr.attr,
  665. &sensor_dev_attr_pwm1_auto_point9_temp.dev_attr.attr,
  666. &sensor_dev_attr_pwm1_auto_point9_temp_hyst.dev_attr.attr,
  667. &sensor_dev_attr_pwm1_auto_point10_pwm.dev_attr.attr,
  668. &sensor_dev_attr_pwm1_auto_point10_temp.dev_attr.attr,
  669. &sensor_dev_attr_pwm1_auto_point10_temp_hyst.dev_attr.attr,
  670. &sensor_dev_attr_pwm1_auto_point11_pwm.dev_attr.attr,
  671. &sensor_dev_attr_pwm1_auto_point11_temp.dev_attr.attr,
  672. &sensor_dev_attr_pwm1_auto_point11_temp_hyst.dev_attr.attr,
  673. &sensor_dev_attr_pwm1_auto_point12_pwm.dev_attr.attr,
  674. &sensor_dev_attr_pwm1_auto_point12_temp.dev_attr.attr,
  675. &sensor_dev_attr_pwm1_auto_point12_temp_hyst.dev_attr.attr,
  676. NULL
  677. };
  678. static const struct attribute_group lm63_group_extra_lut = {
  679. .attrs = lm63_attributes_extra_lut,
  680. };
  681. /*
  682. * On LM63, temp2_crit can be set only once, which should be job
  683. * of the bootloader.
  684. * On LM64, temp2_crit can always be set.
  685. * On LM96163, temp2_crit can be set if bit 1 of the configuration
  686. * register is true.
  687. */
  688. static umode_t lm63_attribute_mode(struct kobject *kobj,
  689. struct attribute *attr, int index)
  690. {
  691. struct device *dev = container_of(kobj, struct device, kobj);
  692. struct i2c_client *client = to_i2c_client(dev);
  693. struct lm63_data *data = i2c_get_clientdata(client);
  694. if (attr == &sensor_dev_attr_temp2_crit.dev_attr.attr
  695. && (data->kind == lm64 ||
  696. (data->kind == lm96163 && (data->config & 0x02))))
  697. return attr->mode | S_IWUSR;
  698. return attr->mode;
  699. }
  700. static const struct attribute_group lm63_group = {
  701. .is_visible = lm63_attribute_mode,
  702. .attrs = lm63_attributes,
  703. };
  704. static struct attribute *lm63_attributes_fan1[] = {
  705. &sensor_dev_attr_fan1_input.dev_attr.attr,
  706. &sensor_dev_attr_fan1_min.dev_attr.attr,
  707. &sensor_dev_attr_fan1_min_alarm.dev_attr.attr,
  708. NULL
  709. };
  710. static const struct attribute_group lm63_group_fan1 = {
  711. .attrs = lm63_attributes_fan1,
  712. };
  713. /*
  714. * Real code
  715. */
  716. /* Return 0 if detection is successful, -ENODEV otherwise */
  717. static int lm63_detect(struct i2c_client *new_client,
  718. struct i2c_board_info *info)
  719. {
  720. struct i2c_adapter *adapter = new_client->adapter;
  721. u8 man_id, chip_id, reg_config1, reg_config2;
  722. u8 reg_alert_status, reg_alert_mask;
  723. int address = new_client->addr;
  724. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  725. return -ENODEV;
  726. man_id = i2c_smbus_read_byte_data(new_client, LM63_REG_MAN_ID);
  727. chip_id = i2c_smbus_read_byte_data(new_client, LM63_REG_CHIP_ID);
  728. reg_config1 = i2c_smbus_read_byte_data(new_client,
  729. LM63_REG_CONFIG1);
  730. reg_config2 = i2c_smbus_read_byte_data(new_client,
  731. LM63_REG_CONFIG2);
  732. reg_alert_status = i2c_smbus_read_byte_data(new_client,
  733. LM63_REG_ALERT_STATUS);
  734. reg_alert_mask = i2c_smbus_read_byte_data(new_client,
  735. LM63_REG_ALERT_MASK);
  736. if (man_id != 0x01 /* National Semiconductor */
  737. || (reg_config1 & 0x18) != 0x00
  738. || (reg_config2 & 0xF8) != 0x00
  739. || (reg_alert_status & 0x20) != 0x00
  740. || (reg_alert_mask & 0xA4) != 0xA4) {
  741. dev_dbg(&adapter->dev,
  742. "Unsupported chip (man_id=0x%02X, chip_id=0x%02X)\n",
  743. man_id, chip_id);
  744. return -ENODEV;
  745. }
  746. if (chip_id == 0x41 && address == 0x4c)
  747. strlcpy(info->type, "lm63", I2C_NAME_SIZE);
  748. else if (chip_id == 0x51 && (address == 0x18 || address == 0x4e))
  749. strlcpy(info->type, "lm64", I2C_NAME_SIZE);
  750. else if (chip_id == 0x49 && address == 0x4c)
  751. strlcpy(info->type, "lm96163", I2C_NAME_SIZE);
  752. else
  753. return -ENODEV;
  754. return 0;
  755. }
  756. static int lm63_probe(struct i2c_client *new_client,
  757. const struct i2c_device_id *id)
  758. {
  759. struct lm63_data *data;
  760. int err;
  761. data = kzalloc(sizeof(struct lm63_data), GFP_KERNEL);
  762. if (!data) {
  763. err = -ENOMEM;
  764. goto exit;
  765. }
  766. i2c_set_clientdata(new_client, data);
  767. data->valid = 0;
  768. mutex_init(&data->update_lock);
  769. /* Set the device type */
  770. data->kind = id->driver_data;
  771. if (data->kind == lm64)
  772. data->temp2_offset = 16000;
  773. /* Initialize chip */
  774. lm63_init_client(new_client);
  775. /* Register sysfs hooks */
  776. err = sysfs_create_group(&new_client->dev.kobj, &lm63_group);
  777. if (err)
  778. goto exit_free;
  779. if (data->config & 0x04) { /* tachometer enabled */
  780. err = sysfs_create_group(&new_client->dev.kobj,
  781. &lm63_group_fan1);
  782. if (err)
  783. goto exit_remove_files;
  784. }
  785. if (data->kind == lm96163) {
  786. err = device_create_file(&new_client->dev,
  787. &dev_attr_temp2_type);
  788. if (err)
  789. goto exit_remove_files;
  790. err = sysfs_create_group(&new_client->dev.kobj,
  791. &lm63_group_extra_lut);
  792. if (err)
  793. goto exit_remove_files;
  794. }
  795. data->hwmon_dev = hwmon_device_register(&new_client->dev);
  796. if (IS_ERR(data->hwmon_dev)) {
  797. err = PTR_ERR(data->hwmon_dev);
  798. goto exit_remove_files;
  799. }
  800. return 0;
  801. exit_remove_files:
  802. sysfs_remove_group(&new_client->dev.kobj, &lm63_group);
  803. sysfs_remove_group(&new_client->dev.kobj, &lm63_group_fan1);
  804. if (data->kind == lm96163) {
  805. device_remove_file(&new_client->dev, &dev_attr_temp2_type);
  806. sysfs_remove_group(&new_client->dev.kobj,
  807. &lm63_group_extra_lut);
  808. }
  809. exit_free:
  810. kfree(data);
  811. exit:
  812. return err;
  813. }
  814. /*
  815. * Ideally we shouldn't have to initialize anything, since the BIOS
  816. * should have taken care of everything
  817. */
  818. static void lm63_init_client(struct i2c_client *client)
  819. {
  820. struct lm63_data *data = i2c_get_clientdata(client);
  821. u8 convrate;
  822. data->config = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG1);
  823. data->config_fan = i2c_smbus_read_byte_data(client,
  824. LM63_REG_CONFIG_FAN);
  825. /* Start converting if needed */
  826. if (data->config & 0x40) { /* standby */
  827. dev_dbg(&client->dev, "Switching to operational mode\n");
  828. data->config &= 0xA7;
  829. i2c_smbus_write_byte_data(client, LM63_REG_CONFIG1,
  830. data->config);
  831. }
  832. /* Tachometer is always enabled on LM64 */
  833. if (data->kind == lm64)
  834. data->config |= 0x04;
  835. /* We may need pwm1_freq before ever updating the client data */
  836. data->pwm1_freq = i2c_smbus_read_byte_data(client, LM63_REG_PWM_FREQ);
  837. if (data->pwm1_freq == 0)
  838. data->pwm1_freq = 1;
  839. switch (data->kind) {
  840. case lm63:
  841. case lm64:
  842. data->max_convrate_hz = LM63_MAX_CONVRATE_HZ;
  843. data->lut_size = 8;
  844. break;
  845. case lm96163:
  846. data->max_convrate_hz = LM96163_MAX_CONVRATE_HZ;
  847. data->lut_size = 12;
  848. data->trutherm
  849. = i2c_smbus_read_byte_data(client,
  850. LM96163_REG_TRUTHERM) & 0x02;
  851. break;
  852. }
  853. convrate = i2c_smbus_read_byte_data(client, LM63_REG_CONVRATE);
  854. if (unlikely(convrate > LM63_MAX_CONVRATE))
  855. convrate = LM63_MAX_CONVRATE;
  856. data->update_interval = UPDATE_INTERVAL(data->max_convrate_hz,
  857. convrate);
  858. /*
  859. * For LM96163, check if high resolution PWM
  860. * and unsigned temperature format is enabled.
  861. */
  862. if (data->kind == lm96163) {
  863. u8 config_enhanced
  864. = i2c_smbus_read_byte_data(client,
  865. LM96163_REG_CONFIG_ENHANCED);
  866. if (config_enhanced & 0x20)
  867. data->lut_temp_highres = true;
  868. if ((config_enhanced & 0x10)
  869. && !(data->config_fan & 0x08) && data->pwm1_freq == 8)
  870. data->pwm_highres = true;
  871. if (config_enhanced & 0x08)
  872. data->remote_unsigned = true;
  873. }
  874. /* Show some debug info about the LM63 configuration */
  875. if (data->kind == lm63)
  876. dev_dbg(&client->dev, "Alert/tach pin configured for %s\n",
  877. (data->config & 0x04) ? "tachometer input" :
  878. "alert output");
  879. dev_dbg(&client->dev, "PWM clock %s kHz, output frequency %u Hz\n",
  880. (data->config_fan & 0x08) ? "1.4" : "360",
  881. ((data->config_fan & 0x08) ? 700 : 180000) / data->pwm1_freq);
  882. dev_dbg(&client->dev, "PWM output active %s, %s mode\n",
  883. (data->config_fan & 0x10) ? "low" : "high",
  884. (data->config_fan & 0x20) ? "manual" : "auto");
  885. }
  886. static int lm63_remove(struct i2c_client *client)
  887. {
  888. struct lm63_data *data = i2c_get_clientdata(client);
  889. hwmon_device_unregister(data->hwmon_dev);
  890. sysfs_remove_group(&client->dev.kobj, &lm63_group);
  891. sysfs_remove_group(&client->dev.kobj, &lm63_group_fan1);
  892. if (data->kind == lm96163) {
  893. device_remove_file(&client->dev, &dev_attr_temp2_type);
  894. sysfs_remove_group(&client->dev.kobj, &lm63_group_extra_lut);
  895. }
  896. kfree(data);
  897. return 0;
  898. }
  899. static struct lm63_data *lm63_update_device(struct device *dev)
  900. {
  901. struct i2c_client *client = to_i2c_client(dev);
  902. struct lm63_data *data = i2c_get_clientdata(client);
  903. unsigned long next_update;
  904. int i;
  905. mutex_lock(&data->update_lock);
  906. next_update = data->last_updated
  907. + msecs_to_jiffies(data->update_interval) + 1;
  908. if (time_after(jiffies, next_update) || !data->valid) {
  909. if (data->config & 0x04) { /* tachometer enabled */
  910. /* order matters for fan1_input */
  911. data->fan[0] = i2c_smbus_read_byte_data(client,
  912. LM63_REG_TACH_COUNT_LSB) & 0xFC;
  913. data->fan[0] |= i2c_smbus_read_byte_data(client,
  914. LM63_REG_TACH_COUNT_MSB) << 8;
  915. data->fan[1] = (i2c_smbus_read_byte_data(client,
  916. LM63_REG_TACH_LIMIT_LSB) & 0xFC)
  917. | (i2c_smbus_read_byte_data(client,
  918. LM63_REG_TACH_LIMIT_MSB) << 8);
  919. }
  920. data->pwm1_freq = i2c_smbus_read_byte_data(client,
  921. LM63_REG_PWM_FREQ);
  922. if (data->pwm1_freq == 0)
  923. data->pwm1_freq = 1;
  924. data->pwm1[0] = i2c_smbus_read_byte_data(client,
  925. LM63_REG_PWM_VALUE);
  926. data->temp8[0] = i2c_smbus_read_byte_data(client,
  927. LM63_REG_LOCAL_TEMP);
  928. data->temp8[1] = i2c_smbus_read_byte_data(client,
  929. LM63_REG_LOCAL_HIGH);
  930. /* order matters for temp2_input */
  931. data->temp11[0] = i2c_smbus_read_byte_data(client,
  932. LM63_REG_REMOTE_TEMP_MSB) << 8;
  933. data->temp11[0] |= i2c_smbus_read_byte_data(client,
  934. LM63_REG_REMOTE_TEMP_LSB);
  935. data->temp11[1] = (i2c_smbus_read_byte_data(client,
  936. LM63_REG_REMOTE_LOW_MSB) << 8)
  937. | i2c_smbus_read_byte_data(client,
  938. LM63_REG_REMOTE_LOW_LSB);
  939. data->temp11[2] = (i2c_smbus_read_byte_data(client,
  940. LM63_REG_REMOTE_HIGH_MSB) << 8)
  941. | i2c_smbus_read_byte_data(client,
  942. LM63_REG_REMOTE_HIGH_LSB);
  943. data->temp11[3] = (i2c_smbus_read_byte_data(client,
  944. LM63_REG_REMOTE_OFFSET_MSB) << 8)
  945. | i2c_smbus_read_byte_data(client,
  946. LM63_REG_REMOTE_OFFSET_LSB);
  947. if (data->kind == lm96163)
  948. data->temp11u = (i2c_smbus_read_byte_data(client,
  949. LM96163_REG_REMOTE_TEMP_U_MSB) << 8)
  950. | i2c_smbus_read_byte_data(client,
  951. LM96163_REG_REMOTE_TEMP_U_LSB);
  952. data->temp8[2] = i2c_smbus_read_byte_data(client,
  953. LM63_REG_REMOTE_TCRIT);
  954. data->temp2_crit_hyst = i2c_smbus_read_byte_data(client,
  955. LM63_REG_REMOTE_TCRIT_HYST);
  956. data->alarms = i2c_smbus_read_byte_data(client,
  957. LM63_REG_ALERT_STATUS) & 0x7F;
  958. data->last_updated = jiffies;
  959. data->valid = 1;
  960. }
  961. if (time_after(jiffies, data->lut_last_updated + 5 * HZ) ||
  962. !data->lut_valid) {
  963. for (i = 0; i < data->lut_size; i++) {
  964. data->pwm1[1 + i] = i2c_smbus_read_byte_data(client,
  965. LM63_REG_LUT_PWM(i));
  966. data->temp8[3 + i] = i2c_smbus_read_byte_data(client,
  967. LM63_REG_LUT_TEMP(i));
  968. }
  969. data->lut_temp_hyst = i2c_smbus_read_byte_data(client,
  970. LM63_REG_LUT_TEMP_HYST);
  971. data->lut_last_updated = jiffies;
  972. data->lut_valid = 1;
  973. }
  974. mutex_unlock(&data->update_lock);
  975. return data;
  976. }
  977. module_i2c_driver(lm63_driver);
  978. MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
  979. MODULE_DESCRIPTION("LM63 driver");
  980. MODULE_LICENSE("GPL");