lm63.c 36 KB

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