lm63.c 36 KB

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