asb100.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  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(int 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 class_device *class_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. .id = I2C_DRIVERID_ASB100,
  183. .attach_adapter = asb100_attach_adapter,
  184. .detach_client = asb100_detach_client,
  185. };
  186. /* 7 Voltages */
  187. #define show_in_reg(reg) \
  188. static ssize_t show_##reg (struct device *dev, char *buf, int nr) \
  189. { \
  190. struct asb100_data *data = asb100_update_device(dev); \
  191. return sprintf(buf, "%d\n", IN_FROM_REG(data->reg[nr])); \
  192. }
  193. show_in_reg(in)
  194. show_in_reg(in_min)
  195. show_in_reg(in_max)
  196. #define set_in_reg(REG, reg) \
  197. static ssize_t set_in_##reg(struct device *dev, const char *buf, \
  198. size_t count, int nr) \
  199. { \
  200. struct i2c_client *client = to_i2c_client(dev); \
  201. struct asb100_data *data = i2c_get_clientdata(client); \
  202. unsigned long val = simple_strtoul(buf, NULL, 10); \
  203. \
  204. mutex_lock(&data->update_lock); \
  205. data->in_##reg[nr] = IN_TO_REG(val); \
  206. asb100_write_value(client, ASB100_REG_IN_##REG(nr), \
  207. data->in_##reg[nr]); \
  208. mutex_unlock(&data->update_lock); \
  209. return count; \
  210. }
  211. set_in_reg(MIN, min)
  212. set_in_reg(MAX, max)
  213. #define sysfs_in(offset) \
  214. static ssize_t \
  215. show_in##offset (struct device *dev, struct device_attribute *attr, char *buf) \
  216. { \
  217. return show_in(dev, buf, offset); \
  218. } \
  219. static DEVICE_ATTR(in##offset##_input, S_IRUGO, \
  220. show_in##offset, NULL); \
  221. static ssize_t \
  222. show_in##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
  223. { \
  224. return show_in_min(dev, buf, offset); \
  225. } \
  226. static ssize_t \
  227. show_in##offset##_max (struct device *dev, struct device_attribute *attr, char *buf) \
  228. { \
  229. return show_in_max(dev, buf, offset); \
  230. } \
  231. static ssize_t set_in##offset##_min (struct device *dev, struct device_attribute *attr, \
  232. const char *buf, size_t count) \
  233. { \
  234. return set_in_min(dev, buf, count, offset); \
  235. } \
  236. static ssize_t set_in##offset##_max (struct device *dev, struct device_attribute *attr, \
  237. const char *buf, size_t count) \
  238. { \
  239. return set_in_max(dev, buf, count, offset); \
  240. } \
  241. static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
  242. show_in##offset##_min, set_in##offset##_min); \
  243. static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
  244. show_in##offset##_max, set_in##offset##_max);
  245. sysfs_in(0);
  246. sysfs_in(1);
  247. sysfs_in(2);
  248. sysfs_in(3);
  249. sysfs_in(4);
  250. sysfs_in(5);
  251. sysfs_in(6);
  252. /* 3 Fans */
  253. static ssize_t show_fan(struct device *dev, char *buf, int nr)
  254. {
  255. struct asb100_data *data = asb100_update_device(dev);
  256. return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
  257. DIV_FROM_REG(data->fan_div[nr])));
  258. }
  259. static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
  260. {
  261. struct asb100_data *data = asb100_update_device(dev);
  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 show_fan_div(struct device *dev, char *buf, int nr)
  266. {
  267. struct asb100_data *data = asb100_update_device(dev);
  268. return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
  269. }
  270. static ssize_t set_fan_min(struct device *dev, const char *buf,
  271. size_t count, int nr)
  272. {
  273. struct i2c_client *client = to_i2c_client(dev);
  274. struct asb100_data *data = i2c_get_clientdata(client);
  275. u32 val = simple_strtoul(buf, NULL, 10);
  276. mutex_lock(&data->update_lock);
  277. data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
  278. asb100_write_value(client, ASB100_REG_FAN_MIN(nr), data->fan_min[nr]);
  279. mutex_unlock(&data->update_lock);
  280. return count;
  281. }
  282. /* Note: we save and restore the fan minimum here, because its value is
  283. determined in part by the fan divisor. This follows the principle of
  284. least surprise; the user doesn't expect the fan minimum to change just
  285. because the divisor changed. */
  286. static ssize_t set_fan_div(struct device *dev, const char *buf,
  287. size_t count, int nr)
  288. {
  289. struct i2c_client *client = to_i2c_client(dev);
  290. struct asb100_data *data = i2c_get_clientdata(client);
  291. unsigned long min;
  292. unsigned long val = simple_strtoul(buf, NULL, 10);
  293. int reg;
  294. mutex_lock(&data->update_lock);
  295. min = FAN_FROM_REG(data->fan_min[nr],
  296. DIV_FROM_REG(data->fan_div[nr]));
  297. data->fan_div[nr] = DIV_TO_REG(val);
  298. switch(nr) {
  299. case 0: /* fan 1 */
  300. reg = asb100_read_value(client, ASB100_REG_VID_FANDIV);
  301. reg = (reg & 0xcf) | (data->fan_div[0] << 4);
  302. asb100_write_value(client, ASB100_REG_VID_FANDIV, reg);
  303. break;
  304. case 1: /* fan 2 */
  305. reg = asb100_read_value(client, ASB100_REG_VID_FANDIV);
  306. reg = (reg & 0x3f) | (data->fan_div[1] << 6);
  307. asb100_write_value(client, ASB100_REG_VID_FANDIV, reg);
  308. break;
  309. case 2: /* fan 3 */
  310. reg = asb100_read_value(client, ASB100_REG_PIN);
  311. reg = (reg & 0x3f) | (data->fan_div[2] << 6);
  312. asb100_write_value(client, ASB100_REG_PIN, reg);
  313. break;
  314. }
  315. data->fan_min[nr] =
  316. FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
  317. asb100_write_value(client, ASB100_REG_FAN_MIN(nr), data->fan_min[nr]);
  318. mutex_unlock(&data->update_lock);
  319. return count;
  320. }
  321. #define sysfs_fan(offset) \
  322. static ssize_t show_fan##offset(struct device *dev, struct device_attribute *attr, char *buf) \
  323. { \
  324. return show_fan(dev, buf, offset - 1); \
  325. } \
  326. static ssize_t show_fan##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \
  327. { \
  328. return show_fan_min(dev, buf, offset - 1); \
  329. } \
  330. static ssize_t show_fan##offset##_div(struct device *dev, struct device_attribute *attr, char *buf) \
  331. { \
  332. return show_fan_div(dev, buf, offset - 1); \
  333. } \
  334. static ssize_t set_fan##offset##_min(struct device *dev, struct device_attribute *attr, const char *buf, \
  335. size_t count) \
  336. { \
  337. return set_fan_min(dev, buf, count, offset - 1); \
  338. } \
  339. static ssize_t set_fan##offset##_div(struct device *dev, struct device_attribute *attr, const char *buf, \
  340. size_t count) \
  341. { \
  342. return set_fan_div(dev, buf, count, offset - 1); \
  343. } \
  344. static DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
  345. show_fan##offset, NULL); \
  346. static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
  347. show_fan##offset##_min, set_fan##offset##_min); \
  348. static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
  349. show_fan##offset##_div, set_fan##offset##_div);
  350. sysfs_fan(1);
  351. sysfs_fan(2);
  352. sysfs_fan(3);
  353. /* 4 Temp. Sensors */
  354. static int sprintf_temp_from_reg(u16 reg, char *buf, int nr)
  355. {
  356. int ret = 0;
  357. switch (nr) {
  358. case 1: case 2:
  359. ret = sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(reg));
  360. break;
  361. case 0: case 3: default:
  362. ret = sprintf(buf, "%d\n", TEMP_FROM_REG(reg));
  363. break;
  364. }
  365. return ret;
  366. }
  367. #define show_temp_reg(reg) \
  368. static ssize_t show_##reg(struct device *dev, char *buf, int nr) \
  369. { \
  370. struct asb100_data *data = asb100_update_device(dev); \
  371. return sprintf_temp_from_reg(data->reg[nr], buf, nr); \
  372. }
  373. show_temp_reg(temp);
  374. show_temp_reg(temp_max);
  375. show_temp_reg(temp_hyst);
  376. #define set_temp_reg(REG, reg) \
  377. static ssize_t set_##reg(struct device *dev, const char *buf, \
  378. size_t count, int nr) \
  379. { \
  380. struct i2c_client *client = to_i2c_client(dev); \
  381. struct asb100_data *data = i2c_get_clientdata(client); \
  382. unsigned long val = simple_strtoul(buf, NULL, 10); \
  383. \
  384. mutex_lock(&data->update_lock); \
  385. switch (nr) { \
  386. case 1: case 2: \
  387. data->reg[nr] = LM75_TEMP_TO_REG(val); \
  388. break; \
  389. case 0: case 3: default: \
  390. data->reg[nr] = TEMP_TO_REG(val); \
  391. break; \
  392. } \
  393. asb100_write_value(client, ASB100_REG_TEMP_##REG(nr+1), \
  394. data->reg[nr]); \
  395. mutex_unlock(&data->update_lock); \
  396. return count; \
  397. }
  398. set_temp_reg(MAX, temp_max);
  399. set_temp_reg(HYST, temp_hyst);
  400. #define sysfs_temp(num) \
  401. static ssize_t show_temp##num(struct device *dev, struct device_attribute *attr, char *buf) \
  402. { \
  403. return show_temp(dev, buf, num-1); \
  404. } \
  405. static DEVICE_ATTR(temp##num##_input, S_IRUGO, show_temp##num, NULL); \
  406. static ssize_t show_temp_max##num(struct device *dev, struct device_attribute *attr, char *buf) \
  407. { \
  408. return show_temp_max(dev, buf, num-1); \
  409. } \
  410. static ssize_t set_temp_max##num(struct device *dev, struct device_attribute *attr, const char *buf, \
  411. size_t count) \
  412. { \
  413. return set_temp_max(dev, buf, count, num-1); \
  414. } \
  415. static DEVICE_ATTR(temp##num##_max, S_IRUGO | S_IWUSR, \
  416. show_temp_max##num, set_temp_max##num); \
  417. static ssize_t show_temp_hyst##num(struct device *dev, struct device_attribute *attr, char *buf) \
  418. { \
  419. return show_temp_hyst(dev, buf, num-1); \
  420. } \
  421. static ssize_t set_temp_hyst##num(struct device *dev, struct device_attribute *attr, const char *buf, \
  422. size_t count) \
  423. { \
  424. return set_temp_hyst(dev, buf, count, num-1); \
  425. } \
  426. static DEVICE_ATTR(temp##num##_max_hyst, S_IRUGO | S_IWUSR, \
  427. show_temp_hyst##num, set_temp_hyst##num);
  428. sysfs_temp(1);
  429. sysfs_temp(2);
  430. sysfs_temp(3);
  431. sysfs_temp(4);
  432. /* VID */
  433. static ssize_t show_vid(struct device *dev, struct device_attribute *attr, char *buf)
  434. {
  435. struct asb100_data *data = asb100_update_device(dev);
  436. return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
  437. }
  438. static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
  439. /* VRM */
  440. static ssize_t show_vrm(struct device *dev, struct device_attribute *attr, char *buf)
  441. {
  442. struct asb100_data *data = asb100_update_device(dev);
  443. return sprintf(buf, "%d\n", data->vrm);
  444. }
  445. static ssize_t set_vrm(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  446. {
  447. struct i2c_client *client = to_i2c_client(dev);
  448. struct asb100_data *data = i2c_get_clientdata(client);
  449. unsigned long val = simple_strtoul(buf, NULL, 10);
  450. data->vrm = val;
  451. return count;
  452. }
  453. /* Alarms */
  454. static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
  455. static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
  456. {
  457. struct asb100_data *data = asb100_update_device(dev);
  458. return sprintf(buf, "%u\n", data->alarms);
  459. }
  460. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  461. /* 1 PWM */
  462. static ssize_t show_pwm1(struct device *dev, struct device_attribute *attr, char *buf)
  463. {
  464. struct asb100_data *data = asb100_update_device(dev);
  465. return sprintf(buf, "%d\n", ASB100_PWM_FROM_REG(data->pwm & 0x0f));
  466. }
  467. static ssize_t set_pwm1(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  468. {
  469. struct i2c_client *client = to_i2c_client(dev);
  470. struct asb100_data *data = i2c_get_clientdata(client);
  471. unsigned long val = simple_strtoul(buf, NULL, 10);
  472. mutex_lock(&data->update_lock);
  473. data->pwm &= 0x80; /* keep the enable bit */
  474. data->pwm |= (0x0f & ASB100_PWM_TO_REG(val));
  475. asb100_write_value(client, ASB100_REG_PWM1, data->pwm);
  476. mutex_unlock(&data->update_lock);
  477. return count;
  478. }
  479. static ssize_t show_pwm_enable1(struct device *dev, struct device_attribute *attr, char *buf)
  480. {
  481. struct asb100_data *data = asb100_update_device(dev);
  482. return sprintf(buf, "%d\n", (data->pwm & 0x80) ? 1 : 0);
  483. }
  484. static ssize_t set_pwm_enable1(struct device *dev, struct device_attribute *attr, const char *buf,
  485. size_t count)
  486. {
  487. struct i2c_client *client = to_i2c_client(dev);
  488. struct asb100_data *data = i2c_get_clientdata(client);
  489. unsigned long val = simple_strtoul(buf, NULL, 10);
  490. mutex_lock(&data->update_lock);
  491. data->pwm &= 0x0f; /* keep the duty cycle bits */
  492. data->pwm |= (val ? 0x80 : 0x00);
  493. asb100_write_value(client, ASB100_REG_PWM1, data->pwm);
  494. mutex_unlock(&data->update_lock);
  495. return count;
  496. }
  497. static DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm1, set_pwm1);
  498. static DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
  499. show_pwm_enable1, set_pwm_enable1);
  500. static struct attribute *asb100_attributes[] = {
  501. &dev_attr_in0_input.attr,
  502. &dev_attr_in0_min.attr,
  503. &dev_attr_in0_max.attr,
  504. &dev_attr_in1_input.attr,
  505. &dev_attr_in1_min.attr,
  506. &dev_attr_in1_max.attr,
  507. &dev_attr_in2_input.attr,
  508. &dev_attr_in2_min.attr,
  509. &dev_attr_in2_max.attr,
  510. &dev_attr_in3_input.attr,
  511. &dev_attr_in3_min.attr,
  512. &dev_attr_in3_max.attr,
  513. &dev_attr_in4_input.attr,
  514. &dev_attr_in4_min.attr,
  515. &dev_attr_in4_max.attr,
  516. &dev_attr_in5_input.attr,
  517. &dev_attr_in5_min.attr,
  518. &dev_attr_in5_max.attr,
  519. &dev_attr_in6_input.attr,
  520. &dev_attr_in6_min.attr,
  521. &dev_attr_in6_max.attr,
  522. &dev_attr_fan1_input.attr,
  523. &dev_attr_fan1_min.attr,
  524. &dev_attr_fan1_div.attr,
  525. &dev_attr_fan2_input.attr,
  526. &dev_attr_fan2_min.attr,
  527. &dev_attr_fan2_div.attr,
  528. &dev_attr_fan3_input.attr,
  529. &dev_attr_fan3_min.attr,
  530. &dev_attr_fan3_div.attr,
  531. &dev_attr_temp1_input.attr,
  532. &dev_attr_temp1_max.attr,
  533. &dev_attr_temp1_max_hyst.attr,
  534. &dev_attr_temp2_input.attr,
  535. &dev_attr_temp2_max.attr,
  536. &dev_attr_temp2_max_hyst.attr,
  537. &dev_attr_temp3_input.attr,
  538. &dev_attr_temp3_max.attr,
  539. &dev_attr_temp3_max_hyst.attr,
  540. &dev_attr_temp4_input.attr,
  541. &dev_attr_temp4_max.attr,
  542. &dev_attr_temp4_max_hyst.attr,
  543. &dev_attr_cpu0_vid.attr,
  544. &dev_attr_vrm.attr,
  545. &dev_attr_alarms.attr,
  546. &dev_attr_pwm1.attr,
  547. &dev_attr_pwm1_enable.attr,
  548. NULL
  549. };
  550. static const struct attribute_group asb100_group = {
  551. .attrs = asb100_attributes,
  552. };
  553. /* This function is called when:
  554. asb100_driver is inserted (when this module is loaded), for each
  555. available adapter
  556. when a new adapter is inserted (and asb100_driver is still present)
  557. */
  558. static int asb100_attach_adapter(struct i2c_adapter *adapter)
  559. {
  560. if (!(adapter->class & I2C_CLASS_HWMON))
  561. return 0;
  562. return i2c_probe(adapter, &addr_data, asb100_detect);
  563. }
  564. static int asb100_detect_subclients(struct i2c_adapter *adapter, int address,
  565. int kind, struct i2c_client *new_client)
  566. {
  567. int i, id, err;
  568. struct asb100_data *data = i2c_get_clientdata(new_client);
  569. data->lm75[0] = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
  570. if (!(data->lm75[0])) {
  571. err = -ENOMEM;
  572. goto ERROR_SC_0;
  573. }
  574. data->lm75[1] = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
  575. if (!(data->lm75[1])) {
  576. err = -ENOMEM;
  577. goto ERROR_SC_1;
  578. }
  579. id = i2c_adapter_id(adapter);
  580. if (force_subclients[0] == id && force_subclients[1] == address) {
  581. for (i = 2; i <= 3; i++) {
  582. if (force_subclients[i] < 0x48 ||
  583. force_subclients[i] > 0x4f) {
  584. dev_err(&new_client->dev, "invalid subclient "
  585. "address %d; must be 0x48-0x4f\n",
  586. force_subclients[i]);
  587. err = -ENODEV;
  588. goto ERROR_SC_2;
  589. }
  590. }
  591. asb100_write_value(new_client, ASB100_REG_I2C_SUBADDR,
  592. (force_subclients[2] & 0x07) |
  593. ((force_subclients[3] & 0x07) <<4));
  594. data->lm75[0]->addr = force_subclients[2];
  595. data->lm75[1]->addr = force_subclients[3];
  596. } else {
  597. int val = asb100_read_value(new_client, ASB100_REG_I2C_SUBADDR);
  598. data->lm75[0]->addr = 0x48 + (val & 0x07);
  599. data->lm75[1]->addr = 0x48 + ((val >> 4) & 0x07);
  600. }
  601. if(data->lm75[0]->addr == data->lm75[1]->addr) {
  602. dev_err(&new_client->dev, "duplicate addresses 0x%x "
  603. "for subclients\n", data->lm75[0]->addr);
  604. err = -ENODEV;
  605. goto ERROR_SC_2;
  606. }
  607. for (i = 0; i <= 1; i++) {
  608. i2c_set_clientdata(data->lm75[i], NULL);
  609. data->lm75[i]->adapter = adapter;
  610. data->lm75[i]->driver = &asb100_driver;
  611. data->lm75[i]->flags = 0;
  612. strlcpy(data->lm75[i]->name, "asb100 subclient", I2C_NAME_SIZE);
  613. }
  614. if ((err = i2c_attach_client(data->lm75[0]))) {
  615. dev_err(&new_client->dev, "subclient %d registration "
  616. "at address 0x%x failed.\n", i, data->lm75[0]->addr);
  617. goto ERROR_SC_2;
  618. }
  619. if ((err = i2c_attach_client(data->lm75[1]))) {
  620. dev_err(&new_client->dev, "subclient %d registration "
  621. "at address 0x%x failed.\n", i, data->lm75[1]->addr);
  622. goto ERROR_SC_3;
  623. }
  624. return 0;
  625. /* Undo inits in case of errors */
  626. ERROR_SC_3:
  627. i2c_detach_client(data->lm75[0]);
  628. ERROR_SC_2:
  629. kfree(data->lm75[1]);
  630. ERROR_SC_1:
  631. kfree(data->lm75[0]);
  632. ERROR_SC_0:
  633. return err;
  634. }
  635. static int asb100_detect(struct i2c_adapter *adapter, int address, int kind)
  636. {
  637. int err;
  638. struct i2c_client *new_client;
  639. struct asb100_data *data;
  640. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
  641. pr_debug("asb100.o: detect failed, "
  642. "smbus byte data not supported!\n");
  643. err = -ENODEV;
  644. goto ERROR0;
  645. }
  646. /* OK. For now, we presume we have a valid client. We now create the
  647. client structure, even though we cannot fill it completely yet.
  648. But it allows us to access asb100_{read,write}_value. */
  649. if (!(data = kzalloc(sizeof(struct asb100_data), GFP_KERNEL))) {
  650. pr_debug("asb100.o: detect failed, kzalloc failed!\n");
  651. err = -ENOMEM;
  652. goto ERROR0;
  653. }
  654. new_client = &data->client;
  655. mutex_init(&data->lock);
  656. i2c_set_clientdata(new_client, data);
  657. new_client->addr = address;
  658. new_client->adapter = adapter;
  659. new_client->driver = &asb100_driver;
  660. new_client->flags = 0;
  661. /* Now, we do the remaining detection. */
  662. /* The chip may be stuck in some other bank than bank 0. This may
  663. make reading other information impossible. Specify a force=... or
  664. force_*=... parameter, and the chip will be reset to the right
  665. bank. */
  666. if (kind < 0) {
  667. int val1 = asb100_read_value(new_client, ASB100_REG_BANK);
  668. int val2 = asb100_read_value(new_client, ASB100_REG_CHIPMAN);
  669. /* If we're in bank 0 */
  670. if ( (!(val1 & 0x07)) &&
  671. /* Check for ASB100 ID (low byte) */
  672. ( ((!(val1 & 0x80)) && (val2 != 0x94)) ||
  673. /* Check for ASB100 ID (high byte ) */
  674. ((val1 & 0x80) && (val2 != 0x06)) ) ) {
  675. pr_debug("asb100.o: detect failed, "
  676. "bad chip id 0x%02x!\n", val2);
  677. err = -ENODEV;
  678. goto ERROR1;
  679. }
  680. } /* kind < 0 */
  681. /* We have either had a force parameter, or we have already detected
  682. Winbond. Put it now into bank 0 and Vendor ID High Byte */
  683. asb100_write_value(new_client, ASB100_REG_BANK,
  684. (asb100_read_value(new_client, ASB100_REG_BANK) & 0x78) | 0x80);
  685. /* Determine the chip type. */
  686. if (kind <= 0) {
  687. int val1 = asb100_read_value(new_client, ASB100_REG_WCHIPID);
  688. int val2 = asb100_read_value(new_client, ASB100_REG_CHIPMAN);
  689. if ((val1 == 0x31) && (val2 == 0x06))
  690. kind = asb100;
  691. else {
  692. if (kind == 0)
  693. dev_warn(&new_client->dev, "ignoring "
  694. "'force' parameter for unknown chip "
  695. "at adapter %d, address 0x%02x.\n",
  696. i2c_adapter_id(adapter), address);
  697. err = -ENODEV;
  698. goto ERROR1;
  699. }
  700. }
  701. /* Fill in remaining client fields and put it into the global list */
  702. strlcpy(new_client->name, "asb100", I2C_NAME_SIZE);
  703. data->type = kind;
  704. data->valid = 0;
  705. mutex_init(&data->update_lock);
  706. /* Tell the I2C layer a new client has arrived */
  707. if ((err = i2c_attach_client(new_client)))
  708. goto ERROR1;
  709. /* Attach secondary lm75 clients */
  710. if ((err = asb100_detect_subclients(adapter, address, kind,
  711. new_client)))
  712. goto ERROR2;
  713. /* Initialize the chip */
  714. asb100_init_client(new_client);
  715. /* A few vars need to be filled upon startup */
  716. data->fan_min[0] = asb100_read_value(new_client, ASB100_REG_FAN_MIN(0));
  717. data->fan_min[1] = asb100_read_value(new_client, ASB100_REG_FAN_MIN(1));
  718. data->fan_min[2] = asb100_read_value(new_client, ASB100_REG_FAN_MIN(2));
  719. /* Register sysfs hooks */
  720. if ((err = sysfs_create_group(&new_client->dev.kobj, &asb100_group)))
  721. goto ERROR3;
  722. data->class_dev = hwmon_device_register(&new_client->dev);
  723. if (IS_ERR(data->class_dev)) {
  724. err = PTR_ERR(data->class_dev);
  725. goto ERROR4;
  726. }
  727. return 0;
  728. ERROR4:
  729. sysfs_remove_group(&new_client->dev.kobj, &asb100_group);
  730. ERROR3:
  731. i2c_detach_client(data->lm75[1]);
  732. i2c_detach_client(data->lm75[0]);
  733. kfree(data->lm75[1]);
  734. kfree(data->lm75[0]);
  735. ERROR2:
  736. i2c_detach_client(new_client);
  737. ERROR1:
  738. kfree(data);
  739. ERROR0:
  740. return err;
  741. }
  742. static int asb100_detach_client(struct i2c_client *client)
  743. {
  744. struct asb100_data *data = i2c_get_clientdata(client);
  745. int err;
  746. /* main client */
  747. if (data) {
  748. hwmon_device_unregister(data->class_dev);
  749. sysfs_remove_group(&client->dev.kobj, &asb100_group);
  750. }
  751. if ((err = i2c_detach_client(client)))
  752. return err;
  753. /* main client */
  754. if (data)
  755. kfree(data);
  756. /* subclient */
  757. else
  758. kfree(client);
  759. return 0;
  760. }
  761. /* The SMBus locks itself, usually, but nothing may access the chip between
  762. bank switches. */
  763. static int asb100_read_value(struct i2c_client *client, u16 reg)
  764. {
  765. struct asb100_data *data = i2c_get_clientdata(client);
  766. struct i2c_client *cl;
  767. int res, bank;
  768. mutex_lock(&data->lock);
  769. bank = (reg >> 8) & 0x0f;
  770. if (bank > 2)
  771. /* switch banks */
  772. i2c_smbus_write_byte_data(client, ASB100_REG_BANK, bank);
  773. if (bank == 0 || bank > 2) {
  774. res = i2c_smbus_read_byte_data(client, reg & 0xff);
  775. } else {
  776. /* switch to subclient */
  777. cl = data->lm75[bank - 1];
  778. /* convert from ISA to LM75 I2C addresses */
  779. switch (reg & 0xff) {
  780. case 0x50: /* TEMP */
  781. res = swab16(i2c_smbus_read_word_data (cl, 0));
  782. break;
  783. case 0x52: /* CONFIG */
  784. res = i2c_smbus_read_byte_data(cl, 1);
  785. break;
  786. case 0x53: /* HYST */
  787. res = swab16(i2c_smbus_read_word_data (cl, 2));
  788. break;
  789. case 0x55: /* MAX */
  790. default:
  791. res = swab16(i2c_smbus_read_word_data (cl, 3));
  792. break;
  793. }
  794. }
  795. if (bank > 2)
  796. i2c_smbus_write_byte_data(client, ASB100_REG_BANK, 0);
  797. mutex_unlock(&data->lock);
  798. return res;
  799. }
  800. static void asb100_write_value(struct i2c_client *client, u16 reg, u16 value)
  801. {
  802. struct asb100_data *data = i2c_get_clientdata(client);
  803. struct i2c_client *cl;
  804. int bank;
  805. mutex_lock(&data->lock);
  806. bank = (reg >> 8) & 0x0f;
  807. if (bank > 2)
  808. /* switch banks */
  809. i2c_smbus_write_byte_data(client, ASB100_REG_BANK, bank);
  810. if (bank == 0 || bank > 2) {
  811. i2c_smbus_write_byte_data(client, reg & 0xff, value & 0xff);
  812. } else {
  813. /* switch to subclient */
  814. cl = data->lm75[bank - 1];
  815. /* convert from ISA to LM75 I2C addresses */
  816. switch (reg & 0xff) {
  817. case 0x52: /* CONFIG */
  818. i2c_smbus_write_byte_data(cl, 1, value & 0xff);
  819. break;
  820. case 0x53: /* HYST */
  821. i2c_smbus_write_word_data(cl, 2, swab16(value));
  822. break;
  823. case 0x55: /* MAX */
  824. i2c_smbus_write_word_data(cl, 3, swab16(value));
  825. break;
  826. }
  827. }
  828. if (bank > 2)
  829. i2c_smbus_write_byte_data(client, ASB100_REG_BANK, 0);
  830. mutex_unlock(&data->lock);
  831. }
  832. static void asb100_init_client(struct i2c_client *client)
  833. {
  834. struct asb100_data *data = i2c_get_clientdata(client);
  835. int vid = 0;
  836. vid = asb100_read_value(client, ASB100_REG_VID_FANDIV) & 0x0f;
  837. vid |= (asb100_read_value(client, ASB100_REG_CHIPID) & 0x01) << 4;
  838. data->vrm = vid_which_vrm();
  839. vid = vid_from_reg(vid, data->vrm);
  840. /* Start monitoring */
  841. asb100_write_value(client, ASB100_REG_CONFIG,
  842. (asb100_read_value(client, ASB100_REG_CONFIG) & 0xf7) | 0x01);
  843. }
  844. static struct asb100_data *asb100_update_device(struct device *dev)
  845. {
  846. struct i2c_client *client = to_i2c_client(dev);
  847. struct asb100_data *data = i2c_get_clientdata(client);
  848. int i;
  849. mutex_lock(&data->update_lock);
  850. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  851. || !data->valid) {
  852. dev_dbg(&client->dev, "starting device update...\n");
  853. /* 7 voltage inputs */
  854. for (i = 0; i < 7; i++) {
  855. data->in[i] = asb100_read_value(client,
  856. ASB100_REG_IN(i));
  857. data->in_min[i] = asb100_read_value(client,
  858. ASB100_REG_IN_MIN(i));
  859. data->in_max[i] = asb100_read_value(client,
  860. ASB100_REG_IN_MAX(i));
  861. }
  862. /* 3 fan inputs */
  863. for (i = 0; i < 3; i++) {
  864. data->fan[i] = asb100_read_value(client,
  865. ASB100_REG_FAN(i));
  866. data->fan_min[i] = asb100_read_value(client,
  867. ASB100_REG_FAN_MIN(i));
  868. }
  869. /* 4 temperature inputs */
  870. for (i = 1; i <= 4; i++) {
  871. data->temp[i-1] = asb100_read_value(client,
  872. ASB100_REG_TEMP(i));
  873. data->temp_max[i-1] = asb100_read_value(client,
  874. ASB100_REG_TEMP_MAX(i));
  875. data->temp_hyst[i-1] = asb100_read_value(client,
  876. ASB100_REG_TEMP_HYST(i));
  877. }
  878. /* VID and fan divisors */
  879. i = asb100_read_value(client, ASB100_REG_VID_FANDIV);
  880. data->vid = i & 0x0f;
  881. data->vid |= (asb100_read_value(client,
  882. ASB100_REG_CHIPID) & 0x01) << 4;
  883. data->fan_div[0] = (i >> 4) & 0x03;
  884. data->fan_div[1] = (i >> 6) & 0x03;
  885. data->fan_div[2] = (asb100_read_value(client,
  886. ASB100_REG_PIN) >> 6) & 0x03;
  887. /* PWM */
  888. data->pwm = asb100_read_value(client, ASB100_REG_PWM1);
  889. /* alarms */
  890. data->alarms = asb100_read_value(client, ASB100_REG_ALARM1) +
  891. (asb100_read_value(client, ASB100_REG_ALARM2) << 8);
  892. data->last_updated = jiffies;
  893. data->valid = 1;
  894. dev_dbg(&client->dev, "... device update complete\n");
  895. }
  896. mutex_unlock(&data->update_lock);
  897. return data;
  898. }
  899. static int __init asb100_init(void)
  900. {
  901. return i2c_add_driver(&asb100_driver);
  902. }
  903. static void __exit asb100_exit(void)
  904. {
  905. i2c_del_driver(&asb100_driver);
  906. }
  907. MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
  908. MODULE_DESCRIPTION("ASB100 Bach driver");
  909. MODULE_LICENSE("GPL");
  910. module_init(asb100_init);
  911. module_exit(asb100_exit);