lm78.c 27 KB

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