asb100.c 30 KB

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