asb100.c 28 KB

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