asb100.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  1. /*
  2. asb100.c - Part of lm_sensors, Linux kernel modules for hardware
  3. monitoring
  4. Copyright (C) 2004 Mark M. Hoffman <mhoffman@lightlink.com>
  5. (derived from w83781d.c)
  6. Copyright (C) 1998 - 2003 Frodo Looijaard <frodol@dds.nl>,
  7. Philip Edelbrock <phil@netroedge.com>, and
  8. Mark Studebaker <mdsxyz123@yahoo.com>
  9. This program is free software; you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation; either version 2 of the License, or
  12. (at your option) any later version.
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU General Public License for more details.
  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. /*
  22. This driver supports the hardware sensor chips: Asus ASB100 and
  23. ASB100-A "BACH".
  24. ASB100-A supports pwm1, while plain ASB100 does not. There is no known
  25. way for the driver to tell which one is there.
  26. Chip #vin #fanin #pwm #temp wchipid vendid i2c ISA
  27. asb100 7 3 1 4 0x31 0x0694 yes no
  28. */
  29. #include <linux/module.h>
  30. #include <linux/slab.h>
  31. #include <linux/i2c.h>
  32. #include <linux/hwmon.h>
  33. #include <linux/hwmon-vid.h>
  34. #include <linux/err.h>
  35. #include <linux/init.h>
  36. #include <linux/jiffies.h>
  37. #include <linux/mutex.h>
  38. #include "lm75.h"
  39. /*
  40. HISTORY:
  41. 2003-12-29 1.0.0 Ported from lm_sensors project for kernel 2.6
  42. */
  43. #define ASB100_VERSION "1.0.0"
  44. /* I2C addresses to scan */
  45. static unsigned short normal_i2c[] = { 0x2d, I2C_CLIENT_END };
  46. /* Insmod parameters */
  47. I2C_CLIENT_INSMOD_1(asb100);
  48. I2C_CLIENT_MODULE_PARM(force_subclients, "List of subclient addresses: "
  49. "{bus, clientaddr, subclientaddr1, subclientaddr2}");
  50. /* Voltage IN registers 0-6 */
  51. #define ASB100_REG_IN(nr) (0x20 + (nr))
  52. #define ASB100_REG_IN_MAX(nr) (0x2b + (nr * 2))
  53. #define ASB100_REG_IN_MIN(nr) (0x2c + (nr * 2))
  54. /* FAN IN registers 1-3 */
  55. #define ASB100_REG_FAN(nr) (0x28 + (nr))
  56. #define ASB100_REG_FAN_MIN(nr) (0x3b + (nr))
  57. /* TEMPERATURE registers 1-4 */
  58. static const u16 asb100_reg_temp[] = {0, 0x27, 0x150, 0x250, 0x17};
  59. static const u16 asb100_reg_temp_max[] = {0, 0x39, 0x155, 0x255, 0x18};
  60. static const u16 asb100_reg_temp_hyst[] = {0, 0x3a, 0x153, 0x253, 0x19};
  61. #define ASB100_REG_TEMP(nr) (asb100_reg_temp[nr])
  62. #define ASB100_REG_TEMP_MAX(nr) (asb100_reg_temp_max[nr])
  63. #define ASB100_REG_TEMP_HYST(nr) (asb100_reg_temp_hyst[nr])
  64. #define ASB100_REG_TEMP2_CONFIG 0x0152
  65. #define ASB100_REG_TEMP3_CONFIG 0x0252
  66. #define ASB100_REG_CONFIG 0x40
  67. #define ASB100_REG_ALARM1 0x41
  68. #define ASB100_REG_ALARM2 0x42
  69. #define ASB100_REG_SMIM1 0x43
  70. #define ASB100_REG_SMIM2 0x44
  71. #define ASB100_REG_VID_FANDIV 0x47
  72. #define ASB100_REG_I2C_ADDR 0x48
  73. #define ASB100_REG_CHIPID 0x49
  74. #define ASB100_REG_I2C_SUBADDR 0x4a
  75. #define ASB100_REG_PIN 0x4b
  76. #define ASB100_REG_IRQ 0x4c
  77. #define ASB100_REG_BANK 0x4e
  78. #define ASB100_REG_CHIPMAN 0x4f
  79. #define ASB100_REG_WCHIPID 0x58
  80. /* bit 7 -> enable, bits 0-3 -> duty cycle */
  81. #define ASB100_REG_PWM1 0x59
  82. /* CONVERSIONS
  83. Rounding and limit checking is only done on the TO_REG variants. */
  84. /* These constants are a guess, consistent w/ w83781d */
  85. #define ASB100_IN_MIN ( 0)
  86. #define ASB100_IN_MAX (4080)
  87. /* IN: 1/1000 V (0V to 4.08V)
  88. REG: 16mV/bit */
  89. static u8 IN_TO_REG(unsigned val)
  90. {
  91. unsigned nval = SENSORS_LIMIT(val, ASB100_IN_MIN, ASB100_IN_MAX);
  92. return (nval + 8) / 16;
  93. }
  94. static unsigned IN_FROM_REG(u8 reg)
  95. {
  96. return reg * 16;
  97. }
  98. static u8 FAN_TO_REG(long rpm, int div)
  99. {
  100. if (rpm == -1)
  101. return 0;
  102. if (rpm == 0)
  103. return 255;
  104. rpm = SENSORS_LIMIT(rpm, 1, 1000000);
  105. return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
  106. }
  107. static int FAN_FROM_REG(u8 val, int div)
  108. {
  109. return val==0 ? -1 : val==255 ? 0 : 1350000/(val*div);
  110. }
  111. /* These constants are a guess, consistent w/ w83781d */
  112. #define ASB100_TEMP_MIN (-128000)
  113. #define ASB100_TEMP_MAX ( 127000)
  114. /* TEMP: 0.001C/bit (-128C to +127C)
  115. REG: 1C/bit, two's complement */
  116. static u8 TEMP_TO_REG(long temp)
  117. {
  118. int ntemp = SENSORS_LIMIT(temp, ASB100_TEMP_MIN, ASB100_TEMP_MAX);
  119. ntemp += (ntemp<0 ? -500 : 500);
  120. return (u8)(ntemp / 1000);
  121. }
  122. static int TEMP_FROM_REG(u8 reg)
  123. {
  124. return (s8)reg * 1000;
  125. }
  126. /* PWM: 0 - 255 per sensors documentation
  127. REG: (6.25% duty cycle per bit) */
  128. static u8 ASB100_PWM_TO_REG(int pwm)
  129. {
  130. pwm = SENSORS_LIMIT(pwm, 0, 255);
  131. return (u8)(pwm / 16);
  132. }
  133. static int ASB100_PWM_FROM_REG(u8 reg)
  134. {
  135. return reg * 16;
  136. }
  137. #define DIV_FROM_REG(val) (1 << (val))
  138. /* FAN DIV: 1, 2, 4, or 8 (defaults to 2)
  139. REG: 0, 1, 2, or 3 (respectively) (defaults to 1) */
  140. static u8 DIV_TO_REG(long val)
  141. {
  142. return val==8 ? 3 : val==4 ? 2 : val==1 ? 0 : 1;
  143. }
  144. /* For each registered client, we need to keep some data in memory. That
  145. data is pointed to by client->data. The structure itself is
  146. dynamically allocated, at the same time the client itself is allocated. */
  147. struct asb100_data {
  148. struct i2c_client client;
  149. struct device *hwmon_dev;
  150. struct mutex lock;
  151. enum chips type;
  152. struct mutex update_lock;
  153. unsigned long last_updated; /* In jiffies */
  154. /* array of 2 pointers to subclients */
  155. struct i2c_client *lm75[2];
  156. char valid; /* !=0 if following fields are valid */
  157. u8 in[7]; /* Register value */
  158. u8 in_max[7]; /* Register value */
  159. u8 in_min[7]; /* Register value */
  160. u8 fan[3]; /* Register value */
  161. u8 fan_min[3]; /* Register value */
  162. u16 temp[4]; /* Register value (0 and 3 are u8 only) */
  163. u16 temp_max[4]; /* Register value (0 and 3 are u8 only) */
  164. u16 temp_hyst[4]; /* Register value (0 and 3 are u8 only) */
  165. u8 fan_div[3]; /* Register encoding, right justified */
  166. u8 pwm; /* Register encoding */
  167. u8 vid; /* Register encoding, combined */
  168. u32 alarms; /* Register encoding, combined */
  169. u8 vrm;
  170. };
  171. static int asb100_read_value(struct i2c_client *client, u16 reg);
  172. static void asb100_write_value(struct i2c_client *client, u16 reg, u16 val);
  173. static int asb100_attach_adapter(struct i2c_adapter *adapter);
  174. static int asb100_detect(struct i2c_adapter *adapter, int address, int kind);
  175. static int asb100_detach_client(struct i2c_client *client);
  176. static struct asb100_data *asb100_update_device(struct device *dev);
  177. static void asb100_init_client(struct i2c_client *client);
  178. static struct i2c_driver asb100_driver = {
  179. .driver = {
  180. .name = "asb100",
  181. },
  182. .attach_adapter = asb100_attach_adapter,
  183. .detach_client = asb100_detach_client,
  184. };
  185. /* 7 Voltages */
  186. #define show_in_reg(reg) \
  187. static ssize_t show_##reg (struct device *dev, char *buf, int nr) \
  188. { \
  189. struct asb100_data *data = asb100_update_device(dev); \
  190. return sprintf(buf, "%d\n", IN_FROM_REG(data->reg[nr])); \
  191. }
  192. show_in_reg(in)
  193. show_in_reg(in_min)
  194. show_in_reg(in_max)
  195. #define set_in_reg(REG, reg) \
  196. static ssize_t set_in_##reg(struct device *dev, const char *buf, \
  197. size_t count, int nr) \
  198. { \
  199. struct i2c_client *client = to_i2c_client(dev); \
  200. struct asb100_data *data = i2c_get_clientdata(client); \
  201. unsigned long val = simple_strtoul(buf, NULL, 10); \
  202. \
  203. mutex_lock(&data->update_lock); \
  204. data->in_##reg[nr] = IN_TO_REG(val); \
  205. asb100_write_value(client, ASB100_REG_IN_##REG(nr), \
  206. data->in_##reg[nr]); \
  207. mutex_unlock(&data->update_lock); \
  208. return count; \
  209. }
  210. set_in_reg(MIN, min)
  211. set_in_reg(MAX, max)
  212. #define sysfs_in(offset) \
  213. static ssize_t \
  214. show_in##offset (struct device *dev, struct device_attribute *attr, char *buf) \
  215. { \
  216. return show_in(dev, buf, offset); \
  217. } \
  218. static DEVICE_ATTR(in##offset##_input, S_IRUGO, \
  219. show_in##offset, NULL); \
  220. static ssize_t \
  221. show_in##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
  222. { \
  223. return show_in_min(dev, buf, offset); \
  224. } \
  225. static ssize_t \
  226. show_in##offset##_max (struct device *dev, struct device_attribute *attr, char *buf) \
  227. { \
  228. return show_in_max(dev, buf, offset); \
  229. } \
  230. static ssize_t set_in##offset##_min (struct device *dev, struct device_attribute *attr, \
  231. const char *buf, size_t count) \
  232. { \
  233. return set_in_min(dev, buf, count, offset); \
  234. } \
  235. static ssize_t set_in##offset##_max (struct device *dev, struct device_attribute *attr, \
  236. const char *buf, size_t count) \
  237. { \
  238. return set_in_max(dev, buf, count, offset); \
  239. } \
  240. static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
  241. show_in##offset##_min, set_in##offset##_min); \
  242. static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
  243. show_in##offset##_max, set_in##offset##_max);
  244. sysfs_in(0);
  245. sysfs_in(1);
  246. sysfs_in(2);
  247. sysfs_in(3);
  248. sysfs_in(4);
  249. sysfs_in(5);
  250. sysfs_in(6);
  251. /* 3 Fans */
  252. static ssize_t show_fan(struct device *dev, char *buf, int nr)
  253. {
  254. struct asb100_data *data = asb100_update_device(dev);
  255. return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
  256. DIV_FROM_REG(data->fan_div[nr])));
  257. }
  258. static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
  259. {
  260. struct asb100_data *data = asb100_update_device(dev);
  261. return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
  262. DIV_FROM_REG(data->fan_div[nr])));
  263. }
  264. static ssize_t show_fan_div(struct device *dev, char *buf, int nr)
  265. {
  266. struct asb100_data *data = asb100_update_device(dev);
  267. return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
  268. }
  269. static ssize_t set_fan_min(struct device *dev, const char *buf,
  270. size_t count, int nr)
  271. {
  272. struct i2c_client *client = to_i2c_client(dev);
  273. struct asb100_data *data = i2c_get_clientdata(client);
  274. u32 val = simple_strtoul(buf, NULL, 10);
  275. mutex_lock(&data->update_lock);
  276. data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
  277. asb100_write_value(client, ASB100_REG_FAN_MIN(nr), data->fan_min[nr]);
  278. mutex_unlock(&data->update_lock);
  279. return count;
  280. }
  281. /* Note: we save and restore the fan minimum here, because its value is
  282. determined in part by the fan divisor. This follows the principle of
  283. least surprise; the user doesn't expect the fan minimum to change just
  284. because the divisor changed. */
  285. static ssize_t set_fan_div(struct device *dev, const char *buf,
  286. size_t count, int nr)
  287. {
  288. struct i2c_client *client = to_i2c_client(dev);
  289. struct asb100_data *data = i2c_get_clientdata(client);
  290. unsigned long min;
  291. unsigned long val = simple_strtoul(buf, NULL, 10);
  292. int reg;
  293. mutex_lock(&data->update_lock);
  294. min = FAN_FROM_REG(data->fan_min[nr],
  295. DIV_FROM_REG(data->fan_div[nr]));
  296. data->fan_div[nr] = DIV_TO_REG(val);
  297. switch(nr) {
  298. case 0: /* fan 1 */
  299. reg = asb100_read_value(client, ASB100_REG_VID_FANDIV);
  300. reg = (reg & 0xcf) | (data->fan_div[0] << 4);
  301. asb100_write_value(client, ASB100_REG_VID_FANDIV, reg);
  302. break;
  303. case 1: /* fan 2 */
  304. reg = asb100_read_value(client, ASB100_REG_VID_FANDIV);
  305. reg = (reg & 0x3f) | (data->fan_div[1] << 6);
  306. asb100_write_value(client, ASB100_REG_VID_FANDIV, reg);
  307. break;
  308. case 2: /* fan 3 */
  309. reg = asb100_read_value(client, ASB100_REG_PIN);
  310. reg = (reg & 0x3f) | (data->fan_div[2] << 6);
  311. asb100_write_value(client, ASB100_REG_PIN, reg);
  312. break;
  313. }
  314. data->fan_min[nr] =
  315. FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
  316. asb100_write_value(client, ASB100_REG_FAN_MIN(nr), data->fan_min[nr]);
  317. mutex_unlock(&data->update_lock);
  318. return count;
  319. }
  320. #define sysfs_fan(offset) \
  321. static ssize_t show_fan##offset(struct device *dev, struct device_attribute *attr, char *buf) \
  322. { \
  323. return show_fan(dev, buf, offset - 1); \
  324. } \
  325. static ssize_t show_fan##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \
  326. { \
  327. return show_fan_min(dev, buf, offset - 1); \
  328. } \
  329. static ssize_t show_fan##offset##_div(struct device *dev, struct device_attribute *attr, char *buf) \
  330. { \
  331. return show_fan_div(dev, buf, offset - 1); \
  332. } \
  333. static ssize_t set_fan##offset##_min(struct device *dev, struct device_attribute *attr, const char *buf, \
  334. size_t count) \
  335. { \
  336. return set_fan_min(dev, buf, count, offset - 1); \
  337. } \
  338. static ssize_t set_fan##offset##_div(struct device *dev, struct device_attribute *attr, const char *buf, \
  339. size_t count) \
  340. { \
  341. return set_fan_div(dev, buf, count, offset - 1); \
  342. } \
  343. static DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
  344. show_fan##offset, NULL); \
  345. static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
  346. show_fan##offset##_min, set_fan##offset##_min); \
  347. static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
  348. show_fan##offset##_div, set_fan##offset##_div);
  349. sysfs_fan(1);
  350. sysfs_fan(2);
  351. sysfs_fan(3);
  352. /* 4 Temp. Sensors */
  353. static int sprintf_temp_from_reg(u16 reg, char *buf, int nr)
  354. {
  355. int ret = 0;
  356. switch (nr) {
  357. case 1: case 2:
  358. ret = sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(reg));
  359. break;
  360. case 0: case 3: default:
  361. ret = sprintf(buf, "%d\n", TEMP_FROM_REG(reg));
  362. break;
  363. }
  364. return ret;
  365. }
  366. #define show_temp_reg(reg) \
  367. static ssize_t show_##reg(struct device *dev, char *buf, int nr) \
  368. { \
  369. struct asb100_data *data = asb100_update_device(dev); \
  370. return sprintf_temp_from_reg(data->reg[nr], buf, nr); \
  371. }
  372. show_temp_reg(temp);
  373. show_temp_reg(temp_max);
  374. show_temp_reg(temp_hyst);
  375. #define set_temp_reg(REG, reg) \
  376. static ssize_t set_##reg(struct device *dev, const char *buf, \
  377. size_t count, int nr) \
  378. { \
  379. struct i2c_client *client = to_i2c_client(dev); \
  380. struct asb100_data *data = i2c_get_clientdata(client); \
  381. long val = simple_strtol(buf, NULL, 10); \
  382. \
  383. mutex_lock(&data->update_lock); \
  384. switch (nr) { \
  385. case 1: case 2: \
  386. data->reg[nr] = LM75_TEMP_TO_REG(val); \
  387. break; \
  388. case 0: case 3: default: \
  389. data->reg[nr] = TEMP_TO_REG(val); \
  390. break; \
  391. } \
  392. asb100_write_value(client, ASB100_REG_TEMP_##REG(nr+1), \
  393. data->reg[nr]); \
  394. mutex_unlock(&data->update_lock); \
  395. return count; \
  396. }
  397. set_temp_reg(MAX, temp_max);
  398. set_temp_reg(HYST, temp_hyst);
  399. #define sysfs_temp(num) \
  400. static ssize_t show_temp##num(struct device *dev, struct device_attribute *attr, char *buf) \
  401. { \
  402. return show_temp(dev, buf, num-1); \
  403. } \
  404. static DEVICE_ATTR(temp##num##_input, S_IRUGO, show_temp##num, NULL); \
  405. static ssize_t show_temp_max##num(struct device *dev, struct device_attribute *attr, char *buf) \
  406. { \
  407. return show_temp_max(dev, buf, num-1); \
  408. } \
  409. static ssize_t set_temp_max##num(struct device *dev, struct device_attribute *attr, const char *buf, \
  410. size_t count) \
  411. { \
  412. return set_temp_max(dev, buf, count, num-1); \
  413. } \
  414. static DEVICE_ATTR(temp##num##_max, S_IRUGO | S_IWUSR, \
  415. show_temp_max##num, set_temp_max##num); \
  416. static ssize_t show_temp_hyst##num(struct device *dev, struct device_attribute *attr, char *buf) \
  417. { \
  418. return show_temp_hyst(dev, buf, num-1); \
  419. } \
  420. static ssize_t set_temp_hyst##num(struct device *dev, struct device_attribute *attr, const char *buf, \
  421. size_t count) \
  422. { \
  423. return set_temp_hyst(dev, buf, count, num-1); \
  424. } \
  425. static DEVICE_ATTR(temp##num##_max_hyst, S_IRUGO | S_IWUSR, \
  426. show_temp_hyst##num, set_temp_hyst##num);
  427. sysfs_temp(1);
  428. sysfs_temp(2);
  429. sysfs_temp(3);
  430. sysfs_temp(4);
  431. /* VID */
  432. static ssize_t show_vid(struct device *dev, struct device_attribute *attr, char *buf)
  433. {
  434. struct asb100_data *data = asb100_update_device(dev);
  435. return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
  436. }
  437. static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
  438. /* VRM */
  439. static ssize_t show_vrm(struct device *dev, struct device_attribute *attr, char *buf)
  440. {
  441. struct asb100_data *data = dev_get_drvdata(dev);
  442. return sprintf(buf, "%d\n", data->vrm);
  443. }
  444. static ssize_t set_vrm(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  445. {
  446. struct asb100_data *data = dev_get_drvdata(dev);
  447. data->vrm = simple_strtoul(buf, NULL, 10);
  448. return count;
  449. }
  450. /* Alarms */
  451. static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
  452. static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
  453. {
  454. struct asb100_data *data = asb100_update_device(dev);
  455. return sprintf(buf, "%u\n", data->alarms);
  456. }
  457. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  458. /* 1 PWM */
  459. static ssize_t show_pwm1(struct device *dev, struct device_attribute *attr, char *buf)
  460. {
  461. struct asb100_data *data = asb100_update_device(dev);
  462. return sprintf(buf, "%d\n", ASB100_PWM_FROM_REG(data->pwm & 0x0f));
  463. }
  464. static ssize_t set_pwm1(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  465. {
  466. struct i2c_client *client = to_i2c_client(dev);
  467. struct asb100_data *data = i2c_get_clientdata(client);
  468. unsigned long val = simple_strtoul(buf, NULL, 10);
  469. mutex_lock(&data->update_lock);
  470. data->pwm &= 0x80; /* keep the enable bit */
  471. data->pwm |= (0x0f & ASB100_PWM_TO_REG(val));
  472. asb100_write_value(client, ASB100_REG_PWM1, data->pwm);
  473. mutex_unlock(&data->update_lock);
  474. return count;
  475. }
  476. static ssize_t show_pwm_enable1(struct device *dev, struct device_attribute *attr, char *buf)
  477. {
  478. struct asb100_data *data = asb100_update_device(dev);
  479. return sprintf(buf, "%d\n", (data->pwm & 0x80) ? 1 : 0);
  480. }
  481. static ssize_t set_pwm_enable1(struct device *dev, struct device_attribute *attr, const char *buf,
  482. size_t count)
  483. {
  484. struct i2c_client *client = to_i2c_client(dev);
  485. struct asb100_data *data = i2c_get_clientdata(client);
  486. unsigned long val = simple_strtoul(buf, NULL, 10);
  487. mutex_lock(&data->update_lock);
  488. data->pwm &= 0x0f; /* keep the duty cycle bits */
  489. data->pwm |= (val ? 0x80 : 0x00);
  490. asb100_write_value(client, ASB100_REG_PWM1, data->pwm);
  491. mutex_unlock(&data->update_lock);
  492. return count;
  493. }
  494. static DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm1, set_pwm1);
  495. static DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
  496. show_pwm_enable1, set_pwm_enable1);
  497. static struct attribute *asb100_attributes[] = {
  498. &dev_attr_in0_input.attr,
  499. &dev_attr_in0_min.attr,
  500. &dev_attr_in0_max.attr,
  501. &dev_attr_in1_input.attr,
  502. &dev_attr_in1_min.attr,
  503. &dev_attr_in1_max.attr,
  504. &dev_attr_in2_input.attr,
  505. &dev_attr_in2_min.attr,
  506. &dev_attr_in2_max.attr,
  507. &dev_attr_in3_input.attr,
  508. &dev_attr_in3_min.attr,
  509. &dev_attr_in3_max.attr,
  510. &dev_attr_in4_input.attr,
  511. &dev_attr_in4_min.attr,
  512. &dev_attr_in4_max.attr,
  513. &dev_attr_in5_input.attr,
  514. &dev_attr_in5_min.attr,
  515. &dev_attr_in5_max.attr,
  516. &dev_attr_in6_input.attr,
  517. &dev_attr_in6_min.attr,
  518. &dev_attr_in6_max.attr,
  519. &dev_attr_fan1_input.attr,
  520. &dev_attr_fan1_min.attr,
  521. &dev_attr_fan1_div.attr,
  522. &dev_attr_fan2_input.attr,
  523. &dev_attr_fan2_min.attr,
  524. &dev_attr_fan2_div.attr,
  525. &dev_attr_fan3_input.attr,
  526. &dev_attr_fan3_min.attr,
  527. &dev_attr_fan3_div.attr,
  528. &dev_attr_temp1_input.attr,
  529. &dev_attr_temp1_max.attr,
  530. &dev_attr_temp1_max_hyst.attr,
  531. &dev_attr_temp2_input.attr,
  532. &dev_attr_temp2_max.attr,
  533. &dev_attr_temp2_max_hyst.attr,
  534. &dev_attr_temp3_input.attr,
  535. &dev_attr_temp3_max.attr,
  536. &dev_attr_temp3_max_hyst.attr,
  537. &dev_attr_temp4_input.attr,
  538. &dev_attr_temp4_max.attr,
  539. &dev_attr_temp4_max_hyst.attr,
  540. &dev_attr_cpu0_vid.attr,
  541. &dev_attr_vrm.attr,
  542. &dev_attr_alarms.attr,
  543. &dev_attr_pwm1.attr,
  544. &dev_attr_pwm1_enable.attr,
  545. NULL
  546. };
  547. static const struct attribute_group asb100_group = {
  548. .attrs = asb100_attributes,
  549. };
  550. /* This function is called when:
  551. asb100_driver is inserted (when this module is loaded), for each
  552. available adapter
  553. when a new adapter is inserted (and asb100_driver is still present)
  554. */
  555. static int asb100_attach_adapter(struct i2c_adapter *adapter)
  556. {
  557. if (!(adapter->class & I2C_CLASS_HWMON))
  558. return 0;
  559. return i2c_probe(adapter, &addr_data, asb100_detect);
  560. }
  561. static int asb100_detect_subclients(struct i2c_adapter *adapter, int address,
  562. int kind, struct i2c_client *new_client)
  563. {
  564. int i, id, err;
  565. struct asb100_data *data = i2c_get_clientdata(new_client);
  566. data->lm75[0] = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
  567. if (!(data->lm75[0])) {
  568. err = -ENOMEM;
  569. goto ERROR_SC_0;
  570. }
  571. data->lm75[1] = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
  572. if (!(data->lm75[1])) {
  573. err = -ENOMEM;
  574. goto ERROR_SC_1;
  575. }
  576. id = i2c_adapter_id(adapter);
  577. if (force_subclients[0] == id && force_subclients[1] == address) {
  578. for (i = 2; i <= 3; i++) {
  579. if (force_subclients[i] < 0x48 ||
  580. force_subclients[i] > 0x4f) {
  581. dev_err(&new_client->dev, "invalid subclient "
  582. "address %d; must be 0x48-0x4f\n",
  583. force_subclients[i]);
  584. err = -ENODEV;
  585. goto ERROR_SC_2;
  586. }
  587. }
  588. asb100_write_value(new_client, ASB100_REG_I2C_SUBADDR,
  589. (force_subclients[2] & 0x07) |
  590. ((force_subclients[3] & 0x07) <<4));
  591. data->lm75[0]->addr = force_subclients[2];
  592. data->lm75[1]->addr = force_subclients[3];
  593. } else {
  594. int val = asb100_read_value(new_client, ASB100_REG_I2C_SUBADDR);
  595. data->lm75[0]->addr = 0x48 + (val & 0x07);
  596. data->lm75[1]->addr = 0x48 + ((val >> 4) & 0x07);
  597. }
  598. if(data->lm75[0]->addr == data->lm75[1]->addr) {
  599. dev_err(&new_client->dev, "duplicate addresses 0x%x "
  600. "for subclients\n", data->lm75[0]->addr);
  601. err = -ENODEV;
  602. goto ERROR_SC_2;
  603. }
  604. for (i = 0; i <= 1; i++) {
  605. i2c_set_clientdata(data->lm75[i], NULL);
  606. data->lm75[i]->adapter = adapter;
  607. data->lm75[i]->driver = &asb100_driver;
  608. data->lm75[i]->flags = 0;
  609. strlcpy(data->lm75[i]->name, "asb100 subclient", I2C_NAME_SIZE);
  610. }
  611. if ((err = i2c_attach_client(data->lm75[0]))) {
  612. dev_err(&new_client->dev, "subclient %d registration "
  613. "at address 0x%x failed.\n", i, data->lm75[0]->addr);
  614. goto ERROR_SC_2;
  615. }
  616. if ((err = i2c_attach_client(data->lm75[1]))) {
  617. dev_err(&new_client->dev, "subclient %d registration "
  618. "at address 0x%x failed.\n", i, data->lm75[1]->addr);
  619. goto ERROR_SC_3;
  620. }
  621. return 0;
  622. /* Undo inits in case of errors */
  623. ERROR_SC_3:
  624. i2c_detach_client(data->lm75[0]);
  625. ERROR_SC_2:
  626. kfree(data->lm75[1]);
  627. ERROR_SC_1:
  628. kfree(data->lm75[0]);
  629. ERROR_SC_0:
  630. return err;
  631. }
  632. static int asb100_detect(struct i2c_adapter *adapter, int address, int kind)
  633. {
  634. int err;
  635. struct i2c_client *new_client;
  636. struct asb100_data *data;
  637. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
  638. pr_debug("asb100.o: detect failed, "
  639. "smbus byte data not supported!\n");
  640. err = -ENODEV;
  641. goto ERROR0;
  642. }
  643. /* OK. For now, we presume we have a valid client. We now create the
  644. client structure, even though we cannot fill it completely yet.
  645. But it allows us to access asb100_{read,write}_value. */
  646. if (!(data = kzalloc(sizeof(struct asb100_data), GFP_KERNEL))) {
  647. pr_debug("asb100.o: detect failed, kzalloc failed!\n");
  648. err = -ENOMEM;
  649. goto ERROR0;
  650. }
  651. new_client = &data->client;
  652. mutex_init(&data->lock);
  653. i2c_set_clientdata(new_client, data);
  654. new_client->addr = address;
  655. new_client->adapter = adapter;
  656. new_client->driver = &asb100_driver;
  657. new_client->flags = 0;
  658. /* Now, we do the remaining detection. */
  659. /* The chip may be stuck in some other bank than bank 0. This may
  660. make reading other information impossible. Specify a force=... or
  661. force_*=... parameter, and the chip will be reset to the right
  662. bank. */
  663. if (kind < 0) {
  664. int val1 = asb100_read_value(new_client, ASB100_REG_BANK);
  665. int val2 = asb100_read_value(new_client, ASB100_REG_CHIPMAN);
  666. /* If we're in bank 0 */
  667. if ( (!(val1 & 0x07)) &&
  668. /* Check for ASB100 ID (low byte) */
  669. ( ((!(val1 & 0x80)) && (val2 != 0x94)) ||
  670. /* Check for ASB100 ID (high byte ) */
  671. ((val1 & 0x80) && (val2 != 0x06)) ) ) {
  672. pr_debug("asb100.o: detect failed, "
  673. "bad chip id 0x%02x!\n", val2);
  674. err = -ENODEV;
  675. goto ERROR1;
  676. }
  677. } /* kind < 0 */
  678. /* We have either had a force parameter, or we have already detected
  679. Winbond. Put it now into bank 0 and Vendor ID High Byte */
  680. asb100_write_value(new_client, ASB100_REG_BANK,
  681. (asb100_read_value(new_client, ASB100_REG_BANK) & 0x78) | 0x80);
  682. /* Determine the chip type. */
  683. if (kind <= 0) {
  684. int val1 = asb100_read_value(new_client, ASB100_REG_WCHIPID);
  685. int val2 = asb100_read_value(new_client, ASB100_REG_CHIPMAN);
  686. if ((val1 == 0x31) && (val2 == 0x06))
  687. kind = asb100;
  688. else {
  689. if (kind == 0)
  690. dev_warn(&new_client->dev, "ignoring "
  691. "'force' parameter for unknown chip "
  692. "at adapter %d, address 0x%02x.\n",
  693. i2c_adapter_id(adapter), address);
  694. err = -ENODEV;
  695. goto ERROR1;
  696. }
  697. }
  698. /* Fill in remaining client fields and put it into the global list */
  699. strlcpy(new_client->name, "asb100", I2C_NAME_SIZE);
  700. data->type = kind;
  701. data->valid = 0;
  702. mutex_init(&data->update_lock);
  703. /* Tell the I2C layer a new client has arrived */
  704. if ((err = i2c_attach_client(new_client)))
  705. goto ERROR1;
  706. /* Attach secondary lm75 clients */
  707. if ((err = asb100_detect_subclients(adapter, address, kind,
  708. new_client)))
  709. goto ERROR2;
  710. /* Initialize the chip */
  711. asb100_init_client(new_client);
  712. /* A few vars need to be filled upon startup */
  713. data->fan_min[0] = asb100_read_value(new_client, ASB100_REG_FAN_MIN(0));
  714. data->fan_min[1] = asb100_read_value(new_client, ASB100_REG_FAN_MIN(1));
  715. data->fan_min[2] = asb100_read_value(new_client, ASB100_REG_FAN_MIN(2));
  716. /* Register sysfs hooks */
  717. if ((err = sysfs_create_group(&new_client->dev.kobj, &asb100_group)))
  718. goto ERROR3;
  719. data->hwmon_dev = hwmon_device_register(&new_client->dev);
  720. if (IS_ERR(data->hwmon_dev)) {
  721. err = PTR_ERR(data->hwmon_dev);
  722. goto ERROR4;
  723. }
  724. return 0;
  725. ERROR4:
  726. sysfs_remove_group(&new_client->dev.kobj, &asb100_group);
  727. ERROR3:
  728. i2c_detach_client(data->lm75[1]);
  729. i2c_detach_client(data->lm75[0]);
  730. kfree(data->lm75[1]);
  731. kfree(data->lm75[0]);
  732. ERROR2:
  733. i2c_detach_client(new_client);
  734. ERROR1:
  735. kfree(data);
  736. ERROR0:
  737. return err;
  738. }
  739. static int asb100_detach_client(struct i2c_client *client)
  740. {
  741. struct asb100_data *data = i2c_get_clientdata(client);
  742. int err;
  743. /* main client */
  744. if (data) {
  745. hwmon_device_unregister(data->hwmon_dev);
  746. sysfs_remove_group(&client->dev.kobj, &asb100_group);
  747. }
  748. if ((err = i2c_detach_client(client)))
  749. return err;
  750. /* main client */
  751. if (data)
  752. kfree(data);
  753. /* subclient */
  754. else
  755. kfree(client);
  756. return 0;
  757. }
  758. /* The SMBus locks itself, usually, but nothing may access the chip between
  759. bank switches. */
  760. static int asb100_read_value(struct i2c_client *client, u16 reg)
  761. {
  762. struct asb100_data *data = i2c_get_clientdata(client);
  763. struct i2c_client *cl;
  764. int res, bank;
  765. mutex_lock(&data->lock);
  766. bank = (reg >> 8) & 0x0f;
  767. if (bank > 2)
  768. /* switch banks */
  769. i2c_smbus_write_byte_data(client, ASB100_REG_BANK, bank);
  770. if (bank == 0 || bank > 2) {
  771. res = i2c_smbus_read_byte_data(client, reg & 0xff);
  772. } else {
  773. /* switch to subclient */
  774. cl = data->lm75[bank - 1];
  775. /* convert from ISA to LM75 I2C addresses */
  776. switch (reg & 0xff) {
  777. case 0x50: /* TEMP */
  778. res = swab16(i2c_smbus_read_word_data (cl, 0));
  779. break;
  780. case 0x52: /* CONFIG */
  781. res = i2c_smbus_read_byte_data(cl, 1);
  782. break;
  783. case 0x53: /* HYST */
  784. res = swab16(i2c_smbus_read_word_data (cl, 2));
  785. break;
  786. case 0x55: /* MAX */
  787. default:
  788. res = swab16(i2c_smbus_read_word_data (cl, 3));
  789. break;
  790. }
  791. }
  792. if (bank > 2)
  793. i2c_smbus_write_byte_data(client, ASB100_REG_BANK, 0);
  794. mutex_unlock(&data->lock);
  795. return res;
  796. }
  797. static void asb100_write_value(struct i2c_client *client, u16 reg, u16 value)
  798. {
  799. struct asb100_data *data = i2c_get_clientdata(client);
  800. struct i2c_client *cl;
  801. int bank;
  802. mutex_lock(&data->lock);
  803. bank = (reg >> 8) & 0x0f;
  804. if (bank > 2)
  805. /* switch banks */
  806. i2c_smbus_write_byte_data(client, ASB100_REG_BANK, bank);
  807. if (bank == 0 || bank > 2) {
  808. i2c_smbus_write_byte_data(client, reg & 0xff, value & 0xff);
  809. } else {
  810. /* switch to subclient */
  811. cl = data->lm75[bank - 1];
  812. /* convert from ISA to LM75 I2C addresses */
  813. switch (reg & 0xff) {
  814. case 0x52: /* CONFIG */
  815. i2c_smbus_write_byte_data(cl, 1, value & 0xff);
  816. break;
  817. case 0x53: /* HYST */
  818. i2c_smbus_write_word_data(cl, 2, swab16(value));
  819. break;
  820. case 0x55: /* MAX */
  821. i2c_smbus_write_word_data(cl, 3, swab16(value));
  822. break;
  823. }
  824. }
  825. if (bank > 2)
  826. i2c_smbus_write_byte_data(client, ASB100_REG_BANK, 0);
  827. mutex_unlock(&data->lock);
  828. }
  829. static void asb100_init_client(struct i2c_client *client)
  830. {
  831. struct asb100_data *data = i2c_get_clientdata(client);
  832. int vid = 0;
  833. vid = asb100_read_value(client, ASB100_REG_VID_FANDIV) & 0x0f;
  834. vid |= (asb100_read_value(client, ASB100_REG_CHIPID) & 0x01) << 4;
  835. data->vrm = vid_which_vrm();
  836. vid = vid_from_reg(vid, data->vrm);
  837. /* Start monitoring */
  838. asb100_write_value(client, ASB100_REG_CONFIG,
  839. (asb100_read_value(client, ASB100_REG_CONFIG) & 0xf7) | 0x01);
  840. }
  841. static struct asb100_data *asb100_update_device(struct device *dev)
  842. {
  843. struct i2c_client *client = to_i2c_client(dev);
  844. struct asb100_data *data = i2c_get_clientdata(client);
  845. int i;
  846. mutex_lock(&data->update_lock);
  847. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  848. || !data->valid) {
  849. dev_dbg(&client->dev, "starting device update...\n");
  850. /* 7 voltage inputs */
  851. for (i = 0; i < 7; i++) {
  852. data->in[i] = asb100_read_value(client,
  853. ASB100_REG_IN(i));
  854. data->in_min[i] = asb100_read_value(client,
  855. ASB100_REG_IN_MIN(i));
  856. data->in_max[i] = asb100_read_value(client,
  857. ASB100_REG_IN_MAX(i));
  858. }
  859. /* 3 fan inputs */
  860. for (i = 0; i < 3; i++) {
  861. data->fan[i] = asb100_read_value(client,
  862. ASB100_REG_FAN(i));
  863. data->fan_min[i] = asb100_read_value(client,
  864. ASB100_REG_FAN_MIN(i));
  865. }
  866. /* 4 temperature inputs */
  867. for (i = 1; i <= 4; i++) {
  868. data->temp[i-1] = asb100_read_value(client,
  869. ASB100_REG_TEMP(i));
  870. data->temp_max[i-1] = asb100_read_value(client,
  871. ASB100_REG_TEMP_MAX(i));
  872. data->temp_hyst[i-1] = asb100_read_value(client,
  873. ASB100_REG_TEMP_HYST(i));
  874. }
  875. /* VID and fan divisors */
  876. i = asb100_read_value(client, ASB100_REG_VID_FANDIV);
  877. data->vid = i & 0x0f;
  878. data->vid |= (asb100_read_value(client,
  879. ASB100_REG_CHIPID) & 0x01) << 4;
  880. data->fan_div[0] = (i >> 4) & 0x03;
  881. data->fan_div[1] = (i >> 6) & 0x03;
  882. data->fan_div[2] = (asb100_read_value(client,
  883. ASB100_REG_PIN) >> 6) & 0x03;
  884. /* PWM */
  885. data->pwm = asb100_read_value(client, ASB100_REG_PWM1);
  886. /* alarms */
  887. data->alarms = asb100_read_value(client, ASB100_REG_ALARM1) +
  888. (asb100_read_value(client, ASB100_REG_ALARM2) << 8);
  889. data->last_updated = jiffies;
  890. data->valid = 1;
  891. dev_dbg(&client->dev, "... device update complete\n");
  892. }
  893. mutex_unlock(&data->update_lock);
  894. return data;
  895. }
  896. static int __init asb100_init(void)
  897. {
  898. return i2c_add_driver(&asb100_driver);
  899. }
  900. static void __exit asb100_exit(void)
  901. {
  902. i2c_del_driver(&asb100_driver);
  903. }
  904. MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
  905. MODULE_DESCRIPTION("ASB100 Bach driver");
  906. MODULE_LICENSE("GPL");
  907. module_init(asb100_init);
  908. module_exit(asb100_exit);