asb100.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  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. .driver = {
  179. .name = "asb100",
  180. },
  181. .id = I2C_DRIVERID_ASB100,
  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] = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
  541. if (!(data->lm75[0])) {
  542. err = -ENOMEM;
  543. goto ERROR_SC_0;
  544. }
  545. data->lm75[1] = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
  546. if (!(data->lm75[1])) {
  547. err = -ENOMEM;
  548. goto ERROR_SC_1;
  549. }
  550. id = i2c_adapter_id(adapter);
  551. if (force_subclients[0] == id && force_subclients[1] == address) {
  552. for (i = 2; i <= 3; i++) {
  553. if (force_subclients[i] < 0x48 ||
  554. force_subclients[i] > 0x4f) {
  555. dev_err(&new_client->dev, "invalid subclient "
  556. "address %d; must be 0x48-0x4f\n",
  557. force_subclients[i]);
  558. err = -ENODEV;
  559. goto ERROR_SC_2;
  560. }
  561. }
  562. asb100_write_value(new_client, ASB100_REG_I2C_SUBADDR,
  563. (force_subclients[2] & 0x07) |
  564. ((force_subclients[3] & 0x07) <<4));
  565. data->lm75[0]->addr = force_subclients[2];
  566. data->lm75[1]->addr = force_subclients[3];
  567. } else {
  568. int val = asb100_read_value(new_client, ASB100_REG_I2C_SUBADDR);
  569. data->lm75[0]->addr = 0x48 + (val & 0x07);
  570. data->lm75[1]->addr = 0x48 + ((val >> 4) & 0x07);
  571. }
  572. if(data->lm75[0]->addr == data->lm75[1]->addr) {
  573. dev_err(&new_client->dev, "duplicate addresses 0x%x "
  574. "for subclients\n", data->lm75[0]->addr);
  575. err = -ENODEV;
  576. goto ERROR_SC_2;
  577. }
  578. for (i = 0; i <= 1; i++) {
  579. i2c_set_clientdata(data->lm75[i], NULL);
  580. data->lm75[i]->adapter = adapter;
  581. data->lm75[i]->driver = &asb100_driver;
  582. data->lm75[i]->flags = 0;
  583. strlcpy(data->lm75[i]->name, "asb100 subclient", I2C_NAME_SIZE);
  584. }
  585. if ((err = i2c_attach_client(data->lm75[0]))) {
  586. dev_err(&new_client->dev, "subclient %d registration "
  587. "at address 0x%x failed.\n", i, data->lm75[0]->addr);
  588. goto ERROR_SC_2;
  589. }
  590. if ((err = i2c_attach_client(data->lm75[1]))) {
  591. dev_err(&new_client->dev, "subclient %d registration "
  592. "at address 0x%x failed.\n", i, data->lm75[1]->addr);
  593. goto ERROR_SC_3;
  594. }
  595. return 0;
  596. /* Undo inits in case of errors */
  597. ERROR_SC_3:
  598. i2c_detach_client(data->lm75[0]);
  599. ERROR_SC_2:
  600. kfree(data->lm75[1]);
  601. ERROR_SC_1:
  602. kfree(data->lm75[0]);
  603. ERROR_SC_0:
  604. return err;
  605. }
  606. static int asb100_detect(struct i2c_adapter *adapter, int address, int kind)
  607. {
  608. int err;
  609. struct i2c_client *new_client;
  610. struct asb100_data *data;
  611. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
  612. pr_debug("asb100.o: detect failed, "
  613. "smbus byte data not supported!\n");
  614. err = -ENODEV;
  615. goto ERROR0;
  616. }
  617. /* OK. For now, we presume we have a valid client. We now create the
  618. client structure, even though we cannot fill it completely yet.
  619. But it allows us to access asb100_{read,write}_value. */
  620. if (!(data = kzalloc(sizeof(struct asb100_data), GFP_KERNEL))) {
  621. pr_debug("asb100.o: detect failed, kzalloc failed!\n");
  622. err = -ENOMEM;
  623. goto ERROR0;
  624. }
  625. new_client = &data->client;
  626. init_MUTEX(&data->lock);
  627. i2c_set_clientdata(new_client, data);
  628. new_client->addr = address;
  629. new_client->adapter = adapter;
  630. new_client->driver = &asb100_driver;
  631. new_client->flags = 0;
  632. /* Now, we do the remaining detection. */
  633. /* The chip may be stuck in some other bank than bank 0. This may
  634. make reading other information impossible. Specify a force=... or
  635. force_*=... parameter, and the chip will be reset to the right
  636. bank. */
  637. if (kind < 0) {
  638. int val1 = asb100_read_value(new_client, ASB100_REG_BANK);
  639. int val2 = asb100_read_value(new_client, ASB100_REG_CHIPMAN);
  640. /* If we're in bank 0 */
  641. if ( (!(val1 & 0x07)) &&
  642. /* Check for ASB100 ID (low byte) */
  643. ( ((!(val1 & 0x80)) && (val2 != 0x94)) ||
  644. /* Check for ASB100 ID (high byte ) */
  645. ((val1 & 0x80) && (val2 != 0x06)) ) ) {
  646. pr_debug("asb100.o: detect failed, "
  647. "bad chip id 0x%02x!\n", val2);
  648. err = -ENODEV;
  649. goto ERROR1;
  650. }
  651. } /* kind < 0 */
  652. /* We have either had a force parameter, or we have already detected
  653. Winbond. Put it now into bank 0 and Vendor ID High Byte */
  654. asb100_write_value(new_client, ASB100_REG_BANK,
  655. (asb100_read_value(new_client, ASB100_REG_BANK) & 0x78) | 0x80);
  656. /* Determine the chip type. */
  657. if (kind <= 0) {
  658. int val1 = asb100_read_value(new_client, ASB100_REG_WCHIPID);
  659. int val2 = asb100_read_value(new_client, ASB100_REG_CHIPMAN);
  660. if ((val1 == 0x31) && (val2 == 0x06))
  661. kind = asb100;
  662. else {
  663. if (kind == 0)
  664. dev_warn(&new_client->dev, "ignoring "
  665. "'force' parameter for unknown chip "
  666. "at adapter %d, address 0x%02x.\n",
  667. i2c_adapter_id(adapter), address);
  668. err = -ENODEV;
  669. goto ERROR1;
  670. }
  671. }
  672. /* Fill in remaining client fields and put it into the global list */
  673. strlcpy(new_client->name, "asb100", I2C_NAME_SIZE);
  674. data->type = kind;
  675. data->valid = 0;
  676. init_MUTEX(&data->update_lock);
  677. /* Tell the I2C layer a new client has arrived */
  678. if ((err = i2c_attach_client(new_client)))
  679. goto ERROR1;
  680. /* Attach secondary lm75 clients */
  681. if ((err = asb100_detect_subclients(adapter, address, kind,
  682. new_client)))
  683. goto ERROR2;
  684. /* Initialize the chip */
  685. asb100_init_client(new_client);
  686. /* A few vars need to be filled upon startup */
  687. data->fan_min[0] = asb100_read_value(new_client, ASB100_REG_FAN_MIN(0));
  688. data->fan_min[1] = asb100_read_value(new_client, ASB100_REG_FAN_MIN(1));
  689. data->fan_min[2] = asb100_read_value(new_client, ASB100_REG_FAN_MIN(2));
  690. /* Register sysfs hooks */
  691. data->class_dev = hwmon_device_register(&new_client->dev);
  692. if (IS_ERR(data->class_dev)) {
  693. err = PTR_ERR(data->class_dev);
  694. goto ERROR3;
  695. }
  696. device_create_file_in(new_client, 0);
  697. device_create_file_in(new_client, 1);
  698. device_create_file_in(new_client, 2);
  699. device_create_file_in(new_client, 3);
  700. device_create_file_in(new_client, 4);
  701. device_create_file_in(new_client, 5);
  702. device_create_file_in(new_client, 6);
  703. device_create_file_fan(new_client, 1);
  704. device_create_file_fan(new_client, 2);
  705. device_create_file_fan(new_client, 3);
  706. device_create_file_temp(new_client, 1);
  707. device_create_file_temp(new_client, 2);
  708. device_create_file_temp(new_client, 3);
  709. device_create_file_temp(new_client, 4);
  710. device_create_file_vid(new_client);
  711. device_create_file_vrm(new_client);
  712. device_create_file_alarms(new_client);
  713. device_create_file_pwm1(new_client);
  714. return 0;
  715. ERROR3:
  716. i2c_detach_client(data->lm75[1]);
  717. i2c_detach_client(data->lm75[0]);
  718. kfree(data->lm75[1]);
  719. kfree(data->lm75[0]);
  720. ERROR2:
  721. i2c_detach_client(new_client);
  722. ERROR1:
  723. kfree(data);
  724. ERROR0:
  725. return err;
  726. }
  727. static int asb100_detach_client(struct i2c_client *client)
  728. {
  729. struct asb100_data *data = i2c_get_clientdata(client);
  730. int err;
  731. /* main client */
  732. if (data)
  733. hwmon_device_unregister(data->class_dev);
  734. if ((err = i2c_detach_client(client)))
  735. return err;
  736. /* main client */
  737. if (data)
  738. kfree(data);
  739. /* subclient */
  740. else
  741. kfree(client);
  742. return 0;
  743. }
  744. /* The SMBus locks itself, usually, but nothing may access the chip between
  745. bank switches. */
  746. static int asb100_read_value(struct i2c_client *client, u16 reg)
  747. {
  748. struct asb100_data *data = i2c_get_clientdata(client);
  749. struct i2c_client *cl;
  750. int res, bank;
  751. down(&data->lock);
  752. bank = (reg >> 8) & 0x0f;
  753. if (bank > 2)
  754. /* switch banks */
  755. i2c_smbus_write_byte_data(client, ASB100_REG_BANK, bank);
  756. if (bank == 0 || bank > 2) {
  757. res = i2c_smbus_read_byte_data(client, reg & 0xff);
  758. } else {
  759. /* switch to subclient */
  760. cl = data->lm75[bank - 1];
  761. /* convert from ISA to LM75 I2C addresses */
  762. switch (reg & 0xff) {
  763. case 0x50: /* TEMP */
  764. res = swab16(i2c_smbus_read_word_data (cl, 0));
  765. break;
  766. case 0x52: /* CONFIG */
  767. res = i2c_smbus_read_byte_data(cl, 1);
  768. break;
  769. case 0x53: /* HYST */
  770. res = swab16(i2c_smbus_read_word_data (cl, 2));
  771. break;
  772. case 0x55: /* MAX */
  773. default:
  774. res = swab16(i2c_smbus_read_word_data (cl, 3));
  775. break;
  776. }
  777. }
  778. if (bank > 2)
  779. i2c_smbus_write_byte_data(client, ASB100_REG_BANK, 0);
  780. up(&data->lock);
  781. return res;
  782. }
  783. static void asb100_write_value(struct i2c_client *client, u16 reg, u16 value)
  784. {
  785. struct asb100_data *data = i2c_get_clientdata(client);
  786. struct i2c_client *cl;
  787. int bank;
  788. down(&data->lock);
  789. bank = (reg >> 8) & 0x0f;
  790. if (bank > 2)
  791. /* switch banks */
  792. i2c_smbus_write_byte_data(client, ASB100_REG_BANK, bank);
  793. if (bank == 0 || bank > 2) {
  794. i2c_smbus_write_byte_data(client, reg & 0xff, value & 0xff);
  795. } else {
  796. /* switch to subclient */
  797. cl = data->lm75[bank - 1];
  798. /* convert from ISA to LM75 I2C addresses */
  799. switch (reg & 0xff) {
  800. case 0x52: /* CONFIG */
  801. i2c_smbus_write_byte_data(cl, 1, value & 0xff);
  802. break;
  803. case 0x53: /* HYST */
  804. i2c_smbus_write_word_data(cl, 2, swab16(value));
  805. break;
  806. case 0x55: /* MAX */
  807. i2c_smbus_write_word_data(cl, 3, swab16(value));
  808. break;
  809. }
  810. }
  811. if (bank > 2)
  812. i2c_smbus_write_byte_data(client, ASB100_REG_BANK, 0);
  813. up(&data->lock);
  814. }
  815. static void asb100_init_client(struct i2c_client *client)
  816. {
  817. struct asb100_data *data = i2c_get_clientdata(client);
  818. int vid = 0;
  819. vid = asb100_read_value(client, ASB100_REG_VID_FANDIV) & 0x0f;
  820. vid |= (asb100_read_value(client, ASB100_REG_CHIPID) & 0x01) << 4;
  821. data->vrm = vid_which_vrm();
  822. vid = vid_from_reg(vid, data->vrm);
  823. /* Start monitoring */
  824. asb100_write_value(client, ASB100_REG_CONFIG,
  825. (asb100_read_value(client, ASB100_REG_CONFIG) & 0xf7) | 0x01);
  826. }
  827. static struct asb100_data *asb100_update_device(struct device *dev)
  828. {
  829. struct i2c_client *client = to_i2c_client(dev);
  830. struct asb100_data *data = i2c_get_clientdata(client);
  831. int i;
  832. down(&data->update_lock);
  833. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  834. || !data->valid) {
  835. dev_dbg(&client->dev, "starting device update...\n");
  836. /* 7 voltage inputs */
  837. for (i = 0; i < 7; i++) {
  838. data->in[i] = asb100_read_value(client,
  839. ASB100_REG_IN(i));
  840. data->in_min[i] = asb100_read_value(client,
  841. ASB100_REG_IN_MIN(i));
  842. data->in_max[i] = asb100_read_value(client,
  843. ASB100_REG_IN_MAX(i));
  844. }
  845. /* 3 fan inputs */
  846. for (i = 0; i < 3; i++) {
  847. data->fan[i] = asb100_read_value(client,
  848. ASB100_REG_FAN(i));
  849. data->fan_min[i] = asb100_read_value(client,
  850. ASB100_REG_FAN_MIN(i));
  851. }
  852. /* 4 temperature inputs */
  853. for (i = 1; i <= 4; i++) {
  854. data->temp[i-1] = asb100_read_value(client,
  855. ASB100_REG_TEMP(i));
  856. data->temp_max[i-1] = asb100_read_value(client,
  857. ASB100_REG_TEMP_MAX(i));
  858. data->temp_hyst[i-1] = asb100_read_value(client,
  859. ASB100_REG_TEMP_HYST(i));
  860. }
  861. /* VID and fan divisors */
  862. i = asb100_read_value(client, ASB100_REG_VID_FANDIV);
  863. data->vid = i & 0x0f;
  864. data->vid |= (asb100_read_value(client,
  865. ASB100_REG_CHIPID) & 0x01) << 4;
  866. data->fan_div[0] = (i >> 4) & 0x03;
  867. data->fan_div[1] = (i >> 6) & 0x03;
  868. data->fan_div[2] = (asb100_read_value(client,
  869. ASB100_REG_PIN) >> 6) & 0x03;
  870. /* PWM */
  871. data->pwm = asb100_read_value(client, ASB100_REG_PWM1);
  872. /* alarms */
  873. data->alarms = asb100_read_value(client, ASB100_REG_ALARM1) +
  874. (asb100_read_value(client, ASB100_REG_ALARM2) << 8);
  875. data->last_updated = jiffies;
  876. data->valid = 1;
  877. dev_dbg(&client->dev, "... device update complete\n");
  878. }
  879. up(&data->update_lock);
  880. return data;
  881. }
  882. static int __init asb100_init(void)
  883. {
  884. return i2c_add_driver(&asb100_driver);
  885. }
  886. static void __exit asb100_exit(void)
  887. {
  888. i2c_del_driver(&asb100_driver);
  889. }
  890. MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
  891. MODULE_DESCRIPTION("ASB100 Bach driver");
  892. MODULE_LICENSE("GPL");
  893. module_init(asb100_init);
  894. module_exit(asb100_exit);