asb100.c 30 KB

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