asb100.c 28 KB

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