asb100.c 30 KB

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