asb100.c 28 KB

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