asb100.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070
  1. /*
  2. asb100.c - Part of lm_sensors, Linux kernel modules for hardware
  3. monitoring
  4. Copyright (C) 2004 Mark M. Hoffman <mhoffman@lightlink.com>
  5. (derived from w83781d.c)
  6. Copyright (C) 1998 - 2003 Frodo Looijaard <frodol@dds.nl>,
  7. Philip Edelbrock <phil@netroedge.com>, and
  8. Mark Studebaker <mdsxyz123@yahoo.com>
  9. This program is free software; you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation; either version 2 of the License, or
  12. (at your option) any later version.
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU General Public License for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. /*
  22. This driver supports the hardware sensor chips: Asus ASB100 and
  23. ASB100-A "BACH".
  24. ASB100-A supports pwm1, while plain ASB100 does not. There is no known
  25. way for the driver to tell which one is there.
  26. Chip #vin #fanin #pwm #temp wchipid vendid i2c ISA
  27. asb100 7 3 1 4 0x31 0x0694 yes no
  28. */
  29. #include <linux/module.h>
  30. #include <linux/slab.h>
  31. #include <linux/i2c.h>
  32. #include <linux/i2c-sensor.h>
  33. #include <linux/i2c-vid.h>
  34. #include <linux/hwmon.h>
  35. #include <linux/err.h>
  36. #include <linux/init.h>
  37. #include <linux/jiffies.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. SENSORS_INSMOD_1(asb100);
  48. I2C_CLIENT_MODULE_PARM(force_subclients, "List of subclient addresses: "
  49. "{bus, clientaddr, subclientaddr1, subclientaddr2}");
  50. /* Voltage IN registers 0-6 */
  51. #define ASB100_REG_IN(nr) (0x20 + (nr))
  52. #define ASB100_REG_IN_MAX(nr) (0x2b + (nr * 2))
  53. #define ASB100_REG_IN_MIN(nr) (0x2c + (nr * 2))
  54. /* FAN IN registers 1-3 */
  55. #define ASB100_REG_FAN(nr) (0x28 + (nr))
  56. #define ASB100_REG_FAN_MIN(nr) (0x3b + (nr))
  57. /* TEMPERATURE registers 1-4 */
  58. static const u16 asb100_reg_temp[] = {0, 0x27, 0x150, 0x250, 0x17};
  59. static const u16 asb100_reg_temp_max[] = {0, 0x39, 0x155, 0x255, 0x18};
  60. static const u16 asb100_reg_temp_hyst[] = {0, 0x3a, 0x153, 0x253, 0x19};
  61. #define ASB100_REG_TEMP(nr) (asb100_reg_temp[nr])
  62. #define ASB100_REG_TEMP_MAX(nr) (asb100_reg_temp_max[nr])
  63. #define ASB100_REG_TEMP_HYST(nr) (asb100_reg_temp_hyst[nr])
  64. #define ASB100_REG_TEMP2_CONFIG 0x0152
  65. #define ASB100_REG_TEMP3_CONFIG 0x0252
  66. #define ASB100_REG_CONFIG 0x40
  67. #define ASB100_REG_ALARM1 0x41
  68. #define ASB100_REG_ALARM2 0x42
  69. #define ASB100_REG_SMIM1 0x43
  70. #define ASB100_REG_SMIM2 0x44
  71. #define ASB100_REG_VID_FANDIV 0x47
  72. #define ASB100_REG_I2C_ADDR 0x48
  73. #define ASB100_REG_CHIPID 0x49
  74. #define ASB100_REG_I2C_SUBADDR 0x4a
  75. #define ASB100_REG_PIN 0x4b
  76. #define ASB100_REG_IRQ 0x4c
  77. #define ASB100_REG_BANK 0x4e
  78. #define ASB100_REG_CHIPMAN 0x4f
  79. #define ASB100_REG_WCHIPID 0x58
  80. /* bit 7 -> enable, bits 0-3 -> duty cycle */
  81. #define ASB100_REG_PWM1 0x59
  82. /* CONVERSIONS
  83. Rounding and limit checking is only done on the TO_REG variants. */
  84. /* These constants are a guess, consistent w/ w83781d */
  85. #define ASB100_IN_MIN ( 0)
  86. #define ASB100_IN_MAX (4080)
  87. /* IN: 1/1000 V (0V to 4.08V)
  88. REG: 16mV/bit */
  89. static u8 IN_TO_REG(unsigned val)
  90. {
  91. unsigned nval = SENSORS_LIMIT(val, ASB100_IN_MIN, ASB100_IN_MAX);
  92. return (nval + 8) / 16;
  93. }
  94. static unsigned IN_FROM_REG(u8 reg)
  95. {
  96. return reg * 16;
  97. }
  98. static u8 FAN_TO_REG(long rpm, int div)
  99. {
  100. if (rpm == -1)
  101. return 0;
  102. if (rpm == 0)
  103. return 255;
  104. rpm = SENSORS_LIMIT(rpm, 1, 1000000);
  105. return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
  106. }
  107. static int FAN_FROM_REG(u8 val, int div)
  108. {
  109. return val==0 ? -1 : val==255 ? 0 : 1350000/(val*div);
  110. }
  111. /* These constants are a guess, consistent w/ w83781d */
  112. #define ASB100_TEMP_MIN (-128000)
  113. #define ASB100_TEMP_MAX ( 127000)
  114. /* TEMP: 0.001C/bit (-128C to +127C)
  115. REG: 1C/bit, two's complement */
  116. static u8 TEMP_TO_REG(int temp)
  117. {
  118. int ntemp = SENSORS_LIMIT(temp, ASB100_TEMP_MIN, ASB100_TEMP_MAX);
  119. ntemp += (ntemp<0 ? -500 : 500);
  120. return (u8)(ntemp / 1000);
  121. }
  122. static int TEMP_FROM_REG(u8 reg)
  123. {
  124. return (s8)reg * 1000;
  125. }
  126. /* PWM: 0 - 255 per sensors documentation
  127. REG: (6.25% duty cycle per bit) */
  128. static u8 ASB100_PWM_TO_REG(int pwm)
  129. {
  130. pwm = SENSORS_LIMIT(pwm, 0, 255);
  131. return (u8)(pwm / 16);
  132. }
  133. static int ASB100_PWM_FROM_REG(u8 reg)
  134. {
  135. return reg * 16;
  136. }
  137. #define DIV_FROM_REG(val) (1 << (val))
  138. /* FAN DIV: 1, 2, 4, or 8 (defaults to 2)
  139. REG: 0, 1, 2, or 3 (respectively) (defaults to 1) */
  140. static u8 DIV_TO_REG(long val)
  141. {
  142. return val==8 ? 3 : val==4 ? 2 : val==1 ? 0 : 1;
  143. }
  144. /* For each registered client, we need to keep some data in memory. That
  145. data is pointed to by client->data. The structure itself is
  146. dynamically allocated, at the same time the client itself is allocated. */
  147. struct asb100_data {
  148. struct i2c_client client;
  149. struct class_device *class_dev;
  150. struct semaphore lock;
  151. enum chips type;
  152. struct semaphore 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. .owner = THIS_MODULE,
  180. .name = "asb100",
  181. .id = I2C_DRIVERID_ASB100,
  182. .flags = I2C_DF_NOTIFY,
  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. down(&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. up(&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. down(&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. up(&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. down(&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. up(&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. down(&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. up(&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. down(&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. up(&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. down(&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. up(&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] = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
  542. if (!(data->lm75[0])) {
  543. err = -ENOMEM;
  544. goto ERROR_SC_0;
  545. }
  546. memset(data->lm75[0], 0x00, sizeof(struct i2c_client));
  547. data->lm75[1] = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
  548. if (!(data->lm75[1])) {
  549. err = -ENOMEM;
  550. goto ERROR_SC_1;
  551. }
  552. memset(data->lm75[1], 0x00, sizeof(struct i2c_client));
  553. id = i2c_adapter_id(adapter);
  554. if (force_subclients[0] == id && force_subclients[1] == address) {
  555. for (i = 2; i <= 3; i++) {
  556. if (force_subclients[i] < 0x48 ||
  557. force_subclients[i] > 0x4f) {
  558. dev_err(&new_client->dev, "invalid subclient "
  559. "address %d; must be 0x48-0x4f\n",
  560. force_subclients[i]);
  561. err = -ENODEV;
  562. goto ERROR_SC_2;
  563. }
  564. }
  565. asb100_write_value(new_client, ASB100_REG_I2C_SUBADDR,
  566. (force_subclients[2] & 0x07) |
  567. ((force_subclients[3] & 0x07) <<4));
  568. data->lm75[0]->addr = force_subclients[2];
  569. data->lm75[1]->addr = force_subclients[3];
  570. } else {
  571. int val = asb100_read_value(new_client, ASB100_REG_I2C_SUBADDR);
  572. data->lm75[0]->addr = 0x48 + (val & 0x07);
  573. data->lm75[1]->addr = 0x48 + ((val >> 4) & 0x07);
  574. }
  575. if(data->lm75[0]->addr == data->lm75[1]->addr) {
  576. dev_err(&new_client->dev, "duplicate addresses 0x%x "
  577. "for subclients\n", data->lm75[0]->addr);
  578. err = -ENODEV;
  579. goto ERROR_SC_2;
  580. }
  581. for (i = 0; i <= 1; i++) {
  582. i2c_set_clientdata(data->lm75[i], NULL);
  583. data->lm75[i]->adapter = adapter;
  584. data->lm75[i]->driver = &asb100_driver;
  585. data->lm75[i]->flags = 0;
  586. strlcpy(data->lm75[i]->name, "asb100 subclient", I2C_NAME_SIZE);
  587. }
  588. if ((err = i2c_attach_client(data->lm75[0]))) {
  589. dev_err(&new_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(&new_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 *new_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 = kmalloc(sizeof(struct asb100_data), GFP_KERNEL))) {
  624. pr_debug("asb100.o: detect failed, kmalloc failed!\n");
  625. err = -ENOMEM;
  626. goto ERROR0;
  627. }
  628. memset(data, 0, sizeof(struct asb100_data));
  629. new_client = &data->client;
  630. init_MUTEX(&data->lock);
  631. i2c_set_clientdata(new_client, data);
  632. new_client->addr = address;
  633. new_client->adapter = adapter;
  634. new_client->driver = &asb100_driver;
  635. new_client->flags = 0;
  636. /* Now, we do the remaining detection. */
  637. /* The chip may be stuck in some other bank than bank 0. This may
  638. make reading other information impossible. Specify a force=... or
  639. force_*=... parameter, and the chip will be reset to the right
  640. bank. */
  641. if (kind < 0) {
  642. int val1 = asb100_read_value(new_client, ASB100_REG_BANK);
  643. int val2 = asb100_read_value(new_client, ASB100_REG_CHIPMAN);
  644. /* If we're in bank 0 */
  645. if ( (!(val1 & 0x07)) &&
  646. /* Check for ASB100 ID (low byte) */
  647. ( ((!(val1 & 0x80)) && (val2 != 0x94)) ||
  648. /* Check for ASB100 ID (high byte ) */
  649. ((val1 & 0x80) && (val2 != 0x06)) ) ) {
  650. pr_debug("asb100.o: detect failed, "
  651. "bad chip id 0x%02x!\n", val2);
  652. err = -ENODEV;
  653. goto ERROR1;
  654. }
  655. } /* kind < 0 */
  656. /* We have either had a force parameter, or we have already detected
  657. Winbond. Put it now into bank 0 and Vendor ID High Byte */
  658. asb100_write_value(new_client, ASB100_REG_BANK,
  659. (asb100_read_value(new_client, ASB100_REG_BANK) & 0x78) | 0x80);
  660. /* Determine the chip type. */
  661. if (kind <= 0) {
  662. int val1 = asb100_read_value(new_client, ASB100_REG_WCHIPID);
  663. int val2 = asb100_read_value(new_client, ASB100_REG_CHIPMAN);
  664. if ((val1 == 0x31) && (val2 == 0x06))
  665. kind = asb100;
  666. else {
  667. if (kind == 0)
  668. dev_warn(&new_client->dev, "ignoring "
  669. "'force' parameter for unknown chip "
  670. "at adapter %d, address 0x%02x.\n",
  671. i2c_adapter_id(adapter), address);
  672. err = -ENODEV;
  673. goto ERROR1;
  674. }
  675. }
  676. /* Fill in remaining client fields and put it into the global list */
  677. strlcpy(new_client->name, "asb100", I2C_NAME_SIZE);
  678. data->type = kind;
  679. data->valid = 0;
  680. init_MUTEX(&data->update_lock);
  681. /* Tell the I2C layer a new client has arrived */
  682. if ((err = i2c_attach_client(new_client)))
  683. goto ERROR1;
  684. /* Attach secondary lm75 clients */
  685. if ((err = asb100_detect_subclients(adapter, address, kind,
  686. new_client)))
  687. goto ERROR2;
  688. /* Initialize the chip */
  689. asb100_init_client(new_client);
  690. /* A few vars need to be filled upon startup */
  691. data->fan_min[0] = asb100_read_value(new_client, ASB100_REG_FAN_MIN(0));
  692. data->fan_min[1] = asb100_read_value(new_client, ASB100_REG_FAN_MIN(1));
  693. data->fan_min[2] = asb100_read_value(new_client, ASB100_REG_FAN_MIN(2));
  694. /* Register sysfs hooks */
  695. data->class_dev = hwmon_device_register(&new_client->dev);
  696. if (IS_ERR(data->class_dev)) {
  697. err = PTR_ERR(data->class_dev);
  698. goto ERROR3;
  699. }
  700. device_create_file_in(new_client, 0);
  701. device_create_file_in(new_client, 1);
  702. device_create_file_in(new_client, 2);
  703. device_create_file_in(new_client, 3);
  704. device_create_file_in(new_client, 4);
  705. device_create_file_in(new_client, 5);
  706. device_create_file_in(new_client, 6);
  707. device_create_file_fan(new_client, 1);
  708. device_create_file_fan(new_client, 2);
  709. device_create_file_fan(new_client, 3);
  710. device_create_file_temp(new_client, 1);
  711. device_create_file_temp(new_client, 2);
  712. device_create_file_temp(new_client, 3);
  713. device_create_file_temp(new_client, 4);
  714. device_create_file_vid(new_client);
  715. device_create_file_vrm(new_client);
  716. device_create_file_alarms(new_client);
  717. device_create_file_pwm1(new_client);
  718. return 0;
  719. ERROR3:
  720. i2c_detach_client(data->lm75[1]);
  721. i2c_detach_client(data->lm75[0]);
  722. kfree(data->lm75[1]);
  723. kfree(data->lm75[0]);
  724. ERROR2:
  725. i2c_detach_client(new_client);
  726. ERROR1:
  727. kfree(data);
  728. ERROR0:
  729. return err;
  730. }
  731. static int asb100_detach_client(struct i2c_client *client)
  732. {
  733. struct asb100_data *data = i2c_get_clientdata(client);
  734. int err;
  735. /* main client */
  736. if (data)
  737. hwmon_device_unregister(data->class_dev);
  738. if ((err = i2c_detach_client(client)))
  739. return err;
  740. /* main client */
  741. if (data)
  742. kfree(data);
  743. /* subclient */
  744. else
  745. kfree(client);
  746. return 0;
  747. }
  748. /* The SMBus locks itself, usually, but nothing may access the chip between
  749. bank switches. */
  750. static int asb100_read_value(struct i2c_client *client, u16 reg)
  751. {
  752. struct asb100_data *data = i2c_get_clientdata(client);
  753. struct i2c_client *cl;
  754. int res, bank;
  755. down(&data->lock);
  756. bank = (reg >> 8) & 0x0f;
  757. if (bank > 2)
  758. /* switch banks */
  759. i2c_smbus_write_byte_data(client, ASB100_REG_BANK, bank);
  760. if (bank == 0 || bank > 2) {
  761. res = i2c_smbus_read_byte_data(client, reg & 0xff);
  762. } else {
  763. /* switch to subclient */
  764. cl = data->lm75[bank - 1];
  765. /* convert from ISA to LM75 I2C addresses */
  766. switch (reg & 0xff) {
  767. case 0x50: /* TEMP */
  768. res = swab16(i2c_smbus_read_word_data (cl, 0));
  769. break;
  770. case 0x52: /* CONFIG */
  771. res = i2c_smbus_read_byte_data(cl, 1);
  772. break;
  773. case 0x53: /* HYST */
  774. res = swab16(i2c_smbus_read_word_data (cl, 2));
  775. break;
  776. case 0x55: /* MAX */
  777. default:
  778. res = swab16(i2c_smbus_read_word_data (cl, 3));
  779. break;
  780. }
  781. }
  782. if (bank > 2)
  783. i2c_smbus_write_byte_data(client, ASB100_REG_BANK, 0);
  784. up(&data->lock);
  785. return res;
  786. }
  787. static void asb100_write_value(struct i2c_client *client, u16 reg, u16 value)
  788. {
  789. struct asb100_data *data = i2c_get_clientdata(client);
  790. struct i2c_client *cl;
  791. int bank;
  792. down(&data->lock);
  793. bank = (reg >> 8) & 0x0f;
  794. if (bank > 2)
  795. /* switch banks */
  796. i2c_smbus_write_byte_data(client, ASB100_REG_BANK, bank);
  797. if (bank == 0 || bank > 2) {
  798. i2c_smbus_write_byte_data(client, reg & 0xff, value & 0xff);
  799. } else {
  800. /* switch to subclient */
  801. cl = data->lm75[bank - 1];
  802. /* convert from ISA to LM75 I2C addresses */
  803. switch (reg & 0xff) {
  804. case 0x52: /* CONFIG */
  805. i2c_smbus_write_byte_data(cl, 1, value & 0xff);
  806. break;
  807. case 0x53: /* HYST */
  808. i2c_smbus_write_word_data(cl, 2, swab16(value));
  809. break;
  810. case 0x55: /* MAX */
  811. i2c_smbus_write_word_data(cl, 3, swab16(value));
  812. break;
  813. }
  814. }
  815. if (bank > 2)
  816. i2c_smbus_write_byte_data(client, ASB100_REG_BANK, 0);
  817. up(&data->lock);
  818. }
  819. static void asb100_init_client(struct i2c_client *client)
  820. {
  821. struct asb100_data *data = i2c_get_clientdata(client);
  822. int vid = 0;
  823. vid = asb100_read_value(client, ASB100_REG_VID_FANDIV) & 0x0f;
  824. vid |= (asb100_read_value(client, ASB100_REG_CHIPID) & 0x01) << 4;
  825. data->vrm = i2c_which_vrm();
  826. vid = vid_from_reg(vid, data->vrm);
  827. /* Start monitoring */
  828. asb100_write_value(client, ASB100_REG_CONFIG,
  829. (asb100_read_value(client, ASB100_REG_CONFIG) & 0xf7) | 0x01);
  830. }
  831. static struct asb100_data *asb100_update_device(struct device *dev)
  832. {
  833. struct i2c_client *client = to_i2c_client(dev);
  834. struct asb100_data *data = i2c_get_clientdata(client);
  835. int i;
  836. down(&data->update_lock);
  837. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  838. || !data->valid) {
  839. dev_dbg(&client->dev, "starting device update...\n");
  840. /* 7 voltage inputs */
  841. for (i = 0; i < 7; i++) {
  842. data->in[i] = asb100_read_value(client,
  843. ASB100_REG_IN(i));
  844. data->in_min[i] = asb100_read_value(client,
  845. ASB100_REG_IN_MIN(i));
  846. data->in_max[i] = asb100_read_value(client,
  847. ASB100_REG_IN_MAX(i));
  848. }
  849. /* 3 fan inputs */
  850. for (i = 0; i < 3; i++) {
  851. data->fan[i] = asb100_read_value(client,
  852. ASB100_REG_FAN(i));
  853. data->fan_min[i] = asb100_read_value(client,
  854. ASB100_REG_FAN_MIN(i));
  855. }
  856. /* 4 temperature inputs */
  857. for (i = 1; i <= 4; i++) {
  858. data->temp[i-1] = asb100_read_value(client,
  859. ASB100_REG_TEMP(i));
  860. data->temp_max[i-1] = asb100_read_value(client,
  861. ASB100_REG_TEMP_MAX(i));
  862. data->temp_hyst[i-1] = asb100_read_value(client,
  863. ASB100_REG_TEMP_HYST(i));
  864. }
  865. /* VID and fan divisors */
  866. i = asb100_read_value(client, ASB100_REG_VID_FANDIV);
  867. data->vid = i & 0x0f;
  868. data->vid |= (asb100_read_value(client,
  869. ASB100_REG_CHIPID) & 0x01) << 4;
  870. data->fan_div[0] = (i >> 4) & 0x03;
  871. data->fan_div[1] = (i >> 6) & 0x03;
  872. data->fan_div[2] = (asb100_read_value(client,
  873. ASB100_REG_PIN) >> 6) & 0x03;
  874. /* PWM */
  875. data->pwm = asb100_read_value(client, ASB100_REG_PWM1);
  876. /* alarms */
  877. data->alarms = asb100_read_value(client, ASB100_REG_ALARM1) +
  878. (asb100_read_value(client, ASB100_REG_ALARM2) << 8);
  879. data->last_updated = jiffies;
  880. data->valid = 1;
  881. dev_dbg(&client->dev, "... device update complete\n");
  882. }
  883. up(&data->update_lock);
  884. return data;
  885. }
  886. static int __init asb100_init(void)
  887. {
  888. return i2c_add_driver(&asb100_driver);
  889. }
  890. static void __exit asb100_exit(void)
  891. {
  892. i2c_del_driver(&asb100_driver);
  893. }
  894. MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
  895. MODULE_DESCRIPTION("ASB100 Bach driver");
  896. MODULE_LICENSE("GPL");
  897. module_init(asb100_init);
  898. module_exit(asb100_exit);