asb100.c 29 KB

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