lm78.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  1. /*
  2. * lm78.c - Part of lm_sensors, Linux kernel modules for hardware
  3. * monitoring
  4. * Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
  5. * Copyright (c) 2007, 2011 Jean Delvare <jdelvare@suse.de>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  22. #include <linux/module.h>
  23. #include <linux/init.h>
  24. #include <linux/slab.h>
  25. #include <linux/jiffies.h>
  26. #include <linux/i2c.h>
  27. #include <linux/hwmon.h>
  28. #include <linux/hwmon-vid.h>
  29. #include <linux/hwmon-sysfs.h>
  30. #include <linux/err.h>
  31. #include <linux/mutex.h>
  32. #ifdef CONFIG_ISA
  33. #include <linux/platform_device.h>
  34. #include <linux/ioport.h>
  35. #include <linux/io.h>
  36. #endif
  37. /* Addresses to scan */
  38. static const unsigned short normal_i2c[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d,
  39. 0x2e, 0x2f, I2C_CLIENT_END };
  40. enum chips { lm78, lm79 };
  41. /* Many LM78 constants specified below */
  42. /* Length of ISA address segment */
  43. #define LM78_EXTENT 8
  44. /* Where are the ISA address/data registers relative to the base address */
  45. #define LM78_ADDR_REG_OFFSET 5
  46. #define LM78_DATA_REG_OFFSET 6
  47. /* The LM78 registers */
  48. #define LM78_REG_IN_MAX(nr) (0x2b + (nr) * 2)
  49. #define LM78_REG_IN_MIN(nr) (0x2c + (nr) * 2)
  50. #define LM78_REG_IN(nr) (0x20 + (nr))
  51. #define LM78_REG_FAN_MIN(nr) (0x3b + (nr))
  52. #define LM78_REG_FAN(nr) (0x28 + (nr))
  53. #define LM78_REG_TEMP 0x27
  54. #define LM78_REG_TEMP_OVER 0x39
  55. #define LM78_REG_TEMP_HYST 0x3a
  56. #define LM78_REG_ALARM1 0x41
  57. #define LM78_REG_ALARM2 0x42
  58. #define LM78_REG_VID_FANDIV 0x47
  59. #define LM78_REG_CONFIG 0x40
  60. #define LM78_REG_CHIPID 0x49
  61. #define LM78_REG_I2C_ADDR 0x48
  62. /*
  63. * Conversions. Rounding and limit checking is only done on the TO_REG
  64. * variants.
  65. */
  66. /*
  67. * IN: mV (0V to 4.08V)
  68. * REG: 16mV/bit
  69. */
  70. static inline u8 IN_TO_REG(unsigned long val)
  71. {
  72. unsigned long nval = clamp_val(val, 0, 4080);
  73. return (nval + 8) / 16;
  74. }
  75. #define IN_FROM_REG(val) ((val) * 16)
  76. static inline u8 FAN_TO_REG(long rpm, int div)
  77. {
  78. if (rpm <= 0)
  79. return 255;
  80. if (rpm > 1350000)
  81. return 1;
  82. return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
  83. }
  84. static inline int FAN_FROM_REG(u8 val, int div)
  85. {
  86. return val == 0 ? -1 : val == 255 ? 0 : 1350000 / (val * div);
  87. }
  88. /*
  89. * TEMP: mC (-128C to +127C)
  90. * REG: 1C/bit, two's complement
  91. */
  92. static inline s8 TEMP_TO_REG(long val)
  93. {
  94. int nval = clamp_val(val, -128000, 127000) ;
  95. return nval < 0 ? (nval - 500) / 1000 : (nval + 500) / 1000;
  96. }
  97. static inline int TEMP_FROM_REG(s8 val)
  98. {
  99. return val * 1000;
  100. }
  101. #define DIV_FROM_REG(val) (1 << (val))
  102. struct lm78_data {
  103. struct i2c_client *client;
  104. struct mutex lock;
  105. enum chips type;
  106. /* For ISA device only */
  107. const char *name;
  108. int isa_addr;
  109. struct mutex update_lock;
  110. char valid; /* !=0 if following fields are valid */
  111. unsigned long last_updated; /* In jiffies */
  112. u8 in[7]; /* Register value */
  113. u8 in_max[7]; /* Register value */
  114. u8 in_min[7]; /* Register value */
  115. u8 fan[3]; /* Register value */
  116. u8 fan_min[3]; /* Register value */
  117. s8 temp; /* Register value */
  118. s8 temp_over; /* Register value */
  119. s8 temp_hyst; /* Register value */
  120. u8 fan_div[3]; /* Register encoding, shifted right */
  121. u8 vid; /* Register encoding, combined */
  122. u16 alarms; /* Register encoding, combined */
  123. };
  124. static int lm78_read_value(struct lm78_data *data, u8 reg);
  125. static int lm78_write_value(struct lm78_data *data, u8 reg, u8 value);
  126. static struct lm78_data *lm78_update_device(struct device *dev);
  127. static void lm78_init_device(struct lm78_data *data);
  128. /* 7 Voltages */
  129. static ssize_t show_in(struct device *dev, struct device_attribute *da,
  130. char *buf)
  131. {
  132. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  133. struct lm78_data *data = lm78_update_device(dev);
  134. return sprintf(buf, "%d\n", IN_FROM_REG(data->in[attr->index]));
  135. }
  136. static ssize_t show_in_min(struct device *dev, struct device_attribute *da,
  137. char *buf)
  138. {
  139. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  140. struct lm78_data *data = lm78_update_device(dev);
  141. return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[attr->index]));
  142. }
  143. static ssize_t show_in_max(struct device *dev, struct device_attribute *da,
  144. char *buf)
  145. {
  146. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  147. struct lm78_data *data = lm78_update_device(dev);
  148. return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[attr->index]));
  149. }
  150. static ssize_t set_in_min(struct device *dev, struct device_attribute *da,
  151. const char *buf, size_t count)
  152. {
  153. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  154. struct lm78_data *data = dev_get_drvdata(dev);
  155. int nr = attr->index;
  156. unsigned long val;
  157. int err;
  158. err = kstrtoul(buf, 10, &val);
  159. if (err)
  160. return err;
  161. mutex_lock(&data->update_lock);
  162. data->in_min[nr] = IN_TO_REG(val);
  163. lm78_write_value(data, LM78_REG_IN_MIN(nr), data->in_min[nr]);
  164. mutex_unlock(&data->update_lock);
  165. return count;
  166. }
  167. static ssize_t set_in_max(struct device *dev, struct device_attribute *da,
  168. const char *buf, size_t count)
  169. {
  170. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  171. struct lm78_data *data = dev_get_drvdata(dev);
  172. int nr = attr->index;
  173. unsigned long val;
  174. int err;
  175. err = kstrtoul(buf, 10, &val);
  176. if (err)
  177. return err;
  178. mutex_lock(&data->update_lock);
  179. data->in_max[nr] = IN_TO_REG(val);
  180. lm78_write_value(data, LM78_REG_IN_MAX(nr), data->in_max[nr]);
  181. mutex_unlock(&data->update_lock);
  182. return count;
  183. }
  184. #define show_in_offset(offset) \
  185. static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
  186. show_in, NULL, offset); \
  187. static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
  188. show_in_min, set_in_min, offset); \
  189. static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
  190. show_in_max, set_in_max, offset);
  191. show_in_offset(0);
  192. show_in_offset(1);
  193. show_in_offset(2);
  194. show_in_offset(3);
  195. show_in_offset(4);
  196. show_in_offset(5);
  197. show_in_offset(6);
  198. /* Temperature */
  199. static ssize_t show_temp(struct device *dev, struct device_attribute *da,
  200. char *buf)
  201. {
  202. struct lm78_data *data = lm78_update_device(dev);
  203. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp));
  204. }
  205. static ssize_t show_temp_over(struct device *dev, struct device_attribute *da,
  206. char *buf)
  207. {
  208. struct lm78_data *data = lm78_update_device(dev);
  209. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_over));
  210. }
  211. static ssize_t set_temp_over(struct device *dev, struct device_attribute *da,
  212. const char *buf, size_t count)
  213. {
  214. struct lm78_data *data = dev_get_drvdata(dev);
  215. long val;
  216. int err;
  217. err = kstrtol(buf, 10, &val);
  218. if (err)
  219. return err;
  220. mutex_lock(&data->update_lock);
  221. data->temp_over = TEMP_TO_REG(val);
  222. lm78_write_value(data, LM78_REG_TEMP_OVER, data->temp_over);
  223. mutex_unlock(&data->update_lock);
  224. return count;
  225. }
  226. static ssize_t show_temp_hyst(struct device *dev, struct device_attribute *da,
  227. char *buf)
  228. {
  229. struct lm78_data *data = lm78_update_device(dev);
  230. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_hyst));
  231. }
  232. static ssize_t set_temp_hyst(struct device *dev, struct device_attribute *da,
  233. const char *buf, size_t count)
  234. {
  235. struct lm78_data *data = dev_get_drvdata(dev);
  236. long val;
  237. int err;
  238. err = kstrtol(buf, 10, &val);
  239. if (err)
  240. return err;
  241. mutex_lock(&data->update_lock);
  242. data->temp_hyst = TEMP_TO_REG(val);
  243. lm78_write_value(data, LM78_REG_TEMP_HYST, data->temp_hyst);
  244. mutex_unlock(&data->update_lock);
  245. return count;
  246. }
  247. static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
  248. static DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR,
  249. show_temp_over, set_temp_over);
  250. static DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR,
  251. show_temp_hyst, set_temp_hyst);
  252. /* 3 Fans */
  253. static ssize_t show_fan(struct device *dev, struct device_attribute *da,
  254. char *buf)
  255. {
  256. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  257. struct lm78_data *data = lm78_update_device(dev);
  258. int nr = attr->index;
  259. return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
  260. DIV_FROM_REG(data->fan_div[nr])));
  261. }
  262. static ssize_t show_fan_min(struct device *dev, struct device_attribute *da,
  263. char *buf)
  264. {
  265. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  266. struct lm78_data *data = lm78_update_device(dev);
  267. int nr = attr->index;
  268. return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
  269. DIV_FROM_REG(data->fan_div[nr])));
  270. }
  271. static ssize_t set_fan_min(struct device *dev, struct device_attribute *da,
  272. const char *buf, size_t count)
  273. {
  274. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  275. struct lm78_data *data = dev_get_drvdata(dev);
  276. int nr = attr->index;
  277. unsigned long val;
  278. int err;
  279. err = kstrtoul(buf, 10, &val);
  280. if (err)
  281. return err;
  282. mutex_lock(&data->update_lock);
  283. data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
  284. lm78_write_value(data, LM78_REG_FAN_MIN(nr), data->fan_min[nr]);
  285. mutex_unlock(&data->update_lock);
  286. return count;
  287. }
  288. static ssize_t show_fan_div(struct device *dev, struct device_attribute *da,
  289. char *buf)
  290. {
  291. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  292. struct lm78_data *data = lm78_update_device(dev);
  293. return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[attr->index]));
  294. }
  295. /*
  296. * Note: we save and restore the fan minimum here, because its value is
  297. * determined in part by the fan divisor. This follows the principle of
  298. * least surprise; the user doesn't expect the fan minimum to change just
  299. * because the divisor changed.
  300. */
  301. static ssize_t set_fan_div(struct device *dev, struct device_attribute *da,
  302. const char *buf, size_t count)
  303. {
  304. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  305. struct lm78_data *data = dev_get_drvdata(dev);
  306. int nr = attr->index;
  307. unsigned long min;
  308. u8 reg;
  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. min = FAN_FROM_REG(data->fan_min[nr],
  316. DIV_FROM_REG(data->fan_div[nr]));
  317. switch (val) {
  318. case 1:
  319. data->fan_div[nr] = 0;
  320. break;
  321. case 2:
  322. data->fan_div[nr] = 1;
  323. break;
  324. case 4:
  325. data->fan_div[nr] = 2;
  326. break;
  327. case 8:
  328. data->fan_div[nr] = 3;
  329. break;
  330. default:
  331. dev_err(dev,
  332. "fan_div value %ld not supported. Choose one of 1, 2, 4 or 8!\n",
  333. val);
  334. mutex_unlock(&data->update_lock);
  335. return -EINVAL;
  336. }
  337. reg = lm78_read_value(data, LM78_REG_VID_FANDIV);
  338. switch (nr) {
  339. case 0:
  340. reg = (reg & 0xcf) | (data->fan_div[nr] << 4);
  341. break;
  342. case 1:
  343. reg = (reg & 0x3f) | (data->fan_div[nr] << 6);
  344. break;
  345. }
  346. lm78_write_value(data, LM78_REG_VID_FANDIV, reg);
  347. data->fan_min[nr] =
  348. FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
  349. lm78_write_value(data, LM78_REG_FAN_MIN(nr), data->fan_min[nr]);
  350. mutex_unlock(&data->update_lock);
  351. return count;
  352. }
  353. #define show_fan_offset(offset) \
  354. static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
  355. show_fan, NULL, offset - 1); \
  356. static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
  357. show_fan_min, set_fan_min, offset - 1);
  358. show_fan_offset(1);
  359. show_fan_offset(2);
  360. show_fan_offset(3);
  361. /* Fan 3 divisor is locked in H/W */
  362. static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR,
  363. show_fan_div, set_fan_div, 0);
  364. static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR,
  365. show_fan_div, set_fan_div, 1);
  366. static SENSOR_DEVICE_ATTR(fan3_div, S_IRUGO, show_fan_div, NULL, 2);
  367. /* VID */
  368. static ssize_t show_vid(struct device *dev, struct device_attribute *da,
  369. char *buf)
  370. {
  371. struct lm78_data *data = lm78_update_device(dev);
  372. return sprintf(buf, "%d\n", vid_from_reg(data->vid, 82));
  373. }
  374. static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
  375. /* Alarms */
  376. static ssize_t show_alarms(struct device *dev, struct device_attribute *da,
  377. char *buf)
  378. {
  379. struct lm78_data *data = lm78_update_device(dev);
  380. return sprintf(buf, "%u\n", data->alarms);
  381. }
  382. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  383. static ssize_t show_alarm(struct device *dev, struct device_attribute *da,
  384. char *buf)
  385. {
  386. struct lm78_data *data = lm78_update_device(dev);
  387. int nr = to_sensor_dev_attr(da)->index;
  388. return sprintf(buf, "%u\n", (data->alarms >> nr) & 1);
  389. }
  390. static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
  391. static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
  392. static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
  393. static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
  394. static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
  395. static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 9);
  396. static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 10);
  397. static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6);
  398. static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7);
  399. static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 11);
  400. static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
  401. static struct attribute *lm78_attrs[] = {
  402. &sensor_dev_attr_in0_input.dev_attr.attr,
  403. &sensor_dev_attr_in0_min.dev_attr.attr,
  404. &sensor_dev_attr_in0_max.dev_attr.attr,
  405. &sensor_dev_attr_in0_alarm.dev_attr.attr,
  406. &sensor_dev_attr_in1_input.dev_attr.attr,
  407. &sensor_dev_attr_in1_min.dev_attr.attr,
  408. &sensor_dev_attr_in1_max.dev_attr.attr,
  409. &sensor_dev_attr_in1_alarm.dev_attr.attr,
  410. &sensor_dev_attr_in2_input.dev_attr.attr,
  411. &sensor_dev_attr_in2_min.dev_attr.attr,
  412. &sensor_dev_attr_in2_max.dev_attr.attr,
  413. &sensor_dev_attr_in2_alarm.dev_attr.attr,
  414. &sensor_dev_attr_in3_input.dev_attr.attr,
  415. &sensor_dev_attr_in3_min.dev_attr.attr,
  416. &sensor_dev_attr_in3_max.dev_attr.attr,
  417. &sensor_dev_attr_in3_alarm.dev_attr.attr,
  418. &sensor_dev_attr_in4_input.dev_attr.attr,
  419. &sensor_dev_attr_in4_min.dev_attr.attr,
  420. &sensor_dev_attr_in4_max.dev_attr.attr,
  421. &sensor_dev_attr_in4_alarm.dev_attr.attr,
  422. &sensor_dev_attr_in5_input.dev_attr.attr,
  423. &sensor_dev_attr_in5_min.dev_attr.attr,
  424. &sensor_dev_attr_in5_max.dev_attr.attr,
  425. &sensor_dev_attr_in5_alarm.dev_attr.attr,
  426. &sensor_dev_attr_in6_input.dev_attr.attr,
  427. &sensor_dev_attr_in6_min.dev_attr.attr,
  428. &sensor_dev_attr_in6_max.dev_attr.attr,
  429. &sensor_dev_attr_in6_alarm.dev_attr.attr,
  430. &dev_attr_temp1_input.attr,
  431. &dev_attr_temp1_max.attr,
  432. &dev_attr_temp1_max_hyst.attr,
  433. &sensor_dev_attr_temp1_alarm.dev_attr.attr,
  434. &sensor_dev_attr_fan1_input.dev_attr.attr,
  435. &sensor_dev_attr_fan1_min.dev_attr.attr,
  436. &sensor_dev_attr_fan1_div.dev_attr.attr,
  437. &sensor_dev_attr_fan1_alarm.dev_attr.attr,
  438. &sensor_dev_attr_fan2_input.dev_attr.attr,
  439. &sensor_dev_attr_fan2_min.dev_attr.attr,
  440. &sensor_dev_attr_fan2_div.dev_attr.attr,
  441. &sensor_dev_attr_fan2_alarm.dev_attr.attr,
  442. &sensor_dev_attr_fan3_input.dev_attr.attr,
  443. &sensor_dev_attr_fan3_min.dev_attr.attr,
  444. &sensor_dev_attr_fan3_div.dev_attr.attr,
  445. &sensor_dev_attr_fan3_alarm.dev_attr.attr,
  446. &dev_attr_alarms.attr,
  447. &dev_attr_cpu0_vid.attr,
  448. NULL
  449. };
  450. ATTRIBUTE_GROUPS(lm78);
  451. /*
  452. * ISA related code
  453. */
  454. #ifdef CONFIG_ISA
  455. /* ISA device, if found */
  456. static struct platform_device *pdev;
  457. static unsigned short isa_address = 0x290;
  458. static struct lm78_data *lm78_data_if_isa(void)
  459. {
  460. return pdev ? platform_get_drvdata(pdev) : NULL;
  461. }
  462. /* Returns 1 if the I2C chip appears to be an alias of the ISA chip */
  463. static int lm78_alias_detect(struct i2c_client *client, u8 chipid)
  464. {
  465. struct lm78_data *isa;
  466. int i;
  467. if (!pdev) /* No ISA chip */
  468. return 0;
  469. isa = platform_get_drvdata(pdev);
  470. if (lm78_read_value(isa, LM78_REG_I2C_ADDR) != client->addr)
  471. return 0; /* Address doesn't match */
  472. if ((lm78_read_value(isa, LM78_REG_CHIPID) & 0xfe) != (chipid & 0xfe))
  473. return 0; /* Chip type doesn't match */
  474. /*
  475. * We compare all the limit registers, the config register and the
  476. * interrupt mask registers
  477. */
  478. for (i = 0x2b; i <= 0x3d; i++) {
  479. if (lm78_read_value(isa, i) !=
  480. i2c_smbus_read_byte_data(client, i))
  481. return 0;
  482. }
  483. if (lm78_read_value(isa, LM78_REG_CONFIG) !=
  484. i2c_smbus_read_byte_data(client, LM78_REG_CONFIG))
  485. return 0;
  486. for (i = 0x43; i <= 0x46; i++) {
  487. if (lm78_read_value(isa, i) !=
  488. i2c_smbus_read_byte_data(client, i))
  489. return 0;
  490. }
  491. return 1;
  492. }
  493. #else /* !CONFIG_ISA */
  494. static int lm78_alias_detect(struct i2c_client *client, u8 chipid)
  495. {
  496. return 0;
  497. }
  498. static struct lm78_data *lm78_data_if_isa(void)
  499. {
  500. return NULL;
  501. }
  502. #endif /* CONFIG_ISA */
  503. static int lm78_i2c_detect(struct i2c_client *client,
  504. struct i2c_board_info *info)
  505. {
  506. int i;
  507. struct lm78_data *isa = lm78_data_if_isa();
  508. const char *client_name;
  509. struct i2c_adapter *adapter = client->adapter;
  510. int address = client->addr;
  511. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  512. return -ENODEV;
  513. /*
  514. * We block updates of the ISA device to minimize the risk of
  515. * concurrent access to the same LM78 chip through different
  516. * interfaces.
  517. */
  518. if (isa)
  519. mutex_lock(&isa->update_lock);
  520. if ((i2c_smbus_read_byte_data(client, LM78_REG_CONFIG) & 0x80)
  521. || i2c_smbus_read_byte_data(client, LM78_REG_I2C_ADDR) != address)
  522. goto err_nodev;
  523. /* Explicitly prevent the misdetection of Winbond chips */
  524. i = i2c_smbus_read_byte_data(client, 0x4f);
  525. if (i == 0xa3 || i == 0x5c)
  526. goto err_nodev;
  527. /* Determine the chip type. */
  528. i = i2c_smbus_read_byte_data(client, LM78_REG_CHIPID);
  529. if (i == 0x00 || i == 0x20 /* LM78 */
  530. || i == 0x40) /* LM78-J */
  531. client_name = "lm78";
  532. else if ((i & 0xfe) == 0xc0)
  533. client_name = "lm79";
  534. else
  535. goto err_nodev;
  536. if (lm78_alias_detect(client, i)) {
  537. dev_dbg(&adapter->dev,
  538. "Device at 0x%02x appears to be the same as ISA device\n",
  539. address);
  540. goto err_nodev;
  541. }
  542. if (isa)
  543. mutex_unlock(&isa->update_lock);
  544. strlcpy(info->type, client_name, I2C_NAME_SIZE);
  545. return 0;
  546. err_nodev:
  547. if (isa)
  548. mutex_unlock(&isa->update_lock);
  549. return -ENODEV;
  550. }
  551. static int lm78_i2c_probe(struct i2c_client *client,
  552. const struct i2c_device_id *id)
  553. {
  554. struct device *dev = &client->dev;
  555. struct device *hwmon_dev;
  556. struct lm78_data *data;
  557. data = devm_kzalloc(dev, sizeof(struct lm78_data), GFP_KERNEL);
  558. if (!data)
  559. return -ENOMEM;
  560. data->client = client;
  561. data->type = id->driver_data;
  562. /* Initialize the LM78 chip */
  563. lm78_init_device(data);
  564. hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
  565. data, lm78_groups);
  566. return PTR_ERR_OR_ZERO(hwmon_dev);
  567. }
  568. static const struct i2c_device_id lm78_i2c_id[] = {
  569. { "lm78", lm78 },
  570. { "lm79", lm79 },
  571. { }
  572. };
  573. MODULE_DEVICE_TABLE(i2c, lm78_i2c_id);
  574. static struct i2c_driver lm78_driver = {
  575. .class = I2C_CLASS_HWMON,
  576. .driver = {
  577. .name = "lm78",
  578. },
  579. .probe = lm78_i2c_probe,
  580. .id_table = lm78_i2c_id,
  581. .detect = lm78_i2c_detect,
  582. .address_list = normal_i2c,
  583. };
  584. /*
  585. * The SMBus locks itself, but ISA access must be locked explicitly!
  586. * We don't want to lock the whole ISA bus, so we lock each client
  587. * separately.
  588. * We ignore the LM78 BUSY flag at this moment - it could lead to deadlocks,
  589. * would slow down the LM78 access and should not be necessary.
  590. */
  591. static int lm78_read_value(struct lm78_data *data, u8 reg)
  592. {
  593. struct i2c_client *client = data->client;
  594. #ifdef CONFIG_ISA
  595. if (!client) { /* ISA device */
  596. int res;
  597. mutex_lock(&data->lock);
  598. outb_p(reg, data->isa_addr + LM78_ADDR_REG_OFFSET);
  599. res = inb_p(data->isa_addr + LM78_DATA_REG_OFFSET);
  600. mutex_unlock(&data->lock);
  601. return res;
  602. } else
  603. #endif
  604. return i2c_smbus_read_byte_data(client, reg);
  605. }
  606. static int lm78_write_value(struct lm78_data *data, u8 reg, u8 value)
  607. {
  608. struct i2c_client *client = data->client;
  609. #ifdef CONFIG_ISA
  610. if (!client) { /* ISA device */
  611. mutex_lock(&data->lock);
  612. outb_p(reg, data->isa_addr + LM78_ADDR_REG_OFFSET);
  613. outb_p(value, data->isa_addr + LM78_DATA_REG_OFFSET);
  614. mutex_unlock(&data->lock);
  615. return 0;
  616. } else
  617. #endif
  618. return i2c_smbus_write_byte_data(client, reg, value);
  619. }
  620. static void lm78_init_device(struct lm78_data *data)
  621. {
  622. u8 config;
  623. int i;
  624. /* Start monitoring */
  625. config = lm78_read_value(data, LM78_REG_CONFIG);
  626. if ((config & 0x09) != 0x01)
  627. lm78_write_value(data, LM78_REG_CONFIG,
  628. (config & 0xf7) | 0x01);
  629. /* A few vars need to be filled upon startup */
  630. for (i = 0; i < 3; i++) {
  631. data->fan_min[i] = lm78_read_value(data,
  632. LM78_REG_FAN_MIN(i));
  633. }
  634. mutex_init(&data->update_lock);
  635. }
  636. static struct lm78_data *lm78_update_device(struct device *dev)
  637. {
  638. struct lm78_data *data = dev_get_drvdata(dev);
  639. int i;
  640. mutex_lock(&data->update_lock);
  641. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  642. || !data->valid) {
  643. dev_dbg(dev, "Starting lm78 update\n");
  644. for (i = 0; i <= 6; i++) {
  645. data->in[i] =
  646. lm78_read_value(data, LM78_REG_IN(i));
  647. data->in_min[i] =
  648. lm78_read_value(data, LM78_REG_IN_MIN(i));
  649. data->in_max[i] =
  650. lm78_read_value(data, LM78_REG_IN_MAX(i));
  651. }
  652. for (i = 0; i < 3; i++) {
  653. data->fan[i] =
  654. lm78_read_value(data, LM78_REG_FAN(i));
  655. data->fan_min[i] =
  656. lm78_read_value(data, LM78_REG_FAN_MIN(i));
  657. }
  658. data->temp = lm78_read_value(data, LM78_REG_TEMP);
  659. data->temp_over =
  660. lm78_read_value(data, LM78_REG_TEMP_OVER);
  661. data->temp_hyst =
  662. lm78_read_value(data, LM78_REG_TEMP_HYST);
  663. i = lm78_read_value(data, LM78_REG_VID_FANDIV);
  664. data->vid = i & 0x0f;
  665. if (data->type == lm79)
  666. data->vid |=
  667. (lm78_read_value(data, LM78_REG_CHIPID) &
  668. 0x01) << 4;
  669. else
  670. data->vid |= 0x10;
  671. data->fan_div[0] = (i >> 4) & 0x03;
  672. data->fan_div[1] = i >> 6;
  673. data->alarms = lm78_read_value(data, LM78_REG_ALARM1) +
  674. (lm78_read_value(data, LM78_REG_ALARM2) << 8);
  675. data->last_updated = jiffies;
  676. data->valid = 1;
  677. data->fan_div[2] = 1;
  678. }
  679. mutex_unlock(&data->update_lock);
  680. return data;
  681. }
  682. #ifdef CONFIG_ISA
  683. static int lm78_isa_probe(struct platform_device *pdev)
  684. {
  685. struct device *dev = &pdev->dev;
  686. struct device *hwmon_dev;
  687. struct lm78_data *data;
  688. struct resource *res;
  689. /* Reserve the ISA region */
  690. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  691. if (!devm_request_region(dev, res->start + LM78_ADDR_REG_OFFSET,
  692. 2, "lm78"))
  693. return -EBUSY;
  694. data = devm_kzalloc(dev, sizeof(struct lm78_data), GFP_KERNEL);
  695. if (!data)
  696. return -ENOMEM;
  697. mutex_init(&data->lock);
  698. data->isa_addr = res->start;
  699. platform_set_drvdata(pdev, data);
  700. if (lm78_read_value(data, LM78_REG_CHIPID) & 0x80) {
  701. data->type = lm79;
  702. data->name = "lm79";
  703. } else {
  704. data->type = lm78;
  705. data->name = "lm78";
  706. }
  707. /* Initialize the LM78 chip */
  708. lm78_init_device(data);
  709. hwmon_dev = devm_hwmon_device_register_with_groups(dev, data->name,
  710. data, lm78_groups);
  711. return PTR_ERR_OR_ZERO(hwmon_dev);
  712. }
  713. static struct platform_driver lm78_isa_driver = {
  714. .driver = {
  715. .name = "lm78",
  716. },
  717. .probe = lm78_isa_probe,
  718. };
  719. /* return 1 if a supported chip is found, 0 otherwise */
  720. static int __init lm78_isa_found(unsigned short address)
  721. {
  722. int val, save, found = 0;
  723. int port;
  724. /*
  725. * Some boards declare base+0 to base+7 as a PNP device, some base+4
  726. * to base+7 and some base+5 to base+6. So we better request each port
  727. * individually for the probing phase.
  728. */
  729. for (port = address; port < address + LM78_EXTENT; port++) {
  730. if (!request_region(port, 1, "lm78")) {
  731. pr_debug("Failed to request port 0x%x\n", port);
  732. goto release;
  733. }
  734. }
  735. #define REALLY_SLOW_IO
  736. /*
  737. * We need the timeouts for at least some LM78-like
  738. * chips. But only if we read 'undefined' registers.
  739. */
  740. val = inb_p(address + 1);
  741. if (inb_p(address + 2) != val
  742. || inb_p(address + 3) != val
  743. || inb_p(address + 7) != val)
  744. goto release;
  745. #undef REALLY_SLOW_IO
  746. /*
  747. * We should be able to change the 7 LSB of the address port. The
  748. * MSB (busy flag) should be clear initially, set after the write.
  749. */
  750. save = inb_p(address + LM78_ADDR_REG_OFFSET);
  751. if (save & 0x80)
  752. goto release;
  753. val = ~save & 0x7f;
  754. outb_p(val, address + LM78_ADDR_REG_OFFSET);
  755. if (inb_p(address + LM78_ADDR_REG_OFFSET) != (val | 0x80)) {
  756. outb_p(save, address + LM78_ADDR_REG_OFFSET);
  757. goto release;
  758. }
  759. /* We found a device, now see if it could be an LM78 */
  760. outb_p(LM78_REG_CONFIG, address + LM78_ADDR_REG_OFFSET);
  761. val = inb_p(address + LM78_DATA_REG_OFFSET);
  762. if (val & 0x80)
  763. goto release;
  764. outb_p(LM78_REG_I2C_ADDR, address + LM78_ADDR_REG_OFFSET);
  765. val = inb_p(address + LM78_DATA_REG_OFFSET);
  766. if (val < 0x03 || val > 0x77) /* Not a valid I2C address */
  767. goto release;
  768. /* The busy flag should be clear again */
  769. if (inb_p(address + LM78_ADDR_REG_OFFSET) & 0x80)
  770. goto release;
  771. /* Explicitly prevent the misdetection of Winbond chips */
  772. outb_p(0x4f, address + LM78_ADDR_REG_OFFSET);
  773. val = inb_p(address + LM78_DATA_REG_OFFSET);
  774. if (val == 0xa3 || val == 0x5c)
  775. goto release;
  776. /* Explicitly prevent the misdetection of ITE chips */
  777. outb_p(0x58, address + LM78_ADDR_REG_OFFSET);
  778. val = inb_p(address + LM78_DATA_REG_OFFSET);
  779. if (val == 0x90)
  780. goto release;
  781. /* Determine the chip type */
  782. outb_p(LM78_REG_CHIPID, address + LM78_ADDR_REG_OFFSET);
  783. val = inb_p(address + LM78_DATA_REG_OFFSET);
  784. if (val == 0x00 || val == 0x20 /* LM78 */
  785. || val == 0x40 /* LM78-J */
  786. || (val & 0xfe) == 0xc0) /* LM79 */
  787. found = 1;
  788. if (found)
  789. pr_info("Found an %s chip at %#x\n",
  790. val & 0x80 ? "LM79" : "LM78", (int)address);
  791. release:
  792. for (port--; port >= address; port--)
  793. release_region(port, 1);
  794. return found;
  795. }
  796. static int __init lm78_isa_device_add(unsigned short address)
  797. {
  798. struct resource res = {
  799. .start = address,
  800. .end = address + LM78_EXTENT - 1,
  801. .name = "lm78",
  802. .flags = IORESOURCE_IO,
  803. };
  804. int err;
  805. pdev = platform_device_alloc("lm78", address);
  806. if (!pdev) {
  807. err = -ENOMEM;
  808. pr_err("Device allocation failed\n");
  809. goto exit;
  810. }
  811. err = platform_device_add_resources(pdev, &res, 1);
  812. if (err) {
  813. pr_err("Device resource addition failed (%d)\n", err);
  814. goto exit_device_put;
  815. }
  816. err = platform_device_add(pdev);
  817. if (err) {
  818. pr_err("Device addition failed (%d)\n", err);
  819. goto exit_device_put;
  820. }
  821. return 0;
  822. exit_device_put:
  823. platform_device_put(pdev);
  824. exit:
  825. pdev = NULL;
  826. return err;
  827. }
  828. static int __init lm78_isa_register(void)
  829. {
  830. int res;
  831. if (lm78_isa_found(isa_address)) {
  832. res = platform_driver_register(&lm78_isa_driver);
  833. if (res)
  834. goto exit;
  835. /* Sets global pdev as a side effect */
  836. res = lm78_isa_device_add(isa_address);
  837. if (res)
  838. goto exit_unreg_isa_driver;
  839. }
  840. return 0;
  841. exit_unreg_isa_driver:
  842. platform_driver_unregister(&lm78_isa_driver);
  843. exit:
  844. return res;
  845. }
  846. static void lm78_isa_unregister(void)
  847. {
  848. if (pdev) {
  849. platform_device_unregister(pdev);
  850. platform_driver_unregister(&lm78_isa_driver);
  851. }
  852. }
  853. #else /* !CONFIG_ISA */
  854. static int __init lm78_isa_register(void)
  855. {
  856. return 0;
  857. }
  858. static void lm78_isa_unregister(void)
  859. {
  860. }
  861. #endif /* CONFIG_ISA */
  862. static int __init sm_lm78_init(void)
  863. {
  864. int res;
  865. /*
  866. * We register the ISA device first, so that we can skip the
  867. * registration of an I2C interface to the same device.
  868. */
  869. res = lm78_isa_register();
  870. if (res)
  871. goto exit;
  872. res = i2c_add_driver(&lm78_driver);
  873. if (res)
  874. goto exit_unreg_isa_device;
  875. return 0;
  876. exit_unreg_isa_device:
  877. lm78_isa_unregister();
  878. exit:
  879. return res;
  880. }
  881. static void __exit sm_lm78_exit(void)
  882. {
  883. lm78_isa_unregister();
  884. i2c_del_driver(&lm78_driver);
  885. }
  886. MODULE_AUTHOR("Frodo Looijaard, Jean Delvare <jdelvare@suse.de>");
  887. MODULE_DESCRIPTION("LM78/LM79 driver");
  888. MODULE_LICENSE("GPL");
  889. module_init(sm_lm78_init);
  890. module_exit(sm_lm78_exit);