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