w83781d.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832
  1. /*
  2. w83781d.c - Part of lm_sensors, Linux kernel modules for hardware
  3. monitoring
  4. Copyright (c) 1998 - 2001 Frodo Looijaard <frodol@dds.nl>,
  5. Philip Edelbrock <phil@netroedge.com>,
  6. and Mark Studebaker <mdsxyz123@yahoo.com>
  7. Copyright (c) 2007 Jean Delvare <khali@linux-fr.org>
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. /*
  21. Supports following chips:
  22. Chip #vin #fanin #pwm #temp wchipid vendid i2c ISA
  23. as99127f 7 3 0 3 0x31 0x12c3 yes no
  24. as99127f rev.2 (type_name = as99127f) 0x31 0x5ca3 yes no
  25. w83781d 7 3 0 3 0x10-1 0x5ca3 yes yes
  26. w83627hf 9 3 2 3 0x21 0x5ca3 yes yes(LPC)
  27. w83782d 9 3 2-4 3 0x30 0x5ca3 yes yes
  28. w83783s 5-6 3 2 1-2 0x40 0x5ca3 yes no
  29. */
  30. #include <linux/module.h>
  31. #include <linux/init.h>
  32. #include <linux/slab.h>
  33. #include <linux/jiffies.h>
  34. #include <linux/i2c.h>
  35. #include <linux/platform_device.h>
  36. #include <linux/ioport.h>
  37. #include <linux/hwmon.h>
  38. #include <linux/hwmon-vid.h>
  39. #include <linux/hwmon-sysfs.h>
  40. #include <linux/sysfs.h>
  41. #include <linux/err.h>
  42. #include <linux/mutex.h>
  43. #include <asm/io.h>
  44. #include "lm75.h"
  45. /* ISA device, if found */
  46. static struct platform_device *pdev;
  47. /* Addresses to scan */
  48. static unsigned short normal_i2c[] = { 0x20, 0x21, 0x22, 0x23, 0x24, 0x25,
  49. 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
  50. 0x2c, 0x2d, 0x2e, 0x2f, I2C_CLIENT_END };
  51. static unsigned short isa_address = 0x290;
  52. /* Insmod parameters */
  53. I2C_CLIENT_INSMOD_5(w83781d, w83782d, w83783s, w83627hf, as99127f);
  54. I2C_CLIENT_MODULE_PARM(force_subclients, "List of subclient addresses: "
  55. "{bus, clientaddr, subclientaddr1, subclientaddr2}");
  56. static int reset;
  57. module_param(reset, bool, 0);
  58. MODULE_PARM_DESC(reset, "Set to one to reset chip on load");
  59. static int init = 1;
  60. module_param(init, bool, 0);
  61. MODULE_PARM_DESC(init, "Set to zero to bypass chip initialization");
  62. /* Constants specified below */
  63. /* Length of ISA address segment */
  64. #define W83781D_EXTENT 8
  65. /* Where are the ISA address/data registers relative to the base address */
  66. #define W83781D_ADDR_REG_OFFSET 5
  67. #define W83781D_DATA_REG_OFFSET 6
  68. /* The device registers */
  69. /* in nr from 0 to 8 */
  70. #define W83781D_REG_IN_MAX(nr) ((nr < 7) ? (0x2b + (nr) * 2) : \
  71. (0x554 + (((nr) - 7) * 2)))
  72. #define W83781D_REG_IN_MIN(nr) ((nr < 7) ? (0x2c + (nr) * 2) : \
  73. (0x555 + (((nr) - 7) * 2)))
  74. #define W83781D_REG_IN(nr) ((nr < 7) ? (0x20 + (nr)) : \
  75. (0x550 + (nr) - 7))
  76. /* fan nr from 0 to 2 */
  77. #define W83781D_REG_FAN_MIN(nr) (0x3b + (nr))
  78. #define W83781D_REG_FAN(nr) (0x28 + (nr))
  79. #define W83781D_REG_BANK 0x4E
  80. #define W83781D_REG_TEMP2_CONFIG 0x152
  81. #define W83781D_REG_TEMP3_CONFIG 0x252
  82. /* temp nr from 1 to 3 */
  83. #define W83781D_REG_TEMP(nr) ((nr == 3) ? (0x0250) : \
  84. ((nr == 2) ? (0x0150) : \
  85. (0x27)))
  86. #define W83781D_REG_TEMP_HYST(nr) ((nr == 3) ? (0x253) : \
  87. ((nr == 2) ? (0x153) : \
  88. (0x3A)))
  89. #define W83781D_REG_TEMP_OVER(nr) ((nr == 3) ? (0x255) : \
  90. ((nr == 2) ? (0x155) : \
  91. (0x39)))
  92. #define W83781D_REG_CONFIG 0x40
  93. /* Interrupt status (W83781D, AS99127F) */
  94. #define W83781D_REG_ALARM1 0x41
  95. #define W83781D_REG_ALARM2 0x42
  96. /* Real-time status (W83782D, W83783S, W83627HF) */
  97. #define W83782D_REG_ALARM1 0x459
  98. #define W83782D_REG_ALARM2 0x45A
  99. #define W83782D_REG_ALARM3 0x45B
  100. #define W83781D_REG_BEEP_CONFIG 0x4D
  101. #define W83781D_REG_BEEP_INTS1 0x56
  102. #define W83781D_REG_BEEP_INTS2 0x57
  103. #define W83781D_REG_BEEP_INTS3 0x453 /* not on W83781D */
  104. #define W83781D_REG_VID_FANDIV 0x47
  105. #define W83781D_REG_CHIPID 0x49
  106. #define W83781D_REG_WCHIPID 0x58
  107. #define W83781D_REG_CHIPMAN 0x4F
  108. #define W83781D_REG_PIN 0x4B
  109. /* 782D/783S only */
  110. #define W83781D_REG_VBAT 0x5D
  111. /* PWM 782D (1-4) and 783S (1-2) only */
  112. static const u8 W83781D_REG_PWM[] = { 0x5B, 0x5A, 0x5E, 0x5F };
  113. #define W83781D_REG_PWMCLK12 0x5C
  114. #define W83781D_REG_PWMCLK34 0x45C
  115. #define W83781D_REG_I2C_ADDR 0x48
  116. #define W83781D_REG_I2C_SUBADDR 0x4A
  117. /* The following are undocumented in the data sheets however we
  118. received the information in an email from Winbond tech support */
  119. /* Sensor selection - not on 781d */
  120. #define W83781D_REG_SCFG1 0x5D
  121. static const u8 BIT_SCFG1[] = { 0x02, 0x04, 0x08 };
  122. #define W83781D_REG_SCFG2 0x59
  123. static const u8 BIT_SCFG2[] = { 0x10, 0x20, 0x40 };
  124. #define W83781D_DEFAULT_BETA 3435
  125. /* RT Table registers */
  126. #define W83781D_REG_RT_IDX 0x50
  127. #define W83781D_REG_RT_VAL 0x51
  128. /* Conversions */
  129. #define IN_TO_REG(val) SENSORS_LIMIT(((val) + 8) / 16, 0, 255)
  130. #define IN_FROM_REG(val) ((val) * 16)
  131. static inline u8
  132. FAN_TO_REG(long rpm, int div)
  133. {
  134. if (rpm == 0)
  135. return 255;
  136. rpm = SENSORS_LIMIT(rpm, 1, 1000000);
  137. return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
  138. }
  139. static inline long
  140. FAN_FROM_REG(u8 val, int div)
  141. {
  142. if (val == 0)
  143. return -1;
  144. if (val == 255)
  145. return 0;
  146. return 1350000 / (val * div);
  147. }
  148. #define TEMP_TO_REG(val) SENSORS_LIMIT((val) / 1000, -127, 128)
  149. #define TEMP_FROM_REG(val) ((val) * 1000)
  150. #define BEEP_MASK_FROM_REG(val,type) ((type) == as99127f ? \
  151. (val) ^ 0x7fff : (val))
  152. #define BEEP_MASK_TO_REG(val,type) ((type) == as99127f ? \
  153. (~(val)) & 0x7fff : (val) & 0xffffff)
  154. #define DIV_FROM_REG(val) (1 << (val))
  155. static inline u8
  156. DIV_TO_REG(long val, enum chips type)
  157. {
  158. int i;
  159. val = SENSORS_LIMIT(val, 1,
  160. ((type == w83781d
  161. || type == as99127f) ? 8 : 128)) >> 1;
  162. for (i = 0; i < 7; i++) {
  163. if (val == 0)
  164. break;
  165. val >>= 1;
  166. }
  167. return i;
  168. }
  169. /* There are some complications in a module like this. First off, W83781D chips
  170. may be both present on the SMBus and the ISA bus, and we have to handle
  171. those cases separately at some places. Second, there might be several
  172. W83781D chips available (well, actually, that is probably never done; but
  173. it is a clean illustration of how to handle a case like that). Finally,
  174. a specific chip may be attached to *both* ISA and SMBus, and we would
  175. not like to detect it double. Fortunately, in the case of the W83781D at
  176. least, a register tells us what SMBus address we are on, so that helps
  177. a bit - except if there could be more than one SMBus. Groan. No solution
  178. for this yet. */
  179. /* For ISA chips, we abuse the i2c_client addr and name fields. We also use
  180. the driver field to differentiate between I2C and ISA chips. */
  181. struct w83781d_data {
  182. struct i2c_client client;
  183. struct class_device *class_dev;
  184. struct mutex lock;
  185. enum chips type;
  186. struct mutex update_lock;
  187. char valid; /* !=0 if following fields are valid */
  188. unsigned long last_updated; /* In jiffies */
  189. struct i2c_client *lm75[2]; /* for secondary I2C addresses */
  190. /* array of 2 pointers to subclients */
  191. u8 in[9]; /* Register value - 8 & 9 for 782D only */
  192. u8 in_max[9]; /* Register value - 8 & 9 for 782D only */
  193. u8 in_min[9]; /* Register value - 8 & 9 for 782D only */
  194. u8 fan[3]; /* Register value */
  195. u8 fan_min[3]; /* Register value */
  196. s8 temp; /* Register value */
  197. s8 temp_max; /* Register value */
  198. s8 temp_max_hyst; /* Register value */
  199. u16 temp_add[2]; /* Register value */
  200. u16 temp_max_add[2]; /* Register value */
  201. u16 temp_max_hyst_add[2]; /* Register value */
  202. u8 fan_div[3]; /* Register encoding, shifted right */
  203. u8 vid; /* Register encoding, combined */
  204. u32 alarms; /* Register encoding, combined */
  205. u32 beep_mask; /* Register encoding, combined */
  206. u8 beep_enable; /* Boolean */
  207. u8 pwm[4]; /* Register value */
  208. u8 pwm2_enable; /* Boolean */
  209. u16 sens[3]; /* 782D/783S only.
  210. 1 = pentium diode; 2 = 3904 diode;
  211. 3000-5000 = thermistor beta.
  212. Default = 3435.
  213. Other Betas unimplemented */
  214. u8 vrm;
  215. };
  216. static int w83781d_attach_adapter(struct i2c_adapter *adapter);
  217. static int w83781d_detect(struct i2c_adapter *adapter, int address, int kind);
  218. static int w83781d_detach_client(struct i2c_client *client);
  219. static int __devinit w83781d_isa_probe(struct platform_device *pdev);
  220. static int __devexit w83781d_isa_remove(struct platform_device *pdev);
  221. static int w83781d_read_value(struct w83781d_data *data, u16 reg);
  222. static int w83781d_write_value(struct w83781d_data *data, u16 reg, u16 value);
  223. static struct w83781d_data *w83781d_update_device(struct device *dev);
  224. static void w83781d_init_device(struct device *dev);
  225. static struct i2c_driver w83781d_driver = {
  226. .driver = {
  227. .name = "w83781d",
  228. },
  229. .id = I2C_DRIVERID_W83781D,
  230. .attach_adapter = w83781d_attach_adapter,
  231. .detach_client = w83781d_detach_client,
  232. };
  233. static struct platform_driver w83781d_isa_driver = {
  234. .driver = {
  235. .owner = THIS_MODULE,
  236. .name = "w83781d",
  237. },
  238. .probe = w83781d_isa_probe,
  239. .remove = w83781d_isa_remove,
  240. };
  241. /* following are the sysfs callback functions */
  242. #define show_in_reg(reg) \
  243. static ssize_t show_##reg (struct device *dev, struct device_attribute *da, \
  244. char *buf) \
  245. { \
  246. struct sensor_device_attribute *attr = to_sensor_dev_attr(da); \
  247. struct w83781d_data *data = w83781d_update_device(dev); \
  248. return sprintf(buf, "%ld\n", \
  249. (long)IN_FROM_REG(data->reg[attr->index])); \
  250. }
  251. show_in_reg(in);
  252. show_in_reg(in_min);
  253. show_in_reg(in_max);
  254. #define store_in_reg(REG, reg) \
  255. static ssize_t store_in_##reg (struct device *dev, struct device_attribute \
  256. *da, const char *buf, size_t count) \
  257. { \
  258. struct sensor_device_attribute *attr = to_sensor_dev_attr(da); \
  259. struct w83781d_data *data = dev_get_drvdata(dev); \
  260. int nr = attr->index; \
  261. u32 val; \
  262. \
  263. val = simple_strtoul(buf, NULL, 10); \
  264. \
  265. mutex_lock(&data->update_lock); \
  266. data->in_##reg[nr] = IN_TO_REG(val); \
  267. w83781d_write_value(data, W83781D_REG_IN_##REG(nr), data->in_##reg[nr]); \
  268. \
  269. mutex_unlock(&data->update_lock); \
  270. return count; \
  271. }
  272. store_in_reg(MIN, min);
  273. store_in_reg(MAX, max);
  274. #define sysfs_in_offsets(offset) \
  275. static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
  276. show_in, NULL, offset); \
  277. static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
  278. show_in_min, store_in_min, offset); \
  279. static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
  280. show_in_max, store_in_max, offset)
  281. sysfs_in_offsets(0);
  282. sysfs_in_offsets(1);
  283. sysfs_in_offsets(2);
  284. sysfs_in_offsets(3);
  285. sysfs_in_offsets(4);
  286. sysfs_in_offsets(5);
  287. sysfs_in_offsets(6);
  288. sysfs_in_offsets(7);
  289. sysfs_in_offsets(8);
  290. #define show_fan_reg(reg) \
  291. static ssize_t show_##reg (struct device *dev, struct device_attribute *da, \
  292. char *buf) \
  293. { \
  294. struct sensor_device_attribute *attr = to_sensor_dev_attr(da); \
  295. struct w83781d_data *data = w83781d_update_device(dev); \
  296. return sprintf(buf,"%ld\n", \
  297. FAN_FROM_REG(data->reg[attr->index], \
  298. DIV_FROM_REG(data->fan_div[attr->index]))); \
  299. }
  300. show_fan_reg(fan);
  301. show_fan_reg(fan_min);
  302. static ssize_t
  303. store_fan_min(struct device *dev, struct device_attribute *da,
  304. const char *buf, size_t count)
  305. {
  306. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  307. struct w83781d_data *data = dev_get_drvdata(dev);
  308. int nr = attr->index;
  309. u32 val;
  310. val = simple_strtoul(buf, NULL, 10);
  311. mutex_lock(&data->update_lock);
  312. data->fan_min[nr] =
  313. FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
  314. w83781d_write_value(data, W83781D_REG_FAN_MIN(nr),
  315. data->fan_min[nr]);
  316. mutex_unlock(&data->update_lock);
  317. return count;
  318. }
  319. static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
  320. static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO | S_IWUSR,
  321. show_fan_min, store_fan_min, 0);
  322. static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
  323. static SENSOR_DEVICE_ATTR(fan2_min, S_IRUGO | S_IWUSR,
  324. show_fan_min, store_fan_min, 1);
  325. static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2);
  326. static SENSOR_DEVICE_ATTR(fan3_min, S_IRUGO | S_IWUSR,
  327. show_fan_min, store_fan_min, 2);
  328. #define show_temp_reg(reg) \
  329. static ssize_t show_##reg (struct device *dev, struct device_attribute *da, \
  330. char *buf) \
  331. { \
  332. struct sensor_device_attribute *attr = to_sensor_dev_attr(da); \
  333. struct w83781d_data *data = w83781d_update_device(dev); \
  334. int nr = attr->index; \
  335. if (nr >= 2) { /* TEMP2 and TEMP3 */ \
  336. return sprintf(buf,"%d\n", \
  337. LM75_TEMP_FROM_REG(data->reg##_add[nr-2])); \
  338. } else { /* TEMP1 */ \
  339. return sprintf(buf,"%ld\n", (long)TEMP_FROM_REG(data->reg)); \
  340. } \
  341. }
  342. show_temp_reg(temp);
  343. show_temp_reg(temp_max);
  344. show_temp_reg(temp_max_hyst);
  345. #define store_temp_reg(REG, reg) \
  346. static ssize_t store_temp_##reg (struct device *dev, \
  347. struct device_attribute *da, const char *buf, size_t count) \
  348. { \
  349. struct sensor_device_attribute *attr = to_sensor_dev_attr(da); \
  350. struct w83781d_data *data = dev_get_drvdata(dev); \
  351. int nr = attr->index; \
  352. s32 val; \
  353. \
  354. val = simple_strtol(buf, NULL, 10); \
  355. \
  356. mutex_lock(&data->update_lock); \
  357. \
  358. if (nr >= 2) { /* TEMP2 and TEMP3 */ \
  359. data->temp_##reg##_add[nr-2] = LM75_TEMP_TO_REG(val); \
  360. w83781d_write_value(data, W83781D_REG_TEMP_##REG(nr), \
  361. data->temp_##reg##_add[nr-2]); \
  362. } else { /* TEMP1 */ \
  363. data->temp_##reg = TEMP_TO_REG(val); \
  364. w83781d_write_value(data, W83781D_REG_TEMP_##REG(nr), \
  365. data->temp_##reg); \
  366. } \
  367. \
  368. mutex_unlock(&data->update_lock); \
  369. return count; \
  370. }
  371. store_temp_reg(OVER, max);
  372. store_temp_reg(HYST, max_hyst);
  373. #define sysfs_temp_offsets(offset) \
  374. static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
  375. show_temp, NULL, offset); \
  376. static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
  377. show_temp_max, store_temp_max, offset); \
  378. static SENSOR_DEVICE_ATTR(temp##offset##_max_hyst, S_IRUGO | S_IWUSR, \
  379. show_temp_max_hyst, store_temp_max_hyst, offset);
  380. sysfs_temp_offsets(1);
  381. sysfs_temp_offsets(2);
  382. sysfs_temp_offsets(3);
  383. static ssize_t
  384. show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
  385. {
  386. struct w83781d_data *data = w83781d_update_device(dev);
  387. return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
  388. }
  389. static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
  390. static ssize_t
  391. show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
  392. {
  393. struct w83781d_data *data = w83781d_update_device(dev);
  394. return sprintf(buf, "%ld\n", (long) data->vrm);
  395. }
  396. static ssize_t
  397. store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  398. {
  399. struct w83781d_data *data = dev_get_drvdata(dev);
  400. u32 val;
  401. val = simple_strtoul(buf, NULL, 10);
  402. data->vrm = val;
  403. return count;
  404. }
  405. static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
  406. static ssize_t
  407. show_alarms_reg(struct device *dev, struct device_attribute *attr, char *buf)
  408. {
  409. struct w83781d_data *data = w83781d_update_device(dev);
  410. return sprintf(buf, "%u\n", data->alarms);
  411. }
  412. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
  413. static ssize_t show_beep_mask (struct device *dev, struct device_attribute *attr, char *buf)
  414. {
  415. struct w83781d_data *data = w83781d_update_device(dev);
  416. return sprintf(buf, "%ld\n",
  417. (long)BEEP_MASK_FROM_REG(data->beep_mask, data->type));
  418. }
  419. static ssize_t show_beep_enable (struct device *dev, struct device_attribute *attr, char *buf)
  420. {
  421. struct w83781d_data *data = w83781d_update_device(dev);
  422. return sprintf(buf, "%ld\n", (long)data->beep_enable);
  423. }
  424. static ssize_t
  425. store_beep_mask(struct device *dev, struct device_attribute *attr,
  426. const char *buf, size_t count)
  427. {
  428. struct w83781d_data *data = dev_get_drvdata(dev);
  429. u32 val;
  430. val = simple_strtoul(buf, NULL, 10);
  431. mutex_lock(&data->update_lock);
  432. data->beep_mask = BEEP_MASK_TO_REG(val, data->type);
  433. w83781d_write_value(data, W83781D_REG_BEEP_INTS1,
  434. data->beep_mask & 0xff);
  435. w83781d_write_value(data, W83781D_REG_BEEP_INTS2,
  436. ((data->beep_mask >> 8) & 0x7f)
  437. | data->beep_enable << 7);
  438. if (data->type != w83781d && data->type != as99127f) {
  439. w83781d_write_value(data, W83781D_REG_BEEP_INTS3,
  440. ((data->beep_mask) >> 16) & 0xff);
  441. }
  442. mutex_unlock(&data->update_lock);
  443. return count;
  444. }
  445. static ssize_t
  446. store_beep_enable(struct device *dev, struct device_attribute *attr,
  447. const char *buf, size_t count)
  448. {
  449. struct w83781d_data *data = dev_get_drvdata(dev);
  450. u32 val;
  451. val = simple_strtoul(buf, NULL, 10);
  452. if (val != 0 && val != 1)
  453. return -EINVAL;
  454. mutex_lock(&data->update_lock);
  455. data->beep_enable = val;
  456. val = w83781d_read_value(data, W83781D_REG_BEEP_INTS2) & 0x7f;
  457. val |= data->beep_enable << 7;
  458. w83781d_write_value(data, W83781D_REG_BEEP_INTS2, val);
  459. mutex_unlock(&data->update_lock);
  460. return count;
  461. }
  462. static DEVICE_ATTR(beep_mask, S_IRUGO | S_IWUSR,
  463. show_beep_mask, store_beep_mask);
  464. static DEVICE_ATTR(beep_enable, S_IRUGO | S_IWUSR,
  465. show_beep_enable, store_beep_enable);
  466. static ssize_t
  467. show_fan_div(struct device *dev, struct device_attribute *da, char *buf)
  468. {
  469. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  470. struct w83781d_data *data = w83781d_update_device(dev);
  471. return sprintf(buf, "%ld\n",
  472. (long) DIV_FROM_REG(data->fan_div[attr->index]));
  473. }
  474. /* Note: we save and restore the fan minimum here, because its value is
  475. determined in part by the fan divisor. This follows the principle of
  476. least surprise; the user doesn't expect the fan minimum to change just
  477. because the divisor changed. */
  478. static ssize_t
  479. store_fan_div(struct device *dev, struct device_attribute *da,
  480. const char *buf, size_t count)
  481. {
  482. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  483. struct w83781d_data *data = dev_get_drvdata(dev);
  484. unsigned long min;
  485. int nr = attr->index;
  486. u8 reg;
  487. unsigned long val = simple_strtoul(buf, NULL, 10);
  488. mutex_lock(&data->update_lock);
  489. /* Save fan_min */
  490. min = FAN_FROM_REG(data->fan_min[nr],
  491. DIV_FROM_REG(data->fan_div[nr]));
  492. data->fan_div[nr] = DIV_TO_REG(val, data->type);
  493. reg = (w83781d_read_value(data, nr==2 ? W83781D_REG_PIN : W83781D_REG_VID_FANDIV)
  494. & (nr==0 ? 0xcf : 0x3f))
  495. | ((data->fan_div[nr] & 0x03) << (nr==0 ? 4 : 6));
  496. w83781d_write_value(data, nr==2 ? W83781D_REG_PIN : W83781D_REG_VID_FANDIV, reg);
  497. /* w83781d and as99127f don't have extended divisor bits */
  498. if (data->type != w83781d && data->type != as99127f) {
  499. reg = (w83781d_read_value(data, W83781D_REG_VBAT)
  500. & ~(1 << (5 + nr)))
  501. | ((data->fan_div[nr] & 0x04) << (3 + nr));
  502. w83781d_write_value(data, W83781D_REG_VBAT, reg);
  503. }
  504. /* Restore fan_min */
  505. data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
  506. w83781d_write_value(data, W83781D_REG_FAN_MIN(nr), data->fan_min[nr]);
  507. mutex_unlock(&data->update_lock);
  508. return count;
  509. }
  510. static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR,
  511. show_fan_div, store_fan_div, 0);
  512. static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR,
  513. show_fan_div, store_fan_div, 1);
  514. static SENSOR_DEVICE_ATTR(fan3_div, S_IRUGO | S_IWUSR,
  515. show_fan_div, store_fan_div, 2);
  516. static ssize_t
  517. show_pwm(struct device *dev, struct device_attribute *da, char *buf)
  518. {
  519. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  520. struct w83781d_data *data = w83781d_update_device(dev);
  521. return sprintf(buf, "%d\n", (int)data->pwm[attr->index]);
  522. }
  523. static ssize_t
  524. show_pwm2_enable(struct device *dev, struct device_attribute *da, char *buf)
  525. {
  526. struct w83781d_data *data = w83781d_update_device(dev);
  527. return sprintf(buf, "%d\n", (int)data->pwm2_enable);
  528. }
  529. static ssize_t
  530. store_pwm(struct device *dev, struct device_attribute *da, const char *buf,
  531. size_t count)
  532. {
  533. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  534. struct w83781d_data *data = dev_get_drvdata(dev);
  535. int nr = attr->index;
  536. u32 val;
  537. val = simple_strtoul(buf, NULL, 10);
  538. mutex_lock(&data->update_lock);
  539. data->pwm[nr] = SENSORS_LIMIT(val, 0, 255);
  540. w83781d_write_value(data, W83781D_REG_PWM[nr], data->pwm[nr]);
  541. mutex_unlock(&data->update_lock);
  542. return count;
  543. }
  544. static ssize_t
  545. store_pwm2_enable(struct device *dev, struct device_attribute *da,
  546. const char *buf, size_t count)
  547. {
  548. struct w83781d_data *data = dev_get_drvdata(dev);
  549. u32 val, reg;
  550. val = simple_strtoul(buf, NULL, 10);
  551. mutex_lock(&data->update_lock);
  552. switch (val) {
  553. case 0:
  554. case 1:
  555. reg = w83781d_read_value(data, W83781D_REG_PWMCLK12);
  556. w83781d_write_value(data, W83781D_REG_PWMCLK12,
  557. (reg & 0xf7) | (val << 3));
  558. reg = w83781d_read_value(data, W83781D_REG_BEEP_CONFIG);
  559. w83781d_write_value(data, W83781D_REG_BEEP_CONFIG,
  560. (reg & 0xef) | (!val << 4));
  561. data->pwm2_enable = val;
  562. break;
  563. default:
  564. mutex_unlock(&data->update_lock);
  565. return -EINVAL;
  566. }
  567. mutex_unlock(&data->update_lock);
  568. return count;
  569. }
  570. static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, store_pwm, 0);
  571. static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, store_pwm, 1);
  572. static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm, store_pwm, 2);
  573. static SENSOR_DEVICE_ATTR(pwm4, S_IRUGO | S_IWUSR, show_pwm, store_pwm, 3);
  574. /* only PWM2 can be enabled/disabled */
  575. static DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR,
  576. show_pwm2_enable, store_pwm2_enable);
  577. static ssize_t
  578. show_sensor(struct device *dev, struct device_attribute *da, char *buf)
  579. {
  580. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  581. struct w83781d_data *data = w83781d_update_device(dev);
  582. return sprintf(buf, "%d\n", (int)data->sens[attr->index]);
  583. }
  584. static ssize_t
  585. store_sensor(struct device *dev, struct device_attribute *da,
  586. const char *buf, size_t count)
  587. {
  588. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  589. struct w83781d_data *data = dev_get_drvdata(dev);
  590. int nr = attr->index;
  591. u32 val, tmp;
  592. val = simple_strtoul(buf, NULL, 10);
  593. mutex_lock(&data->update_lock);
  594. switch (val) {
  595. case 1: /* PII/Celeron diode */
  596. tmp = w83781d_read_value(data, W83781D_REG_SCFG1);
  597. w83781d_write_value(data, W83781D_REG_SCFG1,
  598. tmp | BIT_SCFG1[nr]);
  599. tmp = w83781d_read_value(data, W83781D_REG_SCFG2);
  600. w83781d_write_value(data, W83781D_REG_SCFG2,
  601. tmp | BIT_SCFG2[nr]);
  602. data->sens[nr] = val;
  603. break;
  604. case 2: /* 3904 */
  605. tmp = w83781d_read_value(data, W83781D_REG_SCFG1);
  606. w83781d_write_value(data, W83781D_REG_SCFG1,
  607. tmp | BIT_SCFG1[nr]);
  608. tmp = w83781d_read_value(data, W83781D_REG_SCFG2);
  609. w83781d_write_value(data, W83781D_REG_SCFG2,
  610. tmp & ~BIT_SCFG2[nr]);
  611. data->sens[nr] = val;
  612. break;
  613. case W83781D_DEFAULT_BETA: /* thermistor */
  614. tmp = w83781d_read_value(data, W83781D_REG_SCFG1);
  615. w83781d_write_value(data, W83781D_REG_SCFG1,
  616. tmp & ~BIT_SCFG1[nr]);
  617. data->sens[nr] = val;
  618. break;
  619. default:
  620. dev_err(dev, "Invalid sensor type %ld; must be 1, 2, or %d\n",
  621. (long) val, W83781D_DEFAULT_BETA);
  622. break;
  623. }
  624. mutex_unlock(&data->update_lock);
  625. return count;
  626. }
  627. static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO | S_IWUSR,
  628. show_sensor, store_sensor, 0);
  629. static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO | S_IWUSR,
  630. show_sensor, store_sensor, 0);
  631. static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO | S_IWUSR,
  632. show_sensor, store_sensor, 0);
  633. /* I2C devices get this name attribute automatically, but for ISA devices
  634. we must create it by ourselves. */
  635. static ssize_t
  636. show_name(struct device *dev, struct device_attribute *devattr, char *buf)
  637. {
  638. struct w83781d_data *data = dev_get_drvdata(dev);
  639. return sprintf(buf, "%s\n", data->client.name);
  640. }
  641. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  642. /* This function is called when:
  643. * w83781d_driver is inserted (when this module is loaded), for each
  644. available adapter
  645. * when a new adapter is inserted (and w83781d_driver is still present) */
  646. static int
  647. w83781d_attach_adapter(struct i2c_adapter *adapter)
  648. {
  649. if (!(adapter->class & I2C_CLASS_HWMON))
  650. return 0;
  651. return i2c_probe(adapter, &addr_data, w83781d_detect);
  652. }
  653. /* Assumes that adapter is of I2C, not ISA variety.
  654. * OTHERWISE DON'T CALL THIS
  655. */
  656. static int
  657. w83781d_detect_subclients(struct i2c_adapter *adapter, int address, int kind,
  658. struct i2c_client *new_client)
  659. {
  660. int i, val1 = 0, id;
  661. int err;
  662. const char *client_name = "";
  663. struct w83781d_data *data = i2c_get_clientdata(new_client);
  664. data->lm75[0] = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
  665. if (!(data->lm75[0])) {
  666. err = -ENOMEM;
  667. goto ERROR_SC_0;
  668. }
  669. id = i2c_adapter_id(adapter);
  670. if (force_subclients[0] == id && force_subclients[1] == address) {
  671. for (i = 2; i <= 3; i++) {
  672. if (force_subclients[i] < 0x48 ||
  673. force_subclients[i] > 0x4f) {
  674. dev_err(&new_client->dev, "Invalid subclient "
  675. "address %d; must be 0x48-0x4f\n",
  676. force_subclients[i]);
  677. err = -EINVAL;
  678. goto ERROR_SC_1;
  679. }
  680. }
  681. w83781d_write_value(data, W83781D_REG_I2C_SUBADDR,
  682. (force_subclients[2] & 0x07) |
  683. ((force_subclients[3] & 0x07) << 4));
  684. data->lm75[0]->addr = force_subclients[2];
  685. } else {
  686. val1 = w83781d_read_value(data, W83781D_REG_I2C_SUBADDR);
  687. data->lm75[0]->addr = 0x48 + (val1 & 0x07);
  688. }
  689. if (kind != w83783s) {
  690. data->lm75[1] = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
  691. if (!(data->lm75[1])) {
  692. err = -ENOMEM;
  693. goto ERROR_SC_1;
  694. }
  695. if (force_subclients[0] == id &&
  696. force_subclients[1] == address) {
  697. data->lm75[1]->addr = force_subclients[3];
  698. } else {
  699. data->lm75[1]->addr = 0x48 + ((val1 >> 4) & 0x07);
  700. }
  701. if (data->lm75[0]->addr == data->lm75[1]->addr) {
  702. dev_err(&new_client->dev,
  703. "Duplicate addresses 0x%x for subclients.\n",
  704. data->lm75[0]->addr);
  705. err = -EBUSY;
  706. goto ERROR_SC_2;
  707. }
  708. }
  709. if (kind == w83781d)
  710. client_name = "w83781d subclient";
  711. else if (kind == w83782d)
  712. client_name = "w83782d subclient";
  713. else if (kind == w83783s)
  714. client_name = "w83783s subclient";
  715. else if (kind == w83627hf)
  716. client_name = "w83627hf subclient";
  717. else if (kind == as99127f)
  718. client_name = "as99127f subclient";
  719. for (i = 0; i <= 1; i++) {
  720. /* store all data in w83781d */
  721. i2c_set_clientdata(data->lm75[i], NULL);
  722. data->lm75[i]->adapter = adapter;
  723. data->lm75[i]->driver = &w83781d_driver;
  724. data->lm75[i]->flags = 0;
  725. strlcpy(data->lm75[i]->name, client_name,
  726. I2C_NAME_SIZE);
  727. if ((err = i2c_attach_client(data->lm75[i]))) {
  728. dev_err(&new_client->dev, "Subclient %d "
  729. "registration at address 0x%x "
  730. "failed.\n", i, data->lm75[i]->addr);
  731. if (i == 1)
  732. goto ERROR_SC_3;
  733. goto ERROR_SC_2;
  734. }
  735. if (kind == w83783s)
  736. break;
  737. }
  738. return 0;
  739. /* Undo inits in case of errors */
  740. ERROR_SC_3:
  741. i2c_detach_client(data->lm75[0]);
  742. ERROR_SC_2:
  743. kfree(data->lm75[1]);
  744. ERROR_SC_1:
  745. kfree(data->lm75[0]);
  746. ERROR_SC_0:
  747. return err;
  748. }
  749. #define IN_UNIT_ATTRS(X) \
  750. &sensor_dev_attr_in##X##_input.dev_attr.attr, \
  751. &sensor_dev_attr_in##X##_min.dev_attr.attr, \
  752. &sensor_dev_attr_in##X##_max.dev_attr.attr
  753. #define FAN_UNIT_ATTRS(X) \
  754. &sensor_dev_attr_fan##X##_input.dev_attr.attr, \
  755. &sensor_dev_attr_fan##X##_min.dev_attr.attr, \
  756. &sensor_dev_attr_fan##X##_div.dev_attr.attr
  757. #define TEMP_UNIT_ATTRS(X) \
  758. &sensor_dev_attr_temp##X##_input.dev_attr.attr, \
  759. &sensor_dev_attr_temp##X##_max.dev_attr.attr, \
  760. &sensor_dev_attr_temp##X##_max_hyst.dev_attr.attr
  761. static struct attribute* w83781d_attributes[] = {
  762. IN_UNIT_ATTRS(0),
  763. IN_UNIT_ATTRS(2),
  764. IN_UNIT_ATTRS(3),
  765. IN_UNIT_ATTRS(4),
  766. IN_UNIT_ATTRS(5),
  767. IN_UNIT_ATTRS(6),
  768. FAN_UNIT_ATTRS(1),
  769. FAN_UNIT_ATTRS(2),
  770. FAN_UNIT_ATTRS(3),
  771. TEMP_UNIT_ATTRS(1),
  772. TEMP_UNIT_ATTRS(2),
  773. &dev_attr_cpu0_vid.attr,
  774. &dev_attr_vrm.attr,
  775. &dev_attr_alarms.attr,
  776. &dev_attr_beep_mask.attr,
  777. &dev_attr_beep_enable.attr,
  778. NULL
  779. };
  780. static const struct attribute_group w83781d_group = {
  781. .attrs = w83781d_attributes,
  782. };
  783. static struct attribute *w83781d_attributes_opt[] = {
  784. IN_UNIT_ATTRS(1),
  785. IN_UNIT_ATTRS(7),
  786. IN_UNIT_ATTRS(8),
  787. TEMP_UNIT_ATTRS(3),
  788. &sensor_dev_attr_pwm1.dev_attr.attr,
  789. &sensor_dev_attr_pwm2.dev_attr.attr,
  790. &sensor_dev_attr_pwm3.dev_attr.attr,
  791. &sensor_dev_attr_pwm4.dev_attr.attr,
  792. &dev_attr_pwm2_enable.attr,
  793. &sensor_dev_attr_temp1_type.dev_attr.attr,
  794. &sensor_dev_attr_temp2_type.dev_attr.attr,
  795. &sensor_dev_attr_temp3_type.dev_attr.attr,
  796. NULL
  797. };
  798. static const struct attribute_group w83781d_group_opt = {
  799. .attrs = w83781d_attributes_opt,
  800. };
  801. /* No clean up is done on error, it's up to the caller */
  802. static int
  803. w83781d_create_files(struct device *dev, int kind, int is_isa)
  804. {
  805. int err;
  806. if ((err = sysfs_create_group(&dev->kobj, &w83781d_group)))
  807. return err;
  808. if (kind != w83783s) {
  809. if ((err = device_create_file(dev,
  810. &sensor_dev_attr_in1_input.dev_attr))
  811. || (err = device_create_file(dev,
  812. &sensor_dev_attr_in1_min.dev_attr))
  813. || (err = device_create_file(dev,
  814. &sensor_dev_attr_in1_max.dev_attr)))
  815. return err;
  816. }
  817. if (kind != as99127f && kind != w83781d && kind != w83783s) {
  818. if ((err = device_create_file(dev,
  819. &sensor_dev_attr_in7_input.dev_attr))
  820. || (err = device_create_file(dev,
  821. &sensor_dev_attr_in7_min.dev_attr))
  822. || (err = device_create_file(dev,
  823. &sensor_dev_attr_in7_max.dev_attr))
  824. || (err = device_create_file(dev,
  825. &sensor_dev_attr_in8_input.dev_attr))
  826. || (err = device_create_file(dev,
  827. &sensor_dev_attr_in8_min.dev_attr))
  828. || (err = device_create_file(dev,
  829. &sensor_dev_attr_in8_max.dev_attr)))
  830. return err;
  831. }
  832. if (kind != w83783s) {
  833. if ((err = device_create_file(dev,
  834. &sensor_dev_attr_temp3_input.dev_attr))
  835. || (err = device_create_file(dev,
  836. &sensor_dev_attr_temp3_max.dev_attr))
  837. || (err = device_create_file(dev,
  838. &sensor_dev_attr_temp3_max_hyst.dev_attr)))
  839. return err;
  840. }
  841. if (kind != w83781d && kind != as99127f) {
  842. if ((err = device_create_file(dev,
  843. &sensor_dev_attr_pwm1.dev_attr))
  844. || (err = device_create_file(dev,
  845. &sensor_dev_attr_pwm2.dev_attr))
  846. || (err = device_create_file(dev, &dev_attr_pwm2_enable)))
  847. return err;
  848. }
  849. if (kind == w83782d && !is_isa) {
  850. if ((err = device_create_file(dev,
  851. &sensor_dev_attr_pwm3.dev_attr))
  852. || (err = device_create_file(dev,
  853. &sensor_dev_attr_pwm4.dev_attr)))
  854. return err;
  855. }
  856. if (kind != as99127f && kind != w83781d) {
  857. if ((err = device_create_file(dev,
  858. &sensor_dev_attr_temp1_type.dev_attr))
  859. || (err = device_create_file(dev,
  860. &sensor_dev_attr_temp2_type.dev_attr)))
  861. return err;
  862. if (kind != w83783s) {
  863. if ((err = device_create_file(dev,
  864. &sensor_dev_attr_temp3_type.dev_attr)))
  865. return err;
  866. }
  867. }
  868. if (is_isa) {
  869. err = device_create_file(&pdev->dev, &dev_attr_name);
  870. if (err)
  871. return err;
  872. }
  873. return 0;
  874. }
  875. static int
  876. w83781d_detect(struct i2c_adapter *adapter, int address, int kind)
  877. {
  878. int val1 = 0, val2;
  879. struct i2c_client *client;
  880. struct device *dev;
  881. struct w83781d_data *data;
  882. int err;
  883. const char *client_name = "";
  884. enum vendor { winbond, asus } vendid;
  885. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
  886. err = -EINVAL;
  887. goto ERROR1;
  888. }
  889. /* OK. For now, we presume we have a valid client. We now create the
  890. client structure, even though we cannot fill it completely yet.
  891. But it allows us to access w83781d_{read,write}_value. */
  892. if (!(data = kzalloc(sizeof(struct w83781d_data), GFP_KERNEL))) {
  893. err = -ENOMEM;
  894. goto ERROR1;
  895. }
  896. client = &data->client;
  897. i2c_set_clientdata(client, data);
  898. client->addr = address;
  899. mutex_init(&data->lock);
  900. client->adapter = adapter;
  901. client->driver = &w83781d_driver;
  902. dev = &client->dev;
  903. /* Now, we do the remaining detection. */
  904. /* The w8378?d may be stuck in some other bank than bank 0. This may
  905. make reading other information impossible. Specify a force=... or
  906. force_*=... parameter, and the Winbond will be reset to the right
  907. bank. */
  908. if (kind < 0) {
  909. if (w83781d_read_value(data, W83781D_REG_CONFIG) & 0x80) {
  910. dev_dbg(&adapter->dev, "Detection of w83781d chip "
  911. "failed at step 3\n");
  912. err = -ENODEV;
  913. goto ERROR2;
  914. }
  915. val1 = w83781d_read_value(data, W83781D_REG_BANK);
  916. val2 = w83781d_read_value(data, W83781D_REG_CHIPMAN);
  917. /* Check for Winbond or Asus ID if in bank 0 */
  918. if ((!(val1 & 0x07)) &&
  919. (((!(val1 & 0x80)) && (val2 != 0xa3) && (val2 != 0xc3))
  920. || ((val1 & 0x80) && (val2 != 0x5c) && (val2 != 0x12)))) {
  921. dev_dbg(&adapter->dev, "Detection of w83781d chip "
  922. "failed at step 4\n");
  923. err = -ENODEV;
  924. goto ERROR2;
  925. }
  926. /* If Winbond SMBus, check address at 0x48.
  927. Asus doesn't support, except for as99127f rev.2 */
  928. if ((!(val1 & 0x80) && (val2 == 0xa3)) ||
  929. ((val1 & 0x80) && (val2 == 0x5c))) {
  930. if (w83781d_read_value
  931. (data, W83781D_REG_I2C_ADDR) != address) {
  932. dev_dbg(&adapter->dev, "Detection of w83781d "
  933. "chip failed at step 5\n");
  934. err = -ENODEV;
  935. goto ERROR2;
  936. }
  937. }
  938. }
  939. /* We have either had a force parameter, or we have already detected the
  940. Winbond. Put it now into bank 0 and Vendor ID High Byte */
  941. w83781d_write_value(data, W83781D_REG_BANK,
  942. (w83781d_read_value(data, W83781D_REG_BANK)
  943. & 0x78) | 0x80);
  944. /* Determine the chip type. */
  945. if (kind <= 0) {
  946. /* get vendor ID */
  947. val2 = w83781d_read_value(data, W83781D_REG_CHIPMAN);
  948. if (val2 == 0x5c)
  949. vendid = winbond;
  950. else if (val2 == 0x12)
  951. vendid = asus;
  952. else {
  953. dev_dbg(&adapter->dev, "w83781d chip vendor is "
  954. "neither Winbond nor Asus\n");
  955. err = -ENODEV;
  956. goto ERROR2;
  957. }
  958. val1 = w83781d_read_value(data, W83781D_REG_WCHIPID);
  959. if ((val1 == 0x10 || val1 == 0x11) && vendid == winbond)
  960. kind = w83781d;
  961. else if (val1 == 0x30 && vendid == winbond)
  962. kind = w83782d;
  963. else if (val1 == 0x40 && vendid == winbond && address == 0x2d)
  964. kind = w83783s;
  965. else if (val1 == 0x21 && vendid == winbond)
  966. kind = w83627hf;
  967. else if (val1 == 0x31 && address >= 0x28)
  968. kind = as99127f;
  969. else {
  970. if (kind == 0)
  971. dev_warn(&adapter->dev, "Ignoring 'force' "
  972. "parameter for unknown chip at "
  973. "address 0x%02x\n", address);
  974. err = -EINVAL;
  975. goto ERROR2;
  976. }
  977. }
  978. if (kind == w83781d) {
  979. client_name = "w83781d";
  980. } else if (kind == w83782d) {
  981. client_name = "w83782d";
  982. } else if (kind == w83783s) {
  983. client_name = "w83783s";
  984. } else if (kind == w83627hf) {
  985. client_name = "w83627hf";
  986. } else if (kind == as99127f) {
  987. client_name = "as99127f";
  988. }
  989. /* Fill in the remaining client fields and put into the global list */
  990. strlcpy(client->name, client_name, I2C_NAME_SIZE);
  991. data->type = kind;
  992. /* Tell the I2C layer a new client has arrived */
  993. if ((err = i2c_attach_client(client)))
  994. goto ERROR2;
  995. /* attach secondary i2c lm75-like clients */
  996. if ((err = w83781d_detect_subclients(adapter, address,
  997. kind, client)))
  998. goto ERROR3;
  999. /* Initialize the chip */
  1000. w83781d_init_device(dev);
  1001. /* Register sysfs hooks */
  1002. err = w83781d_create_files(dev, kind, 0);
  1003. if (err)
  1004. goto ERROR4;
  1005. data->class_dev = hwmon_device_register(dev);
  1006. if (IS_ERR(data->class_dev)) {
  1007. err = PTR_ERR(data->class_dev);
  1008. goto ERROR4;
  1009. }
  1010. return 0;
  1011. ERROR4:
  1012. sysfs_remove_group(&dev->kobj, &w83781d_group);
  1013. sysfs_remove_group(&dev->kobj, &w83781d_group_opt);
  1014. if (data->lm75[1]) {
  1015. i2c_detach_client(data->lm75[1]);
  1016. kfree(data->lm75[1]);
  1017. }
  1018. if (data->lm75[0]) {
  1019. i2c_detach_client(data->lm75[0]);
  1020. kfree(data->lm75[0]);
  1021. }
  1022. ERROR3:
  1023. i2c_detach_client(client);
  1024. ERROR2:
  1025. kfree(data);
  1026. ERROR1:
  1027. return err;
  1028. }
  1029. static int
  1030. w83781d_detach_client(struct i2c_client *client)
  1031. {
  1032. struct w83781d_data *data = i2c_get_clientdata(client);
  1033. int err;
  1034. /* main client */
  1035. if (data) {
  1036. hwmon_device_unregister(data->class_dev);
  1037. sysfs_remove_group(&client->dev.kobj, &w83781d_group);
  1038. sysfs_remove_group(&client->dev.kobj, &w83781d_group_opt);
  1039. }
  1040. if ((err = i2c_detach_client(client)))
  1041. return err;
  1042. /* main client */
  1043. if (data)
  1044. kfree(data);
  1045. /* subclient */
  1046. else
  1047. kfree(client);
  1048. return 0;
  1049. }
  1050. static int __devinit
  1051. w83781d_isa_probe(struct platform_device *pdev)
  1052. {
  1053. int err, reg;
  1054. struct w83781d_data *data;
  1055. struct resource *res;
  1056. const char *name;
  1057. /* Reserve the ISA region */
  1058. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  1059. if (!request_region(res->start, W83781D_EXTENT, "w83781d")) {
  1060. err = -EBUSY;
  1061. goto exit;
  1062. }
  1063. if (!(data = kzalloc(sizeof(struct w83781d_data), GFP_KERNEL))) {
  1064. err = -ENOMEM;
  1065. goto exit_release_region;
  1066. }
  1067. mutex_init(&data->lock);
  1068. data->client.addr = res->start;
  1069. i2c_set_clientdata(&data->client, data);
  1070. platform_set_drvdata(pdev, data);
  1071. reg = w83781d_read_value(data, W83781D_REG_WCHIPID);
  1072. switch (reg) {
  1073. case 0x21:
  1074. data->type = w83627hf;
  1075. name = "w83627hf";
  1076. break;
  1077. case 0x30:
  1078. data->type = w83782d;
  1079. name = "w83782d";
  1080. break;
  1081. default:
  1082. data->type = w83781d;
  1083. name = "w83781d";
  1084. }
  1085. strlcpy(data->client.name, name, I2C_NAME_SIZE);
  1086. /* Initialize the W83781D chip */
  1087. w83781d_init_device(&pdev->dev);
  1088. /* Register sysfs hooks */
  1089. err = w83781d_create_files(&pdev->dev, data->type, 1);
  1090. if (err)
  1091. goto exit_remove_files;
  1092. data->class_dev = hwmon_device_register(&pdev->dev);
  1093. if (IS_ERR(data->class_dev)) {
  1094. err = PTR_ERR(data->class_dev);
  1095. goto exit_remove_files;
  1096. }
  1097. return 0;
  1098. exit_remove_files:
  1099. sysfs_remove_group(&pdev->dev.kobj, &w83781d_group);
  1100. sysfs_remove_group(&pdev->dev.kobj, &w83781d_group_opt);
  1101. device_remove_file(&pdev->dev, &dev_attr_name);
  1102. kfree(data);
  1103. exit_release_region:
  1104. release_region(res->start, W83781D_EXTENT);
  1105. exit:
  1106. return err;
  1107. }
  1108. static int __devexit
  1109. w83781d_isa_remove(struct platform_device *pdev)
  1110. {
  1111. struct w83781d_data *data = platform_get_drvdata(pdev);
  1112. hwmon_device_unregister(data->class_dev);
  1113. sysfs_remove_group(&pdev->dev.kobj, &w83781d_group);
  1114. sysfs_remove_group(&pdev->dev.kobj, &w83781d_group_opt);
  1115. device_remove_file(&pdev->dev, &dev_attr_name);
  1116. release_region(data->client.addr, W83781D_EXTENT);
  1117. kfree(data);
  1118. return 0;
  1119. }
  1120. /* The SMBus locks itself, usually, but nothing may access the Winbond between
  1121. bank switches. ISA access must always be locked explicitly!
  1122. We ignore the W83781D BUSY flag at this moment - it could lead to deadlocks,
  1123. would slow down the W83781D access and should not be necessary.
  1124. There are some ugly typecasts here, but the good news is - they should
  1125. nowhere else be necessary! */
  1126. static int
  1127. w83781d_read_value(struct w83781d_data *data, u16 reg)
  1128. {
  1129. struct i2c_client *client = &data->client;
  1130. int res, word_sized, bank;
  1131. struct i2c_client *cl;
  1132. mutex_lock(&data->lock);
  1133. if (!client->driver) { /* ISA device */
  1134. word_sized = (((reg & 0xff00) == 0x100)
  1135. || ((reg & 0xff00) == 0x200))
  1136. && (((reg & 0x00ff) == 0x50)
  1137. || ((reg & 0x00ff) == 0x53)
  1138. || ((reg & 0x00ff) == 0x55));
  1139. if (reg & 0xff00) {
  1140. outb_p(W83781D_REG_BANK,
  1141. client->addr + W83781D_ADDR_REG_OFFSET);
  1142. outb_p(reg >> 8,
  1143. client->addr + W83781D_DATA_REG_OFFSET);
  1144. }
  1145. outb_p(reg & 0xff, client->addr + W83781D_ADDR_REG_OFFSET);
  1146. res = inb_p(client->addr + W83781D_DATA_REG_OFFSET);
  1147. if (word_sized) {
  1148. outb_p((reg & 0xff) + 1,
  1149. client->addr + W83781D_ADDR_REG_OFFSET);
  1150. res =
  1151. (res << 8) + inb_p(client->addr +
  1152. W83781D_DATA_REG_OFFSET);
  1153. }
  1154. if (reg & 0xff00) {
  1155. outb_p(W83781D_REG_BANK,
  1156. client->addr + W83781D_ADDR_REG_OFFSET);
  1157. outb_p(0, client->addr + W83781D_DATA_REG_OFFSET);
  1158. }
  1159. } else {
  1160. bank = (reg >> 8) & 0x0f;
  1161. if (bank > 2)
  1162. /* switch banks */
  1163. i2c_smbus_write_byte_data(client, W83781D_REG_BANK,
  1164. bank);
  1165. if (bank == 0 || bank > 2) {
  1166. res = i2c_smbus_read_byte_data(client, reg & 0xff);
  1167. } else {
  1168. /* switch to subclient */
  1169. cl = data->lm75[bank - 1];
  1170. /* convert from ISA to LM75 I2C addresses */
  1171. switch (reg & 0xff) {
  1172. case 0x50: /* TEMP */
  1173. res = swab16(i2c_smbus_read_word_data(cl, 0));
  1174. break;
  1175. case 0x52: /* CONFIG */
  1176. res = i2c_smbus_read_byte_data(cl, 1);
  1177. break;
  1178. case 0x53: /* HYST */
  1179. res = swab16(i2c_smbus_read_word_data(cl, 2));
  1180. break;
  1181. case 0x55: /* OVER */
  1182. default:
  1183. res = swab16(i2c_smbus_read_word_data(cl, 3));
  1184. break;
  1185. }
  1186. }
  1187. if (bank > 2)
  1188. i2c_smbus_write_byte_data(client, W83781D_REG_BANK, 0);
  1189. }
  1190. mutex_unlock(&data->lock);
  1191. return res;
  1192. }
  1193. static int
  1194. w83781d_write_value(struct w83781d_data *data, u16 reg, u16 value)
  1195. {
  1196. struct i2c_client *client = &data->client;
  1197. int word_sized, bank;
  1198. struct i2c_client *cl;
  1199. mutex_lock(&data->lock);
  1200. if (!client->driver) { /* ISA device */
  1201. word_sized = (((reg & 0xff00) == 0x100)
  1202. || ((reg & 0xff00) == 0x200))
  1203. && (((reg & 0x00ff) == 0x53)
  1204. || ((reg & 0x00ff) == 0x55));
  1205. if (reg & 0xff00) {
  1206. outb_p(W83781D_REG_BANK,
  1207. client->addr + W83781D_ADDR_REG_OFFSET);
  1208. outb_p(reg >> 8,
  1209. client->addr + W83781D_DATA_REG_OFFSET);
  1210. }
  1211. outb_p(reg & 0xff, client->addr + W83781D_ADDR_REG_OFFSET);
  1212. if (word_sized) {
  1213. outb_p(value >> 8,
  1214. client->addr + W83781D_DATA_REG_OFFSET);
  1215. outb_p((reg & 0xff) + 1,
  1216. client->addr + W83781D_ADDR_REG_OFFSET);
  1217. }
  1218. outb_p(value & 0xff, client->addr + W83781D_DATA_REG_OFFSET);
  1219. if (reg & 0xff00) {
  1220. outb_p(W83781D_REG_BANK,
  1221. client->addr + W83781D_ADDR_REG_OFFSET);
  1222. outb_p(0, client->addr + W83781D_DATA_REG_OFFSET);
  1223. }
  1224. } else {
  1225. bank = (reg >> 8) & 0x0f;
  1226. if (bank > 2)
  1227. /* switch banks */
  1228. i2c_smbus_write_byte_data(client, W83781D_REG_BANK,
  1229. bank);
  1230. if (bank == 0 || bank > 2) {
  1231. i2c_smbus_write_byte_data(client, reg & 0xff,
  1232. value & 0xff);
  1233. } else {
  1234. /* switch to subclient */
  1235. cl = data->lm75[bank - 1];
  1236. /* convert from ISA to LM75 I2C addresses */
  1237. switch (reg & 0xff) {
  1238. case 0x52: /* CONFIG */
  1239. i2c_smbus_write_byte_data(cl, 1, value & 0xff);
  1240. break;
  1241. case 0x53: /* HYST */
  1242. i2c_smbus_write_word_data(cl, 2, swab16(value));
  1243. break;
  1244. case 0x55: /* OVER */
  1245. i2c_smbus_write_word_data(cl, 3, swab16(value));
  1246. break;
  1247. }
  1248. }
  1249. if (bank > 2)
  1250. i2c_smbus_write_byte_data(client, W83781D_REG_BANK, 0);
  1251. }
  1252. mutex_unlock(&data->lock);
  1253. return 0;
  1254. }
  1255. static void
  1256. w83781d_init_device(struct device *dev)
  1257. {
  1258. struct w83781d_data *data = dev_get_drvdata(dev);
  1259. int i, p;
  1260. int type = data->type;
  1261. u8 tmp;
  1262. if (type == w83627hf)
  1263. dev_info(dev, "The W83627HF chip is better supported by the "
  1264. "w83627hf driver, support will be dropped from the "
  1265. "w83781d driver soon\n");
  1266. if (reset && type != as99127f) { /* this resets registers we don't have
  1267. documentation for on the as99127f */
  1268. /* Resetting the chip has been the default for a long time,
  1269. but it causes the BIOS initializations (fan clock dividers,
  1270. thermal sensor types...) to be lost, so it is now optional.
  1271. It might even go away if nobody reports it as being useful,
  1272. as I see very little reason why this would be needed at
  1273. all. */
  1274. dev_info(dev, "If reset=1 solved a problem you were "
  1275. "having, please report!\n");
  1276. /* save these registers */
  1277. i = w83781d_read_value(data, W83781D_REG_BEEP_CONFIG);
  1278. p = w83781d_read_value(data, W83781D_REG_PWMCLK12);
  1279. /* Reset all except Watchdog values and last conversion values
  1280. This sets fan-divs to 2, among others */
  1281. w83781d_write_value(data, W83781D_REG_CONFIG, 0x80);
  1282. /* Restore the registers and disable power-on abnormal beep.
  1283. This saves FAN 1/2/3 input/output values set by BIOS. */
  1284. w83781d_write_value(data, W83781D_REG_BEEP_CONFIG, i | 0x80);
  1285. w83781d_write_value(data, W83781D_REG_PWMCLK12, p);
  1286. /* Disable master beep-enable (reset turns it on).
  1287. Individual beep_mask should be reset to off but for some reason
  1288. disabling this bit helps some people not get beeped */
  1289. w83781d_write_value(data, W83781D_REG_BEEP_INTS2, 0);
  1290. }
  1291. /* Disable power-on abnormal beep, as advised by the datasheet.
  1292. Already done if reset=1. */
  1293. if (init && !reset && type != as99127f) {
  1294. i = w83781d_read_value(data, W83781D_REG_BEEP_CONFIG);
  1295. w83781d_write_value(data, W83781D_REG_BEEP_CONFIG, i | 0x80);
  1296. }
  1297. data->vrm = vid_which_vrm();
  1298. if ((type != w83781d) && (type != as99127f)) {
  1299. tmp = w83781d_read_value(data, W83781D_REG_SCFG1);
  1300. for (i = 1; i <= 3; i++) {
  1301. if (!(tmp & BIT_SCFG1[i - 1])) {
  1302. data->sens[i - 1] = W83781D_DEFAULT_BETA;
  1303. } else {
  1304. if (w83781d_read_value
  1305. (data,
  1306. W83781D_REG_SCFG2) & BIT_SCFG2[i - 1])
  1307. data->sens[i - 1] = 1;
  1308. else
  1309. data->sens[i - 1] = 2;
  1310. }
  1311. if (type == w83783s && i == 2)
  1312. break;
  1313. }
  1314. }
  1315. if (init && type != as99127f) {
  1316. /* Enable temp2 */
  1317. tmp = w83781d_read_value(data, W83781D_REG_TEMP2_CONFIG);
  1318. if (tmp & 0x01) {
  1319. dev_warn(dev, "Enabling temp2, readings "
  1320. "might not make sense\n");
  1321. w83781d_write_value(data, W83781D_REG_TEMP2_CONFIG,
  1322. tmp & 0xfe);
  1323. }
  1324. /* Enable temp3 */
  1325. if (type != w83783s) {
  1326. tmp = w83781d_read_value(data,
  1327. W83781D_REG_TEMP3_CONFIG);
  1328. if (tmp & 0x01) {
  1329. dev_warn(dev, "Enabling temp3, "
  1330. "readings might not make sense\n");
  1331. w83781d_write_value(data,
  1332. W83781D_REG_TEMP3_CONFIG, tmp & 0xfe);
  1333. }
  1334. }
  1335. }
  1336. /* Start monitoring */
  1337. w83781d_write_value(data, W83781D_REG_CONFIG,
  1338. (w83781d_read_value(data,
  1339. W83781D_REG_CONFIG) & 0xf7)
  1340. | 0x01);
  1341. /* A few vars need to be filled upon startup */
  1342. for (i = 0; i < 3; i++) {
  1343. data->fan_min[i] = w83781d_read_value(data,
  1344. W83781D_REG_FAN_MIN(i));
  1345. }
  1346. mutex_init(&data->update_lock);
  1347. }
  1348. static struct w83781d_data *w83781d_update_device(struct device *dev)
  1349. {
  1350. struct w83781d_data *data = dev_get_drvdata(dev);
  1351. struct i2c_client *client = &data->client;
  1352. int i;
  1353. mutex_lock(&data->update_lock);
  1354. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  1355. || !data->valid) {
  1356. dev_dbg(dev, "Starting device update\n");
  1357. for (i = 0; i <= 8; i++) {
  1358. if (data->type == w83783s && i == 1)
  1359. continue; /* 783S has no in1 */
  1360. data->in[i] =
  1361. w83781d_read_value(data, W83781D_REG_IN(i));
  1362. data->in_min[i] =
  1363. w83781d_read_value(data, W83781D_REG_IN_MIN(i));
  1364. data->in_max[i] =
  1365. w83781d_read_value(data, W83781D_REG_IN_MAX(i));
  1366. if ((data->type != w83782d)
  1367. && (data->type != w83627hf) && (i == 6))
  1368. break;
  1369. }
  1370. for (i = 0; i < 3; i++) {
  1371. data->fan[i] =
  1372. w83781d_read_value(data, W83781D_REG_FAN(i));
  1373. data->fan_min[i] =
  1374. w83781d_read_value(data, W83781D_REG_FAN_MIN(i));
  1375. }
  1376. if (data->type != w83781d && data->type != as99127f) {
  1377. for (i = 0; i < 4; i++) {
  1378. data->pwm[i] =
  1379. w83781d_read_value(data,
  1380. W83781D_REG_PWM[i]);
  1381. if ((data->type != w83782d || !client->driver)
  1382. && i == 1)
  1383. break;
  1384. }
  1385. /* Only PWM2 can be disabled */
  1386. data->pwm2_enable = (w83781d_read_value(data,
  1387. W83781D_REG_PWMCLK12) & 0x08) >> 3;
  1388. }
  1389. data->temp = w83781d_read_value(data, W83781D_REG_TEMP(1));
  1390. data->temp_max =
  1391. w83781d_read_value(data, W83781D_REG_TEMP_OVER(1));
  1392. data->temp_max_hyst =
  1393. w83781d_read_value(data, W83781D_REG_TEMP_HYST(1));
  1394. data->temp_add[0] =
  1395. w83781d_read_value(data, W83781D_REG_TEMP(2));
  1396. data->temp_max_add[0] =
  1397. w83781d_read_value(data, W83781D_REG_TEMP_OVER(2));
  1398. data->temp_max_hyst_add[0] =
  1399. w83781d_read_value(data, W83781D_REG_TEMP_HYST(2));
  1400. if (data->type != w83783s) {
  1401. data->temp_add[1] =
  1402. w83781d_read_value(data, W83781D_REG_TEMP(3));
  1403. data->temp_max_add[1] =
  1404. w83781d_read_value(data,
  1405. W83781D_REG_TEMP_OVER(3));
  1406. data->temp_max_hyst_add[1] =
  1407. w83781d_read_value(data,
  1408. W83781D_REG_TEMP_HYST(3));
  1409. }
  1410. i = w83781d_read_value(data, W83781D_REG_VID_FANDIV);
  1411. data->vid = i & 0x0f;
  1412. data->vid |= (w83781d_read_value(data,
  1413. W83781D_REG_CHIPID) & 0x01) << 4;
  1414. data->fan_div[0] = (i >> 4) & 0x03;
  1415. data->fan_div[1] = (i >> 6) & 0x03;
  1416. data->fan_div[2] = (w83781d_read_value(data,
  1417. W83781D_REG_PIN) >> 6) & 0x03;
  1418. if ((data->type != w83781d) && (data->type != as99127f)) {
  1419. i = w83781d_read_value(data, W83781D_REG_VBAT);
  1420. data->fan_div[0] |= (i >> 3) & 0x04;
  1421. data->fan_div[1] |= (i >> 4) & 0x04;
  1422. data->fan_div[2] |= (i >> 5) & 0x04;
  1423. }
  1424. if ((data->type == w83782d) || (data->type == w83627hf)) {
  1425. data->alarms = w83781d_read_value(data,
  1426. W83782D_REG_ALARM1)
  1427. | (w83781d_read_value(data,
  1428. W83782D_REG_ALARM2) << 8)
  1429. | (w83781d_read_value(data,
  1430. W83782D_REG_ALARM3) << 16);
  1431. } else if (data->type == w83783s) {
  1432. data->alarms = w83781d_read_value(data,
  1433. W83782D_REG_ALARM1)
  1434. | (w83781d_read_value(data,
  1435. W83782D_REG_ALARM2) << 8);
  1436. } else {
  1437. /* No real-time status registers, fall back to
  1438. interrupt status registers */
  1439. data->alarms = w83781d_read_value(data,
  1440. W83781D_REG_ALARM1)
  1441. | (w83781d_read_value(data,
  1442. W83781D_REG_ALARM2) << 8);
  1443. }
  1444. i = w83781d_read_value(data, W83781D_REG_BEEP_INTS2);
  1445. data->beep_enable = i >> 7;
  1446. data->beep_mask = ((i & 0x7f) << 8) +
  1447. w83781d_read_value(data, W83781D_REG_BEEP_INTS1);
  1448. if ((data->type != w83781d) && (data->type != as99127f)) {
  1449. data->beep_mask |=
  1450. w83781d_read_value(data,
  1451. W83781D_REG_BEEP_INTS3) << 16;
  1452. }
  1453. data->last_updated = jiffies;
  1454. data->valid = 1;
  1455. }
  1456. mutex_unlock(&data->update_lock);
  1457. return data;
  1458. }
  1459. /* return 1 if a supported chip is found, 0 otherwise */
  1460. static int __init
  1461. w83781d_isa_found(unsigned short address)
  1462. {
  1463. int val, save, found = 0;
  1464. if (!request_region(address, W83781D_EXTENT, "w83781d"))
  1465. return 0;
  1466. #define REALLY_SLOW_IO
  1467. /* We need the timeouts for at least some W83781D-like
  1468. chips. But only if we read 'undefined' registers. */
  1469. val = inb_p(address + 1);
  1470. if (inb_p(address + 2) != val
  1471. || inb_p(address + 3) != val
  1472. || inb_p(address + 7) != val) {
  1473. pr_debug("w83781d: Detection failed at step 1\n");
  1474. goto release;
  1475. }
  1476. #undef REALLY_SLOW_IO
  1477. /* We should be able to change the 7 LSB of the address port. The
  1478. MSB (busy flag) should be clear initially, set after the write. */
  1479. save = inb_p(address + W83781D_ADDR_REG_OFFSET);
  1480. if (save & 0x80) {
  1481. pr_debug("w83781d: Detection failed at step 2\n");
  1482. goto release;
  1483. }
  1484. val = ~save & 0x7f;
  1485. outb_p(val, address + W83781D_ADDR_REG_OFFSET);
  1486. if (inb_p(address + W83781D_ADDR_REG_OFFSET) != (val | 0x80)) {
  1487. outb_p(save, address + W83781D_ADDR_REG_OFFSET);
  1488. pr_debug("w83781d: Detection failed at step 3\n");
  1489. goto release;
  1490. }
  1491. /* We found a device, now see if it could be a W83781D */
  1492. outb_p(W83781D_REG_CONFIG, address + W83781D_ADDR_REG_OFFSET);
  1493. val = inb_p(address + W83781D_DATA_REG_OFFSET);
  1494. if (val & 0x80) {
  1495. pr_debug("w83781d: Detection failed at step 4\n");
  1496. goto release;
  1497. }
  1498. outb_p(W83781D_REG_BANK, address + W83781D_ADDR_REG_OFFSET);
  1499. save = inb_p(address + W83781D_DATA_REG_OFFSET);
  1500. outb_p(W83781D_REG_CHIPMAN, address + W83781D_ADDR_REG_OFFSET);
  1501. val = inb_p(address + W83781D_DATA_REG_OFFSET);
  1502. if ((!(save & 0x80) && (val != 0xa3))
  1503. || ((save & 0x80) && (val != 0x5c))) {
  1504. pr_debug("w83781d: Detection failed at step 5\n");
  1505. goto release;
  1506. }
  1507. outb_p(W83781D_REG_I2C_ADDR, address + W83781D_ADDR_REG_OFFSET);
  1508. val = inb_p(address + W83781D_DATA_REG_OFFSET);
  1509. if (val < 0x03 || val > 0x77) { /* Not a valid I2C address */
  1510. pr_debug("w83781d: Detection failed at step 6\n");
  1511. goto release;
  1512. }
  1513. /* The busy flag should be clear again */
  1514. if (inb_p(address + W83781D_ADDR_REG_OFFSET) & 0x80) {
  1515. pr_debug("w83781d: Detection failed at step 7\n");
  1516. goto release;
  1517. }
  1518. /* Determine the chip type */
  1519. outb_p(W83781D_REG_BANK, address + W83781D_ADDR_REG_OFFSET);
  1520. save = inb_p(address + W83781D_DATA_REG_OFFSET);
  1521. outb_p(save & 0xf8, address + W83781D_DATA_REG_OFFSET);
  1522. outb_p(W83781D_REG_WCHIPID, address + W83781D_ADDR_REG_OFFSET);
  1523. val = inb_p(address + W83781D_DATA_REG_OFFSET);
  1524. if ((val & 0xfe) == 0x10 /* W83781D */
  1525. || val == 0x30 /* W83782D */
  1526. || val == 0x21) /* W83627HF */
  1527. found = 1;
  1528. if (found)
  1529. pr_info("w83781d: Found a %s chip at %#x\n",
  1530. val == 0x21 ? "W83627HF" :
  1531. val == 0x30 ? "W83782D" : "W83781D", (int)address);
  1532. release:
  1533. release_region(address, W83781D_EXTENT);
  1534. return found;
  1535. }
  1536. static int __init
  1537. w83781d_isa_device_add(unsigned short address)
  1538. {
  1539. struct resource res = {
  1540. .start = address,
  1541. .end = address + W83781D_EXTENT,
  1542. .name = "w83781d",
  1543. .flags = IORESOURCE_IO,
  1544. };
  1545. int err;
  1546. pdev = platform_device_alloc("w83781d", address);
  1547. if (!pdev) {
  1548. err = -ENOMEM;
  1549. printk(KERN_ERR "w83781d: Device allocation failed\n");
  1550. goto exit;
  1551. }
  1552. err = platform_device_add_resources(pdev, &res, 1);
  1553. if (err) {
  1554. printk(KERN_ERR "w83781d: Device resource addition failed "
  1555. "(%d)\n", err);
  1556. goto exit_device_put;
  1557. }
  1558. err = platform_device_add(pdev);
  1559. if (err) {
  1560. printk(KERN_ERR "w83781d: Device addition failed (%d)\n",
  1561. err);
  1562. goto exit_device_put;
  1563. }
  1564. return 0;
  1565. exit_device_put:
  1566. platform_device_put(pdev);
  1567. exit:
  1568. pdev = NULL;
  1569. return err;
  1570. }
  1571. static int __init
  1572. sensors_w83781d_init(void)
  1573. {
  1574. int res;
  1575. res = i2c_add_driver(&w83781d_driver);
  1576. if (res)
  1577. goto exit;
  1578. if (w83781d_isa_found(isa_address)) {
  1579. res = platform_driver_register(&w83781d_isa_driver);
  1580. if (res)
  1581. goto exit_unreg_i2c_driver;
  1582. /* Sets global pdev as a side effect */
  1583. res = w83781d_isa_device_add(isa_address);
  1584. if (res)
  1585. goto exit_unreg_isa_driver;
  1586. }
  1587. return 0;
  1588. exit_unreg_isa_driver:
  1589. platform_driver_unregister(&w83781d_isa_driver);
  1590. exit_unreg_i2c_driver:
  1591. i2c_del_driver(&w83781d_driver);
  1592. exit:
  1593. return res;
  1594. }
  1595. static void __exit
  1596. sensors_w83781d_exit(void)
  1597. {
  1598. if (pdev) {
  1599. platform_device_unregister(pdev);
  1600. platform_driver_unregister(&w83781d_isa_driver);
  1601. }
  1602. i2c_del_driver(&w83781d_driver);
  1603. }
  1604. MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
  1605. "Philip Edelbrock <phil@netroedge.com>, "
  1606. "and Mark Studebaker <mdsxyz123@yahoo.com>");
  1607. MODULE_DESCRIPTION("W83781D driver");
  1608. MODULE_LICENSE("GPL");
  1609. module_init(sensors_w83781d_init);
  1610. module_exit(sensors_w83781d_exit);