lm63.c 37 KB

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