asb100.c 30 KB

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