abituguru.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511
  1. /*
  2. abituguru.c Copyright (c) 2005-2006 Hans de Goede <hdegoede@redhat.com>
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. */
  15. /*
  16. This driver supports the sensor part of the first and second revision of
  17. the custom Abit uGuru chip found on Abit uGuru motherboards. Note: because
  18. of lack of specs the CPU/RAM voltage & frequency control is not supported!
  19. */
  20. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  21. #include <linux/module.h>
  22. #include <linux/sched.h>
  23. #include <linux/init.h>
  24. #include <linux/slab.h>
  25. #include <linux/jiffies.h>
  26. #include <linux/mutex.h>
  27. #include <linux/err.h>
  28. #include <linux/delay.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/hwmon.h>
  31. #include <linux/hwmon-sysfs.h>
  32. #include <linux/dmi.h>
  33. #include <linux/io.h>
  34. /* Banks */
  35. #define ABIT_UGURU_ALARM_BANK 0x20 /* 1x 3 bytes */
  36. #define ABIT_UGURU_SENSOR_BANK1 0x21 /* 16x volt and temp */
  37. #define ABIT_UGURU_FAN_PWM 0x24 /* 3x 5 bytes */
  38. #define ABIT_UGURU_SENSOR_BANK2 0x26 /* fans */
  39. /* max nr of sensors in bank1, a bank1 sensor can be in, temp or nc */
  40. #define ABIT_UGURU_MAX_BANK1_SENSORS 16
  41. /* Warning if you increase one of the 2 MAX defines below to 10 or higher you
  42. should adjust the belonging _NAMES_LENGTH macro for the 2 digit number! */
  43. /* max nr of sensors in bank2, currently mb's with max 6 fans are known */
  44. #define ABIT_UGURU_MAX_BANK2_SENSORS 6
  45. /* max nr of pwm outputs, currently mb's with max 5 pwm outputs are known */
  46. #define ABIT_UGURU_MAX_PWMS 5
  47. /* uGuru sensor bank 1 flags */ /* Alarm if: */
  48. #define ABIT_UGURU_TEMP_HIGH_ALARM_ENABLE 0x01 /* temp over warn */
  49. #define ABIT_UGURU_VOLT_HIGH_ALARM_ENABLE 0x02 /* volt over max */
  50. #define ABIT_UGURU_VOLT_LOW_ALARM_ENABLE 0x04 /* volt under min */
  51. #define ABIT_UGURU_TEMP_HIGH_ALARM_FLAG 0x10 /* temp is over warn */
  52. #define ABIT_UGURU_VOLT_HIGH_ALARM_FLAG 0x20 /* volt is over max */
  53. #define ABIT_UGURU_VOLT_LOW_ALARM_FLAG 0x40 /* volt is under min */
  54. /* uGuru sensor bank 2 flags */ /* Alarm if: */
  55. #define ABIT_UGURU_FAN_LOW_ALARM_ENABLE 0x01 /* fan under min */
  56. /* uGuru sensor bank common flags */
  57. #define ABIT_UGURU_BEEP_ENABLE 0x08 /* beep if alarm */
  58. #define ABIT_UGURU_SHUTDOWN_ENABLE 0x80 /* shutdown if alarm */
  59. /* uGuru fan PWM (speed control) flags */
  60. #define ABIT_UGURU_FAN_PWM_ENABLE 0x80 /* enable speed control */
  61. /* Values used for conversion */
  62. #define ABIT_UGURU_FAN_MAX 15300 /* RPM */
  63. /* Bank1 sensor types */
  64. #define ABIT_UGURU_IN_SENSOR 0
  65. #define ABIT_UGURU_TEMP_SENSOR 1
  66. #define ABIT_UGURU_NC 2
  67. /* In many cases we need to wait for the uGuru to reach a certain status, most
  68. of the time it will reach this status within 30 - 90 ISA reads, and thus we
  69. can best busy wait. This define gives the total amount of reads to try. */
  70. #define ABIT_UGURU_WAIT_TIMEOUT 125
  71. /* However sometimes older versions of the uGuru seem to be distracted and they
  72. do not respond for a long time. To handle this we sleep before each of the
  73. last ABIT_UGURU_WAIT_TIMEOUT_SLEEP tries. */
  74. #define ABIT_UGURU_WAIT_TIMEOUT_SLEEP 5
  75. /* Normally all expected status in abituguru_ready, are reported after the
  76. first read, but sometimes not and we need to poll. */
  77. #define ABIT_UGURU_READY_TIMEOUT 5
  78. /* Maximum 3 retries on timedout reads/writes, delay 200 ms before retrying */
  79. #define ABIT_UGURU_MAX_RETRIES 3
  80. #define ABIT_UGURU_RETRY_DELAY (HZ/5)
  81. /* Maximum 2 timeouts in abituguru_update_device, iow 3 in a row is an error */
  82. #define ABIT_UGURU_MAX_TIMEOUTS 2
  83. /* utility macros */
  84. #define ABIT_UGURU_NAME "abituguru"
  85. #define ABIT_UGURU_DEBUG(level, format, arg...) \
  86. if (level <= verbose) \
  87. printk(KERN_DEBUG ABIT_UGURU_NAME ": " format , ## arg)
  88. /* Macros to help calculate the sysfs_names array length */
  89. /* sum of strlen of: in??_input\0, in??_{min,max}\0, in??_{min,max}_alarm\0,
  90. in??_{min,max}_alarm_enable\0, in??_beep\0, in??_shutdown\0 */
  91. #define ABITUGURU_IN_NAMES_LENGTH (11 + 2 * 9 + 2 * 15 + 2 * 22 + 10 + 14)
  92. /* sum of strlen of: temp??_input\0, temp??_max\0, temp??_crit\0,
  93. temp??_alarm\0, temp??_alarm_enable\0, temp??_beep\0, temp??_shutdown\0 */
  94. #define ABITUGURU_TEMP_NAMES_LENGTH (13 + 11 + 12 + 13 + 20 + 12 + 16)
  95. /* sum of strlen of: fan?_input\0, fan?_min\0, fan?_alarm\0,
  96. fan?_alarm_enable\0, fan?_beep\0, fan?_shutdown\0 */
  97. #define ABITUGURU_FAN_NAMES_LENGTH (11 + 9 + 11 + 18 + 10 + 14)
  98. /* sum of strlen of: pwm?_enable\0, pwm?_auto_channels_temp\0,
  99. pwm?_auto_point{1,2}_pwm\0, pwm?_auto_point{1,2}_temp\0 */
  100. #define ABITUGURU_PWM_NAMES_LENGTH (12 + 24 + 2 * 21 + 2 * 22)
  101. /* IN_NAMES_LENGTH > TEMP_NAMES_LENGTH so assume all bank1 sensors are in */
  102. #define ABITUGURU_SYSFS_NAMES_LENGTH ( \
  103. ABIT_UGURU_MAX_BANK1_SENSORS * ABITUGURU_IN_NAMES_LENGTH + \
  104. ABIT_UGURU_MAX_BANK2_SENSORS * ABITUGURU_FAN_NAMES_LENGTH + \
  105. ABIT_UGURU_MAX_PWMS * ABITUGURU_PWM_NAMES_LENGTH)
  106. /* All the macros below are named identical to the oguru and oguru2 programs
  107. reverse engineered by Olle Sandberg, hence the names might not be 100%
  108. logical. I could come up with better names, but I prefer keeping the names
  109. identical so that this driver can be compared with his work more easily. */
  110. /* Two i/o-ports are used by uGuru */
  111. #define ABIT_UGURU_BASE 0x00E0
  112. /* Used to tell uGuru what to read and to read the actual data */
  113. #define ABIT_UGURU_CMD 0x00
  114. /* Mostly used to check if uGuru is busy */
  115. #define ABIT_UGURU_DATA 0x04
  116. #define ABIT_UGURU_REGION_LENGTH 5
  117. /* uGuru status' */
  118. #define ABIT_UGURU_STATUS_WRITE 0x00 /* Ready to be written */
  119. #define ABIT_UGURU_STATUS_READ 0x01 /* Ready to be read */
  120. #define ABIT_UGURU_STATUS_INPUT 0x08 /* More input */
  121. #define ABIT_UGURU_STATUS_READY 0x09 /* Ready to be written */
  122. /* Constants */
  123. /* in (Volt) sensors go up to 3494 mV, temp to 255000 millidegrees Celsius */
  124. static const int abituguru_bank1_max_value[2] = { 3494, 255000 };
  125. /* Min / Max allowed values for sensor2 (fan) alarm threshold, these values
  126. correspond to 300-3000 RPM */
  127. static const u8 abituguru_bank2_min_threshold = 5;
  128. static const u8 abituguru_bank2_max_threshold = 50;
  129. /* Register 0 is a bitfield, 1 and 2 are pwm settings (255 = 100%), 3 and 4
  130. are temperature trip points. */
  131. static const int abituguru_pwm_settings_multiplier[5] = { 0, 1, 1, 1000, 1000 };
  132. /* Min / Max allowed values for pwm_settings. Note: pwm1 (CPU fan) is a
  133. special case the minium allowed pwm% setting for this is 30% (77) on
  134. some MB's this special case is handled in the code! */
  135. static const u8 abituguru_pwm_min[5] = { 0, 170, 170, 25, 25 };
  136. static const u8 abituguru_pwm_max[5] = { 0, 255, 255, 75, 75 };
  137. /* Insmod parameters */
  138. static int force;
  139. module_param(force, bool, 0);
  140. MODULE_PARM_DESC(force, "Set to one to force detection.");
  141. static int bank1_types[ABIT_UGURU_MAX_BANK1_SENSORS] = { -1, -1, -1, -1, -1,
  142. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 };
  143. module_param_array(bank1_types, int, NULL, 0);
  144. MODULE_PARM_DESC(bank1_types, "Bank1 sensortype autodetection override:\n"
  145. " -1 autodetect\n"
  146. " 0 volt sensor\n"
  147. " 1 temp sensor\n"
  148. " 2 not connected");
  149. static int fan_sensors;
  150. module_param(fan_sensors, int, 0);
  151. MODULE_PARM_DESC(fan_sensors, "Number of fan sensors on the uGuru "
  152. "(0 = autodetect)");
  153. static int pwms;
  154. module_param(pwms, int, 0);
  155. MODULE_PARM_DESC(pwms, "Number of PWMs on the uGuru "
  156. "(0 = autodetect)");
  157. /* Default verbose is 2, since this driver is still in the testing phase */
  158. static int verbose = 2;
  159. module_param(verbose, int, 0644);
  160. MODULE_PARM_DESC(verbose, "How verbose should the driver be? (0-3):\n"
  161. " 0 normal output\n"
  162. " 1 + verbose error reporting\n"
  163. " 2 + sensors type probing info\n"
  164. " 3 + retryable error reporting");
  165. /* For the Abit uGuru, we need to keep some data in memory.
  166. The structure is dynamically allocated, at the same time when a new
  167. abituguru device is allocated. */
  168. struct abituguru_data {
  169. struct device *hwmon_dev; /* hwmon registered device */
  170. struct mutex update_lock; /* protect access to data and uGuru */
  171. unsigned long last_updated; /* In jiffies */
  172. unsigned short addr; /* uguru base address */
  173. char uguru_ready; /* is the uguru in ready state? */
  174. unsigned char update_timeouts; /* number of update timeouts since last
  175. successful update */
  176. /* The sysfs attr and their names are generated automatically, for bank1
  177. we cannot use a predefined array because we don't know beforehand
  178. of a sensor is a volt or a temp sensor, for bank2 and the pwms its
  179. easier todo things the same way. For in sensors we have 9 (temp 7)
  180. sysfs entries per sensor, for bank2 and pwms 6. */
  181. struct sensor_device_attribute_2 sysfs_attr[
  182. ABIT_UGURU_MAX_BANK1_SENSORS * 9 +
  183. ABIT_UGURU_MAX_BANK2_SENSORS * 6 + ABIT_UGURU_MAX_PWMS * 6];
  184. /* Buffer to store the dynamically generated sysfs names */
  185. char sysfs_names[ABITUGURU_SYSFS_NAMES_LENGTH];
  186. /* Bank 1 data */
  187. /* number of and addresses of [0] in, [1] temp sensors */
  188. u8 bank1_sensors[2];
  189. u8 bank1_address[2][ABIT_UGURU_MAX_BANK1_SENSORS];
  190. u8 bank1_value[ABIT_UGURU_MAX_BANK1_SENSORS];
  191. /* This array holds 3 entries per sensor for the bank 1 sensor settings
  192. (flags, min, max for voltage / flags, warn, shutdown for temp). */
  193. u8 bank1_settings[ABIT_UGURU_MAX_BANK1_SENSORS][3];
  194. /* Maximum value for each sensor used for scaling in mV/millidegrees
  195. Celsius. */
  196. int bank1_max_value[ABIT_UGURU_MAX_BANK1_SENSORS];
  197. /* Bank 2 data, ABIT_UGURU_MAX_BANK2_SENSORS entries for bank2 */
  198. u8 bank2_sensors; /* actual number of bank2 sensors found */
  199. u8 bank2_value[ABIT_UGURU_MAX_BANK2_SENSORS];
  200. u8 bank2_settings[ABIT_UGURU_MAX_BANK2_SENSORS][2]; /* flags, min */
  201. /* Alarms 2 bytes for bank1, 1 byte for bank2 */
  202. u8 alarms[3];
  203. /* Fan PWM (speed control) 5 bytes per PWM */
  204. u8 pwms; /* actual number of pwms found */
  205. u8 pwm_settings[ABIT_UGURU_MAX_PWMS][5];
  206. };
  207. static const char *never_happen = "This should never happen.";
  208. static const char *report_this =
  209. "Please report this to the abituguru maintainer (see MAINTAINERS)";
  210. /* wait till the uguru is in the specified state */
  211. static int abituguru_wait(struct abituguru_data *data, u8 state)
  212. {
  213. int timeout = ABIT_UGURU_WAIT_TIMEOUT;
  214. while (inb_p(data->addr + ABIT_UGURU_DATA) != state) {
  215. timeout--;
  216. if (timeout == 0)
  217. return -EBUSY;
  218. /* sleep a bit before our last few tries, see the comment on
  219. this where ABIT_UGURU_WAIT_TIMEOUT_SLEEP is defined. */
  220. if (timeout <= ABIT_UGURU_WAIT_TIMEOUT_SLEEP)
  221. msleep(0);
  222. }
  223. return 0;
  224. }
  225. /* Put the uguru in ready for input state */
  226. static int abituguru_ready(struct abituguru_data *data)
  227. {
  228. int timeout = ABIT_UGURU_READY_TIMEOUT;
  229. if (data->uguru_ready)
  230. return 0;
  231. /* Reset? / Prepare for next read/write cycle */
  232. outb(0x00, data->addr + ABIT_UGURU_DATA);
  233. /* Wait till the uguru is ready */
  234. if (abituguru_wait(data, ABIT_UGURU_STATUS_READY)) {
  235. ABIT_UGURU_DEBUG(1,
  236. "timeout exceeded waiting for ready state\n");
  237. return -EIO;
  238. }
  239. /* Cmd port MUST be read now and should contain 0xAC */
  240. while (inb_p(data->addr + ABIT_UGURU_CMD) != 0xAC) {
  241. timeout--;
  242. if (timeout == 0) {
  243. ABIT_UGURU_DEBUG(1,
  244. "CMD reg does not hold 0xAC after ready command\n");
  245. return -EIO;
  246. }
  247. msleep(0);
  248. }
  249. /* After this the ABIT_UGURU_DATA port should contain
  250. ABIT_UGURU_STATUS_INPUT */
  251. timeout = ABIT_UGURU_READY_TIMEOUT;
  252. while (inb_p(data->addr + ABIT_UGURU_DATA) != ABIT_UGURU_STATUS_INPUT) {
  253. timeout--;
  254. if (timeout == 0) {
  255. ABIT_UGURU_DEBUG(1,
  256. "state != more input after ready command\n");
  257. return -EIO;
  258. }
  259. msleep(0);
  260. }
  261. data->uguru_ready = 1;
  262. return 0;
  263. }
  264. /* Send the bank and then sensor address to the uGuru for the next read/write
  265. cycle. This function gets called as the first part of a read/write by
  266. abituguru_read and abituguru_write. This function should never be
  267. called by any other function. */
  268. static int abituguru_send_address(struct abituguru_data *data,
  269. u8 bank_addr, u8 sensor_addr, int retries)
  270. {
  271. /* assume the caller does error handling itself if it has not requested
  272. any retries, and thus be quiet. */
  273. int report_errors = retries;
  274. for (;;) {
  275. /* Make sure the uguru is ready and then send the bank address,
  276. after this the uguru is no longer "ready". */
  277. if (abituguru_ready(data) != 0)
  278. return -EIO;
  279. outb(bank_addr, data->addr + ABIT_UGURU_DATA);
  280. data->uguru_ready = 0;
  281. /* Wait till the uguru is ABIT_UGURU_STATUS_INPUT state again
  282. and send the sensor addr */
  283. if (abituguru_wait(data, ABIT_UGURU_STATUS_INPUT)) {
  284. if (retries) {
  285. ABIT_UGURU_DEBUG(3, "timeout exceeded "
  286. "waiting for more input state, %d "
  287. "tries remaining\n", retries);
  288. set_current_state(TASK_UNINTERRUPTIBLE);
  289. schedule_timeout(ABIT_UGURU_RETRY_DELAY);
  290. retries--;
  291. continue;
  292. }
  293. if (report_errors)
  294. ABIT_UGURU_DEBUG(1, "timeout exceeded "
  295. "waiting for more input state "
  296. "(bank: %d)\n", (int)bank_addr);
  297. return -EBUSY;
  298. }
  299. outb(sensor_addr, data->addr + ABIT_UGURU_CMD);
  300. return 0;
  301. }
  302. }
  303. /* Read count bytes from sensor sensor_addr in bank bank_addr and store the
  304. result in buf, retry the send address part of the read retries times. */
  305. static int abituguru_read(struct abituguru_data *data,
  306. u8 bank_addr, u8 sensor_addr, u8 *buf, int count, int retries)
  307. {
  308. int i;
  309. /* Send the address */
  310. i = abituguru_send_address(data, bank_addr, sensor_addr, retries);
  311. if (i)
  312. return i;
  313. /* And read the data */
  314. for (i = 0; i < count; i++) {
  315. if (abituguru_wait(data, ABIT_UGURU_STATUS_READ)) {
  316. ABIT_UGURU_DEBUG(retries ? 1 : 3,
  317. "timeout exceeded waiting for "
  318. "read state (bank: %d, sensor: %d)\n",
  319. (int)bank_addr, (int)sensor_addr);
  320. break;
  321. }
  322. buf[i] = inb(data->addr + ABIT_UGURU_CMD);
  323. }
  324. /* Last put the chip back in ready state */
  325. abituguru_ready(data);
  326. return i;
  327. }
  328. /* Write count bytes from buf to sensor sensor_addr in bank bank_addr, the send
  329. address part of the write is always retried ABIT_UGURU_MAX_RETRIES times. */
  330. static int abituguru_write(struct abituguru_data *data,
  331. u8 bank_addr, u8 sensor_addr, u8 *buf, int count)
  332. {
  333. /* We use the ready timeout as we have to wait for 0xAC just like the
  334. ready function */
  335. int i, timeout = ABIT_UGURU_READY_TIMEOUT;
  336. /* Send the address */
  337. i = abituguru_send_address(data, bank_addr, sensor_addr,
  338. ABIT_UGURU_MAX_RETRIES);
  339. if (i)
  340. return i;
  341. /* And write the data */
  342. for (i = 0; i < count; i++) {
  343. if (abituguru_wait(data, ABIT_UGURU_STATUS_WRITE)) {
  344. ABIT_UGURU_DEBUG(1, "timeout exceeded waiting for "
  345. "write state (bank: %d, sensor: %d)\n",
  346. (int)bank_addr, (int)sensor_addr);
  347. break;
  348. }
  349. outb(buf[i], data->addr + ABIT_UGURU_CMD);
  350. }
  351. /* Now we need to wait till the chip is ready to be read again,
  352. so that we can read 0xAC as confirmation that our write has
  353. succeeded. */
  354. if (abituguru_wait(data, ABIT_UGURU_STATUS_READ)) {
  355. ABIT_UGURU_DEBUG(1, "timeout exceeded waiting for read state "
  356. "after write (bank: %d, sensor: %d)\n", (int)bank_addr,
  357. (int)sensor_addr);
  358. return -EIO;
  359. }
  360. /* Cmd port MUST be read now and should contain 0xAC */
  361. while (inb_p(data->addr + ABIT_UGURU_CMD) != 0xAC) {
  362. timeout--;
  363. if (timeout == 0) {
  364. ABIT_UGURU_DEBUG(1, "CMD reg does not hold 0xAC after "
  365. "write (bank: %d, sensor: %d)\n",
  366. (int)bank_addr, (int)sensor_addr);
  367. return -EIO;
  368. }
  369. msleep(0);
  370. }
  371. /* Last put the chip back in ready state */
  372. abituguru_ready(data);
  373. return i;
  374. }
  375. /* Detect sensor type. Temp and Volt sensors are enabled with
  376. different masks and will ignore enable masks not meant for them.
  377. This enables us to test what kind of sensor we're dealing with.
  378. By setting the alarm thresholds so that we will always get an
  379. alarm for sensor type X and then enabling the sensor as sensor type
  380. X, if we then get an alarm it is a sensor of type X. */
  381. static int __devinit
  382. abituguru_detect_bank1_sensor_type(struct abituguru_data *data,
  383. u8 sensor_addr)
  384. {
  385. u8 val, test_flag, buf[3];
  386. int i, ret = -ENODEV; /* error is the most common used retval :| */
  387. /* If overriden by the user return the user selected type */
  388. if (bank1_types[sensor_addr] >= ABIT_UGURU_IN_SENSOR &&
  389. bank1_types[sensor_addr] <= ABIT_UGURU_NC) {
  390. ABIT_UGURU_DEBUG(2, "assuming sensor type %d for bank1 sensor "
  391. "%d because of \"bank1_types\" module param\n",
  392. bank1_types[sensor_addr], (int)sensor_addr);
  393. return bank1_types[sensor_addr];
  394. }
  395. /* First read the sensor and the current settings */
  396. if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1, sensor_addr, &val,
  397. 1, ABIT_UGURU_MAX_RETRIES) != 1)
  398. return -ENODEV;
  399. /* Test val is sane / usable for sensor type detection. */
  400. if ((val < 10u) || (val > 250u)) {
  401. pr_warn("bank1-sensor: %d reading (%d) too close to limits, "
  402. "unable to determine sensor type, skipping sensor\n",
  403. (int)sensor_addr, (int)val);
  404. /* assume no sensor is there for sensors for which we can't
  405. determine the sensor type because their reading is too close
  406. to their limits, this usually means no sensor is there. */
  407. return ABIT_UGURU_NC;
  408. }
  409. ABIT_UGURU_DEBUG(2, "testing bank1 sensor %d\n", (int)sensor_addr);
  410. /* Volt sensor test, enable volt low alarm, set min value ridicously
  411. high, or vica versa if the reading is very high. If its a volt
  412. sensor this should always give us an alarm. */
  413. if (val <= 240u) {
  414. buf[0] = ABIT_UGURU_VOLT_LOW_ALARM_ENABLE;
  415. buf[1] = 245;
  416. buf[2] = 250;
  417. test_flag = ABIT_UGURU_VOLT_LOW_ALARM_FLAG;
  418. } else {
  419. buf[0] = ABIT_UGURU_VOLT_HIGH_ALARM_ENABLE;
  420. buf[1] = 5;
  421. buf[2] = 10;
  422. test_flag = ABIT_UGURU_VOLT_HIGH_ALARM_FLAG;
  423. }
  424. if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2, sensor_addr,
  425. buf, 3) != 3)
  426. goto abituguru_detect_bank1_sensor_type_exit;
  427. /* Now we need 20 ms to give the uguru time to read the sensors
  428. and raise a voltage alarm */
  429. set_current_state(TASK_UNINTERRUPTIBLE);
  430. schedule_timeout(HZ/50);
  431. /* Check for alarm and check the alarm is a volt low alarm. */
  432. if (abituguru_read(data, ABIT_UGURU_ALARM_BANK, 0, buf, 3,
  433. ABIT_UGURU_MAX_RETRIES) != 3)
  434. goto abituguru_detect_bank1_sensor_type_exit;
  435. if (buf[sensor_addr/8] & (0x01 << (sensor_addr % 8))) {
  436. if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1 + 1,
  437. sensor_addr, buf, 3,
  438. ABIT_UGURU_MAX_RETRIES) != 3)
  439. goto abituguru_detect_bank1_sensor_type_exit;
  440. if (buf[0] & test_flag) {
  441. ABIT_UGURU_DEBUG(2, " found volt sensor\n");
  442. ret = ABIT_UGURU_IN_SENSOR;
  443. goto abituguru_detect_bank1_sensor_type_exit;
  444. } else
  445. ABIT_UGURU_DEBUG(2, " alarm raised during volt "
  446. "sensor test, but volt range flag not set\n");
  447. } else
  448. ABIT_UGURU_DEBUG(2, " alarm not raised during volt sensor "
  449. "test\n");
  450. /* Temp sensor test, enable sensor as a temp sensor, set beep value
  451. ridicously low (but not too low, otherwise uguru ignores it).
  452. If its a temp sensor this should always give us an alarm. */
  453. buf[0] = ABIT_UGURU_TEMP_HIGH_ALARM_ENABLE;
  454. buf[1] = 5;
  455. buf[2] = 10;
  456. if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2, sensor_addr,
  457. buf, 3) != 3)
  458. goto abituguru_detect_bank1_sensor_type_exit;
  459. /* Now we need 50 ms to give the uguru time to read the sensors
  460. and raise a temp alarm */
  461. set_current_state(TASK_UNINTERRUPTIBLE);
  462. schedule_timeout(HZ/20);
  463. /* Check for alarm and check the alarm is a temp high alarm. */
  464. if (abituguru_read(data, ABIT_UGURU_ALARM_BANK, 0, buf, 3,
  465. ABIT_UGURU_MAX_RETRIES) != 3)
  466. goto abituguru_detect_bank1_sensor_type_exit;
  467. if (buf[sensor_addr/8] & (0x01 << (sensor_addr % 8))) {
  468. if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1 + 1,
  469. sensor_addr, buf, 3,
  470. ABIT_UGURU_MAX_RETRIES) != 3)
  471. goto abituguru_detect_bank1_sensor_type_exit;
  472. if (buf[0] & ABIT_UGURU_TEMP_HIGH_ALARM_FLAG) {
  473. ABIT_UGURU_DEBUG(2, " found temp sensor\n");
  474. ret = ABIT_UGURU_TEMP_SENSOR;
  475. goto abituguru_detect_bank1_sensor_type_exit;
  476. } else
  477. ABIT_UGURU_DEBUG(2, " alarm raised during temp "
  478. "sensor test, but temp high flag not set\n");
  479. } else
  480. ABIT_UGURU_DEBUG(2, " alarm not raised during temp sensor "
  481. "test\n");
  482. ret = ABIT_UGURU_NC;
  483. abituguru_detect_bank1_sensor_type_exit:
  484. /* Restore original settings, failing here is really BAD, it has been
  485. reported that some BIOS-es hang when entering the uGuru menu with
  486. invalid settings present in the uGuru, so we try this 3 times. */
  487. for (i = 0; i < 3; i++)
  488. if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2,
  489. sensor_addr, data->bank1_settings[sensor_addr],
  490. 3) == 3)
  491. break;
  492. if (i == 3) {
  493. pr_err("Fatal error could not restore original settings. %s %s\n",
  494. never_happen, report_this);
  495. return -ENODEV;
  496. }
  497. return ret;
  498. }
  499. /* These functions try to find out how many sensors there are in bank2 and how
  500. many pwms there are. The purpose of this is to make sure that we don't give
  501. the user the possibility to change settings for non-existent sensors / pwm.
  502. The uGuru will happily read / write whatever memory happens to be after the
  503. memory storing the PWM settings when reading/writing to a PWM which is not
  504. there. Notice even if we detect a PWM which doesn't exist we normally won't
  505. write to it, unless the user tries to change the settings.
  506. Although the uGuru allows reading (settings) from non existing bank2
  507. sensors, my version of the uGuru does seem to stop writing to them, the
  508. write function above aborts in this case with:
  509. "CMD reg does not hold 0xAC after write"
  510. Notice these 2 tests are non destructive iow read-only tests, otherwise
  511. they would defeat their purpose. Although for the bank2_sensors detection a
  512. read/write test would be feasible because of the reaction above, I've
  513. however opted to stay on the safe side. */
  514. static void __devinit
  515. abituguru_detect_no_bank2_sensors(struct abituguru_data *data)
  516. {
  517. int i;
  518. if (fan_sensors > 0 && fan_sensors <= ABIT_UGURU_MAX_BANK2_SENSORS) {
  519. data->bank2_sensors = fan_sensors;
  520. ABIT_UGURU_DEBUG(2, "assuming %d fan sensors because of "
  521. "\"fan_sensors\" module param\n",
  522. (int)data->bank2_sensors);
  523. return;
  524. }
  525. ABIT_UGURU_DEBUG(2, "detecting number of fan sensors\n");
  526. for (i = 0; i < ABIT_UGURU_MAX_BANK2_SENSORS; i++) {
  527. /* 0x89 are the known used bits:
  528. -0x80 enable shutdown
  529. -0x08 enable beep
  530. -0x01 enable alarm
  531. All other bits should be 0, but on some motherboards
  532. 0x40 (bit 6) is also high for some of the fans?? */
  533. if (data->bank2_settings[i][0] & ~0xC9) {
  534. ABIT_UGURU_DEBUG(2, " bank2 sensor %d does not seem "
  535. "to be a fan sensor: settings[0] = %02X\n",
  536. i, (unsigned int)data->bank2_settings[i][0]);
  537. break;
  538. }
  539. /* check if the threshold is within the allowed range */
  540. if (data->bank2_settings[i][1] <
  541. abituguru_bank2_min_threshold) {
  542. ABIT_UGURU_DEBUG(2, " bank2 sensor %d does not seem "
  543. "to be a fan sensor: the threshold (%d) is "
  544. "below the minimum (%d)\n", i,
  545. (int)data->bank2_settings[i][1],
  546. (int)abituguru_bank2_min_threshold);
  547. break;
  548. }
  549. if (data->bank2_settings[i][1] >
  550. abituguru_bank2_max_threshold) {
  551. ABIT_UGURU_DEBUG(2, " bank2 sensor %d does not seem "
  552. "to be a fan sensor: the threshold (%d) is "
  553. "above the maximum (%d)\n", i,
  554. (int)data->bank2_settings[i][1],
  555. (int)abituguru_bank2_max_threshold);
  556. break;
  557. }
  558. }
  559. data->bank2_sensors = i;
  560. ABIT_UGURU_DEBUG(2, " found: %d fan sensors\n",
  561. (int)data->bank2_sensors);
  562. }
  563. static void __devinit
  564. abituguru_detect_no_pwms(struct abituguru_data *data)
  565. {
  566. int i, j;
  567. if (pwms > 0 && pwms <= ABIT_UGURU_MAX_PWMS) {
  568. data->pwms = pwms;
  569. ABIT_UGURU_DEBUG(2, "assuming %d PWM outputs because of "
  570. "\"pwms\" module param\n", (int)data->pwms);
  571. return;
  572. }
  573. ABIT_UGURU_DEBUG(2, "detecting number of PWM outputs\n");
  574. for (i = 0; i < ABIT_UGURU_MAX_PWMS; i++) {
  575. /* 0x80 is the enable bit and the low
  576. nibble is which temp sensor to use,
  577. the other bits should be 0 */
  578. if (data->pwm_settings[i][0] & ~0x8F) {
  579. ABIT_UGURU_DEBUG(2, " pwm channel %d does not seem "
  580. "to be a pwm channel: settings[0] = %02X\n",
  581. i, (unsigned int)data->pwm_settings[i][0]);
  582. break;
  583. }
  584. /* the low nibble must correspond to one of the temp sensors
  585. we've found */
  586. for (j = 0; j < data->bank1_sensors[ABIT_UGURU_TEMP_SENSOR];
  587. j++) {
  588. if (data->bank1_address[ABIT_UGURU_TEMP_SENSOR][j] ==
  589. (data->pwm_settings[i][0] & 0x0F))
  590. break;
  591. }
  592. if (j == data->bank1_sensors[ABIT_UGURU_TEMP_SENSOR]) {
  593. ABIT_UGURU_DEBUG(2, " pwm channel %d does not seem "
  594. "to be a pwm channel: %d is not a valid temp "
  595. "sensor address\n", i,
  596. data->pwm_settings[i][0] & 0x0F);
  597. break;
  598. }
  599. /* check if all other settings are within the allowed range */
  600. for (j = 1; j < 5; j++) {
  601. u8 min;
  602. /* special case pwm1 min pwm% */
  603. if ((i == 0) && ((j == 1) || (j == 2)))
  604. min = 77;
  605. else
  606. min = abituguru_pwm_min[j];
  607. if (data->pwm_settings[i][j] < min) {
  608. ABIT_UGURU_DEBUG(2, " pwm channel %d does "
  609. "not seem to be a pwm channel: "
  610. "setting %d (%d) is below the minimum "
  611. "value (%d)\n", i, j,
  612. (int)data->pwm_settings[i][j],
  613. (int)min);
  614. goto abituguru_detect_no_pwms_exit;
  615. }
  616. if (data->pwm_settings[i][j] > abituguru_pwm_max[j]) {
  617. ABIT_UGURU_DEBUG(2, " pwm channel %d does "
  618. "not seem to be a pwm channel: "
  619. "setting %d (%d) is above the maximum "
  620. "value (%d)\n", i, j,
  621. (int)data->pwm_settings[i][j],
  622. (int)abituguru_pwm_max[j]);
  623. goto abituguru_detect_no_pwms_exit;
  624. }
  625. }
  626. /* check that min temp < max temp and min pwm < max pwm */
  627. if (data->pwm_settings[i][1] >= data->pwm_settings[i][2]) {
  628. ABIT_UGURU_DEBUG(2, " pwm channel %d does not seem "
  629. "to be a pwm channel: min pwm (%d) >= "
  630. "max pwm (%d)\n", i,
  631. (int)data->pwm_settings[i][1],
  632. (int)data->pwm_settings[i][2]);
  633. break;
  634. }
  635. if (data->pwm_settings[i][3] >= data->pwm_settings[i][4]) {
  636. ABIT_UGURU_DEBUG(2, " pwm channel %d does not seem "
  637. "to be a pwm channel: min temp (%d) >= "
  638. "max temp (%d)\n", i,
  639. (int)data->pwm_settings[i][3],
  640. (int)data->pwm_settings[i][4]);
  641. break;
  642. }
  643. }
  644. abituguru_detect_no_pwms_exit:
  645. data->pwms = i;
  646. ABIT_UGURU_DEBUG(2, " found: %d PWM outputs\n", (int)data->pwms);
  647. }
  648. /* Following are the sysfs callback functions. These functions expect:
  649. sensor_device_attribute_2->index: sensor address/offset in the bank
  650. sensor_device_attribute_2->nr: register offset, bitmask or NA. */
  651. static struct abituguru_data *abituguru_update_device(struct device *dev);
  652. static ssize_t show_bank1_value(struct device *dev,
  653. struct device_attribute *devattr, char *buf)
  654. {
  655. struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
  656. struct abituguru_data *data = abituguru_update_device(dev);
  657. if (!data)
  658. return -EIO;
  659. return sprintf(buf, "%d\n", (data->bank1_value[attr->index] *
  660. data->bank1_max_value[attr->index] + 128) / 255);
  661. }
  662. static ssize_t show_bank1_setting(struct device *dev,
  663. struct device_attribute *devattr, char *buf)
  664. {
  665. struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
  666. struct abituguru_data *data = dev_get_drvdata(dev);
  667. return sprintf(buf, "%d\n",
  668. (data->bank1_settings[attr->index][attr->nr] *
  669. data->bank1_max_value[attr->index] + 128) / 255);
  670. }
  671. static ssize_t show_bank2_value(struct device *dev,
  672. struct device_attribute *devattr, char *buf)
  673. {
  674. struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
  675. struct abituguru_data *data = abituguru_update_device(dev);
  676. if (!data)
  677. return -EIO;
  678. return sprintf(buf, "%d\n", (data->bank2_value[attr->index] *
  679. ABIT_UGURU_FAN_MAX + 128) / 255);
  680. }
  681. static ssize_t show_bank2_setting(struct device *dev,
  682. struct device_attribute *devattr, char *buf)
  683. {
  684. struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
  685. struct abituguru_data *data = dev_get_drvdata(dev);
  686. return sprintf(buf, "%d\n",
  687. (data->bank2_settings[attr->index][attr->nr] *
  688. ABIT_UGURU_FAN_MAX + 128) / 255);
  689. }
  690. static ssize_t store_bank1_setting(struct device *dev, struct device_attribute
  691. *devattr, const char *buf, size_t count)
  692. {
  693. struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
  694. struct abituguru_data *data = dev_get_drvdata(dev);
  695. u8 val = (simple_strtoul(buf, NULL, 10) * 255 +
  696. data->bank1_max_value[attr->index]/2) /
  697. data->bank1_max_value[attr->index];
  698. ssize_t ret = count;
  699. mutex_lock(&data->update_lock);
  700. if (data->bank1_settings[attr->index][attr->nr] != val) {
  701. u8 orig_val = data->bank1_settings[attr->index][attr->nr];
  702. data->bank1_settings[attr->index][attr->nr] = val;
  703. if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2,
  704. attr->index, data->bank1_settings[attr->index],
  705. 3) <= attr->nr) {
  706. data->bank1_settings[attr->index][attr->nr] = orig_val;
  707. ret = -EIO;
  708. }
  709. }
  710. mutex_unlock(&data->update_lock);
  711. return ret;
  712. }
  713. static ssize_t store_bank2_setting(struct device *dev, struct device_attribute
  714. *devattr, const char *buf, size_t count)
  715. {
  716. struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
  717. struct abituguru_data *data = dev_get_drvdata(dev);
  718. u8 val = (simple_strtoul(buf, NULL, 10)*255 + ABIT_UGURU_FAN_MAX/2) /
  719. ABIT_UGURU_FAN_MAX;
  720. ssize_t ret = count;
  721. /* this check can be done before taking the lock */
  722. if ((val < abituguru_bank2_min_threshold) ||
  723. (val > abituguru_bank2_max_threshold))
  724. return -EINVAL;
  725. mutex_lock(&data->update_lock);
  726. if (data->bank2_settings[attr->index][attr->nr] != val) {
  727. u8 orig_val = data->bank2_settings[attr->index][attr->nr];
  728. data->bank2_settings[attr->index][attr->nr] = val;
  729. if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK2 + 2,
  730. attr->index, data->bank2_settings[attr->index],
  731. 2) <= attr->nr) {
  732. data->bank2_settings[attr->index][attr->nr] = orig_val;
  733. ret = -EIO;
  734. }
  735. }
  736. mutex_unlock(&data->update_lock);
  737. return ret;
  738. }
  739. static ssize_t show_bank1_alarm(struct device *dev,
  740. struct device_attribute *devattr, char *buf)
  741. {
  742. struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
  743. struct abituguru_data *data = abituguru_update_device(dev);
  744. if (!data)
  745. return -EIO;
  746. /* See if the alarm bit for this sensor is set, and if the
  747. alarm matches the type of alarm we're looking for (for volt
  748. it can be either low or high). The type is stored in a few
  749. readonly bits in the settings part of the relevant sensor.
  750. The bitmask of the type is passed to us in attr->nr. */
  751. if ((data->alarms[attr->index / 8] & (0x01 << (attr->index % 8))) &&
  752. (data->bank1_settings[attr->index][0] & attr->nr))
  753. return sprintf(buf, "1\n");
  754. else
  755. return sprintf(buf, "0\n");
  756. }
  757. static ssize_t show_bank2_alarm(struct device *dev,
  758. struct device_attribute *devattr, char *buf)
  759. {
  760. struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
  761. struct abituguru_data *data = abituguru_update_device(dev);
  762. if (!data)
  763. return -EIO;
  764. if (data->alarms[2] & (0x01 << attr->index))
  765. return sprintf(buf, "1\n");
  766. else
  767. return sprintf(buf, "0\n");
  768. }
  769. static ssize_t show_bank1_mask(struct device *dev,
  770. struct device_attribute *devattr, char *buf)
  771. {
  772. struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
  773. struct abituguru_data *data = dev_get_drvdata(dev);
  774. if (data->bank1_settings[attr->index][0] & attr->nr)
  775. return sprintf(buf, "1\n");
  776. else
  777. return sprintf(buf, "0\n");
  778. }
  779. static ssize_t show_bank2_mask(struct device *dev,
  780. struct device_attribute *devattr, char *buf)
  781. {
  782. struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
  783. struct abituguru_data *data = dev_get_drvdata(dev);
  784. if (data->bank2_settings[attr->index][0] & attr->nr)
  785. return sprintf(buf, "1\n");
  786. else
  787. return sprintf(buf, "0\n");
  788. }
  789. static ssize_t store_bank1_mask(struct device *dev,
  790. struct device_attribute *devattr, const char *buf, size_t count)
  791. {
  792. struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
  793. struct abituguru_data *data = dev_get_drvdata(dev);
  794. int mask = simple_strtoul(buf, NULL, 10);
  795. ssize_t ret = count;
  796. u8 orig_val;
  797. mutex_lock(&data->update_lock);
  798. orig_val = data->bank1_settings[attr->index][0];
  799. if (mask)
  800. data->bank1_settings[attr->index][0] |= attr->nr;
  801. else
  802. data->bank1_settings[attr->index][0] &= ~attr->nr;
  803. if ((data->bank1_settings[attr->index][0] != orig_val) &&
  804. (abituguru_write(data,
  805. ABIT_UGURU_SENSOR_BANK1 + 2, attr->index,
  806. data->bank1_settings[attr->index], 3) < 1)) {
  807. data->bank1_settings[attr->index][0] = orig_val;
  808. ret = -EIO;
  809. }
  810. mutex_unlock(&data->update_lock);
  811. return ret;
  812. }
  813. static ssize_t store_bank2_mask(struct device *dev,
  814. struct device_attribute *devattr, const char *buf, size_t count)
  815. {
  816. struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
  817. struct abituguru_data *data = dev_get_drvdata(dev);
  818. int mask = simple_strtoul(buf, NULL, 10);
  819. ssize_t ret = count;
  820. u8 orig_val;
  821. mutex_lock(&data->update_lock);
  822. orig_val = data->bank2_settings[attr->index][0];
  823. if (mask)
  824. data->bank2_settings[attr->index][0] |= attr->nr;
  825. else
  826. data->bank2_settings[attr->index][0] &= ~attr->nr;
  827. if ((data->bank2_settings[attr->index][0] != orig_val) &&
  828. (abituguru_write(data,
  829. ABIT_UGURU_SENSOR_BANK2 + 2, attr->index,
  830. data->bank2_settings[attr->index], 2) < 1)) {
  831. data->bank2_settings[attr->index][0] = orig_val;
  832. ret = -EIO;
  833. }
  834. mutex_unlock(&data->update_lock);
  835. return ret;
  836. }
  837. /* Fan PWM (speed control) */
  838. static ssize_t show_pwm_setting(struct device *dev,
  839. struct device_attribute *devattr, char *buf)
  840. {
  841. struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
  842. struct abituguru_data *data = dev_get_drvdata(dev);
  843. return sprintf(buf, "%d\n", data->pwm_settings[attr->index][attr->nr] *
  844. abituguru_pwm_settings_multiplier[attr->nr]);
  845. }
  846. static ssize_t store_pwm_setting(struct device *dev, struct device_attribute
  847. *devattr, const char *buf, size_t count)
  848. {
  849. struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
  850. struct abituguru_data *data = dev_get_drvdata(dev);
  851. u8 min, val = (simple_strtoul(buf, NULL, 10) +
  852. abituguru_pwm_settings_multiplier[attr->nr]/2) /
  853. abituguru_pwm_settings_multiplier[attr->nr];
  854. ssize_t ret = count;
  855. /* special case pwm1 min pwm% */
  856. if ((attr->index == 0) && ((attr->nr == 1) || (attr->nr == 2)))
  857. min = 77;
  858. else
  859. min = abituguru_pwm_min[attr->nr];
  860. /* this check can be done before taking the lock */
  861. if ((val < min) || (val > abituguru_pwm_max[attr->nr]))
  862. return -EINVAL;
  863. mutex_lock(&data->update_lock);
  864. /* this check needs to be done after taking the lock */
  865. if ((attr->nr & 1) &&
  866. (val >= data->pwm_settings[attr->index][attr->nr + 1]))
  867. ret = -EINVAL;
  868. else if (!(attr->nr & 1) &&
  869. (val <= data->pwm_settings[attr->index][attr->nr - 1]))
  870. ret = -EINVAL;
  871. else if (data->pwm_settings[attr->index][attr->nr] != val) {
  872. u8 orig_val = data->pwm_settings[attr->index][attr->nr];
  873. data->pwm_settings[attr->index][attr->nr] = val;
  874. if (abituguru_write(data, ABIT_UGURU_FAN_PWM + 1,
  875. attr->index, data->pwm_settings[attr->index],
  876. 5) <= attr->nr) {
  877. data->pwm_settings[attr->index][attr->nr] =
  878. orig_val;
  879. ret = -EIO;
  880. }
  881. }
  882. mutex_unlock(&data->update_lock);
  883. return ret;
  884. }
  885. static ssize_t show_pwm_sensor(struct device *dev,
  886. struct device_attribute *devattr, char *buf)
  887. {
  888. struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
  889. struct abituguru_data *data = dev_get_drvdata(dev);
  890. int i;
  891. /* We need to walk to the temp sensor addresses to find what
  892. the userspace id of the configured temp sensor is. */
  893. for (i = 0; i < data->bank1_sensors[ABIT_UGURU_TEMP_SENSOR]; i++)
  894. if (data->bank1_address[ABIT_UGURU_TEMP_SENSOR][i] ==
  895. (data->pwm_settings[attr->index][0] & 0x0F))
  896. return sprintf(buf, "%d\n", i+1);
  897. return -ENXIO;
  898. }
  899. static ssize_t store_pwm_sensor(struct device *dev, struct device_attribute
  900. *devattr, const char *buf, size_t count)
  901. {
  902. struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
  903. struct abituguru_data *data = dev_get_drvdata(dev);
  904. unsigned long val = simple_strtoul(buf, NULL, 10) - 1;
  905. ssize_t ret = count;
  906. mutex_lock(&data->update_lock);
  907. if (val < data->bank1_sensors[ABIT_UGURU_TEMP_SENSOR]) {
  908. u8 orig_val = data->pwm_settings[attr->index][0];
  909. u8 address = data->bank1_address[ABIT_UGURU_TEMP_SENSOR][val];
  910. data->pwm_settings[attr->index][0] &= 0xF0;
  911. data->pwm_settings[attr->index][0] |= address;
  912. if (data->pwm_settings[attr->index][0] != orig_val) {
  913. if (abituguru_write(data, ABIT_UGURU_FAN_PWM + 1,
  914. attr->index,
  915. data->pwm_settings[attr->index],
  916. 5) < 1) {
  917. data->pwm_settings[attr->index][0] = orig_val;
  918. ret = -EIO;
  919. }
  920. }
  921. }
  922. else
  923. ret = -EINVAL;
  924. mutex_unlock(&data->update_lock);
  925. return ret;
  926. }
  927. static ssize_t show_pwm_enable(struct device *dev,
  928. struct device_attribute *devattr, char *buf)
  929. {
  930. struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
  931. struct abituguru_data *data = dev_get_drvdata(dev);
  932. int res = 0;
  933. if (data->pwm_settings[attr->index][0] & ABIT_UGURU_FAN_PWM_ENABLE)
  934. res = 2;
  935. return sprintf(buf, "%d\n", res);
  936. }
  937. static ssize_t store_pwm_enable(struct device *dev, struct device_attribute
  938. *devattr, const char *buf, size_t count)
  939. {
  940. struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
  941. struct abituguru_data *data = dev_get_drvdata(dev);
  942. u8 orig_val, user_val = simple_strtoul(buf, NULL, 10);
  943. ssize_t ret = count;
  944. mutex_lock(&data->update_lock);
  945. orig_val = data->pwm_settings[attr->index][0];
  946. switch (user_val) {
  947. case 0:
  948. data->pwm_settings[attr->index][0] &=
  949. ~ABIT_UGURU_FAN_PWM_ENABLE;
  950. break;
  951. case 2:
  952. data->pwm_settings[attr->index][0] |=
  953. ABIT_UGURU_FAN_PWM_ENABLE;
  954. break;
  955. default:
  956. ret = -EINVAL;
  957. }
  958. if ((data->pwm_settings[attr->index][0] != orig_val) &&
  959. (abituguru_write(data, ABIT_UGURU_FAN_PWM + 1,
  960. attr->index, data->pwm_settings[attr->index],
  961. 5) < 1)) {
  962. data->pwm_settings[attr->index][0] = orig_val;
  963. ret = -EIO;
  964. }
  965. mutex_unlock(&data->update_lock);
  966. return ret;
  967. }
  968. static ssize_t show_name(struct device *dev,
  969. struct device_attribute *devattr, char *buf)
  970. {
  971. return sprintf(buf, "%s\n", ABIT_UGURU_NAME);
  972. }
  973. /* Sysfs attr templates, the real entries are generated automatically. */
  974. static const
  975. struct sensor_device_attribute_2 abituguru_sysfs_bank1_templ[2][9] = {
  976. {
  977. SENSOR_ATTR_2(in%d_input, 0444, show_bank1_value, NULL, 0, 0),
  978. SENSOR_ATTR_2(in%d_min, 0644, show_bank1_setting,
  979. store_bank1_setting, 1, 0),
  980. SENSOR_ATTR_2(in%d_min_alarm, 0444, show_bank1_alarm, NULL,
  981. ABIT_UGURU_VOLT_LOW_ALARM_FLAG, 0),
  982. SENSOR_ATTR_2(in%d_max, 0644, show_bank1_setting,
  983. store_bank1_setting, 2, 0),
  984. SENSOR_ATTR_2(in%d_max_alarm, 0444, show_bank1_alarm, NULL,
  985. ABIT_UGURU_VOLT_HIGH_ALARM_FLAG, 0),
  986. SENSOR_ATTR_2(in%d_beep, 0644, show_bank1_mask,
  987. store_bank1_mask, ABIT_UGURU_BEEP_ENABLE, 0),
  988. SENSOR_ATTR_2(in%d_shutdown, 0644, show_bank1_mask,
  989. store_bank1_mask, ABIT_UGURU_SHUTDOWN_ENABLE, 0),
  990. SENSOR_ATTR_2(in%d_min_alarm_enable, 0644, show_bank1_mask,
  991. store_bank1_mask, ABIT_UGURU_VOLT_LOW_ALARM_ENABLE, 0),
  992. SENSOR_ATTR_2(in%d_max_alarm_enable, 0644, show_bank1_mask,
  993. store_bank1_mask, ABIT_UGURU_VOLT_HIGH_ALARM_ENABLE, 0),
  994. }, {
  995. SENSOR_ATTR_2(temp%d_input, 0444, show_bank1_value, NULL, 0, 0),
  996. SENSOR_ATTR_2(temp%d_alarm, 0444, show_bank1_alarm, NULL,
  997. ABIT_UGURU_TEMP_HIGH_ALARM_FLAG, 0),
  998. SENSOR_ATTR_2(temp%d_max, 0644, show_bank1_setting,
  999. store_bank1_setting, 1, 0),
  1000. SENSOR_ATTR_2(temp%d_crit, 0644, show_bank1_setting,
  1001. store_bank1_setting, 2, 0),
  1002. SENSOR_ATTR_2(temp%d_beep, 0644, show_bank1_mask,
  1003. store_bank1_mask, ABIT_UGURU_BEEP_ENABLE, 0),
  1004. SENSOR_ATTR_2(temp%d_shutdown, 0644, show_bank1_mask,
  1005. store_bank1_mask, ABIT_UGURU_SHUTDOWN_ENABLE, 0),
  1006. SENSOR_ATTR_2(temp%d_alarm_enable, 0644, show_bank1_mask,
  1007. store_bank1_mask, ABIT_UGURU_TEMP_HIGH_ALARM_ENABLE, 0),
  1008. }
  1009. };
  1010. static const struct sensor_device_attribute_2 abituguru_sysfs_fan_templ[6] = {
  1011. SENSOR_ATTR_2(fan%d_input, 0444, show_bank2_value, NULL, 0, 0),
  1012. SENSOR_ATTR_2(fan%d_alarm, 0444, show_bank2_alarm, NULL, 0, 0),
  1013. SENSOR_ATTR_2(fan%d_min, 0644, show_bank2_setting,
  1014. store_bank2_setting, 1, 0),
  1015. SENSOR_ATTR_2(fan%d_beep, 0644, show_bank2_mask,
  1016. store_bank2_mask, ABIT_UGURU_BEEP_ENABLE, 0),
  1017. SENSOR_ATTR_2(fan%d_shutdown, 0644, show_bank2_mask,
  1018. store_bank2_mask, ABIT_UGURU_SHUTDOWN_ENABLE, 0),
  1019. SENSOR_ATTR_2(fan%d_alarm_enable, 0644, show_bank2_mask,
  1020. store_bank2_mask, ABIT_UGURU_FAN_LOW_ALARM_ENABLE, 0),
  1021. };
  1022. static const struct sensor_device_attribute_2 abituguru_sysfs_pwm_templ[6] = {
  1023. SENSOR_ATTR_2(pwm%d_enable, 0644, show_pwm_enable,
  1024. store_pwm_enable, 0, 0),
  1025. SENSOR_ATTR_2(pwm%d_auto_channels_temp, 0644, show_pwm_sensor,
  1026. store_pwm_sensor, 0, 0),
  1027. SENSOR_ATTR_2(pwm%d_auto_point1_pwm, 0644, show_pwm_setting,
  1028. store_pwm_setting, 1, 0),
  1029. SENSOR_ATTR_2(pwm%d_auto_point2_pwm, 0644, show_pwm_setting,
  1030. store_pwm_setting, 2, 0),
  1031. SENSOR_ATTR_2(pwm%d_auto_point1_temp, 0644, show_pwm_setting,
  1032. store_pwm_setting, 3, 0),
  1033. SENSOR_ATTR_2(pwm%d_auto_point2_temp, 0644, show_pwm_setting,
  1034. store_pwm_setting, 4, 0),
  1035. };
  1036. static struct sensor_device_attribute_2 abituguru_sysfs_attr[] = {
  1037. SENSOR_ATTR_2(name, 0444, show_name, NULL, 0, 0),
  1038. };
  1039. static int __devinit abituguru_probe(struct platform_device *pdev)
  1040. {
  1041. struct abituguru_data *data;
  1042. int i, j, used, sysfs_names_free, sysfs_attr_i, res = -ENODEV;
  1043. char *sysfs_filename;
  1044. /* El weirdo probe order, to keep the sysfs order identical to the
  1045. BIOS and window-appliction listing order. */
  1046. const u8 probe_order[ABIT_UGURU_MAX_BANK1_SENSORS] = {
  1047. 0x00, 0x01, 0x03, 0x04, 0x0A, 0x08, 0x0E, 0x02,
  1048. 0x09, 0x06, 0x05, 0x0B, 0x0F, 0x0D, 0x07, 0x0C };
  1049. if (!(data = kzalloc(sizeof(struct abituguru_data), GFP_KERNEL)))
  1050. return -ENOMEM;
  1051. data->addr = platform_get_resource(pdev, IORESOURCE_IO, 0)->start;
  1052. mutex_init(&data->update_lock);
  1053. platform_set_drvdata(pdev, data);
  1054. /* See if the uGuru is ready */
  1055. if (inb_p(data->addr + ABIT_UGURU_DATA) == ABIT_UGURU_STATUS_INPUT)
  1056. data->uguru_ready = 1;
  1057. /* Completely read the uGuru this has 2 purposes:
  1058. - testread / see if one really is there.
  1059. - make an in memory copy of all the uguru settings for future use. */
  1060. if (abituguru_read(data, ABIT_UGURU_ALARM_BANK, 0,
  1061. data->alarms, 3, ABIT_UGURU_MAX_RETRIES) != 3)
  1062. goto abituguru_probe_error;
  1063. for (i = 0; i < ABIT_UGURU_MAX_BANK1_SENSORS; i++) {
  1064. if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1, i,
  1065. &data->bank1_value[i], 1,
  1066. ABIT_UGURU_MAX_RETRIES) != 1)
  1067. goto abituguru_probe_error;
  1068. if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1+1, i,
  1069. data->bank1_settings[i], 3,
  1070. ABIT_UGURU_MAX_RETRIES) != 3)
  1071. goto abituguru_probe_error;
  1072. }
  1073. /* Note: We don't know how many bank2 sensors / pwms there really are,
  1074. but in order to "detect" this we need to read the maximum amount
  1075. anyways. If we read sensors/pwms not there we'll just read crap
  1076. this can't hurt. We need the detection because we don't want
  1077. unwanted writes, which will hurt! */
  1078. for (i = 0; i < ABIT_UGURU_MAX_BANK2_SENSORS; i++) {
  1079. if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK2, i,
  1080. &data->bank2_value[i], 1,
  1081. ABIT_UGURU_MAX_RETRIES) != 1)
  1082. goto abituguru_probe_error;
  1083. if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK2+1, i,
  1084. data->bank2_settings[i], 2,
  1085. ABIT_UGURU_MAX_RETRIES) != 2)
  1086. goto abituguru_probe_error;
  1087. }
  1088. for (i = 0; i < ABIT_UGURU_MAX_PWMS; i++) {
  1089. if (abituguru_read(data, ABIT_UGURU_FAN_PWM, i,
  1090. data->pwm_settings[i], 5,
  1091. ABIT_UGURU_MAX_RETRIES) != 5)
  1092. goto abituguru_probe_error;
  1093. }
  1094. data->last_updated = jiffies;
  1095. /* Detect sensor types and fill the sysfs attr for bank1 */
  1096. sysfs_attr_i = 0;
  1097. sysfs_filename = data->sysfs_names;
  1098. sysfs_names_free = ABITUGURU_SYSFS_NAMES_LENGTH;
  1099. for (i = 0; i < ABIT_UGURU_MAX_BANK1_SENSORS; i++) {
  1100. res = abituguru_detect_bank1_sensor_type(data, probe_order[i]);
  1101. if (res < 0)
  1102. goto abituguru_probe_error;
  1103. if (res == ABIT_UGURU_NC)
  1104. continue;
  1105. /* res 1 (temp) sensors have 7 sysfs entries, 0 (in) 9 */
  1106. for (j = 0; j < (res ? 7 : 9); j++) {
  1107. used = snprintf(sysfs_filename, sysfs_names_free,
  1108. abituguru_sysfs_bank1_templ[res][j].dev_attr.
  1109. attr.name, data->bank1_sensors[res] + res)
  1110. + 1;
  1111. data->sysfs_attr[sysfs_attr_i] =
  1112. abituguru_sysfs_bank1_templ[res][j];
  1113. data->sysfs_attr[sysfs_attr_i].dev_attr.attr.name =
  1114. sysfs_filename;
  1115. data->sysfs_attr[sysfs_attr_i].index = probe_order[i];
  1116. sysfs_filename += used;
  1117. sysfs_names_free -= used;
  1118. sysfs_attr_i++;
  1119. }
  1120. data->bank1_max_value[probe_order[i]] =
  1121. abituguru_bank1_max_value[res];
  1122. data->bank1_address[res][data->bank1_sensors[res]] =
  1123. probe_order[i];
  1124. data->bank1_sensors[res]++;
  1125. }
  1126. /* Detect number of sensors and fill the sysfs attr for bank2 (fans) */
  1127. abituguru_detect_no_bank2_sensors(data);
  1128. for (i = 0; i < data->bank2_sensors; i++) {
  1129. for (j = 0; j < ARRAY_SIZE(abituguru_sysfs_fan_templ); j++) {
  1130. used = snprintf(sysfs_filename, sysfs_names_free,
  1131. abituguru_sysfs_fan_templ[j].dev_attr.attr.name,
  1132. i + 1) + 1;
  1133. data->sysfs_attr[sysfs_attr_i] =
  1134. abituguru_sysfs_fan_templ[j];
  1135. data->sysfs_attr[sysfs_attr_i].dev_attr.attr.name =
  1136. sysfs_filename;
  1137. data->sysfs_attr[sysfs_attr_i].index = i;
  1138. sysfs_filename += used;
  1139. sysfs_names_free -= used;
  1140. sysfs_attr_i++;
  1141. }
  1142. }
  1143. /* Detect number of sensors and fill the sysfs attr for pwms */
  1144. abituguru_detect_no_pwms(data);
  1145. for (i = 0; i < data->pwms; i++) {
  1146. for (j = 0; j < ARRAY_SIZE(abituguru_sysfs_pwm_templ); j++) {
  1147. used = snprintf(sysfs_filename, sysfs_names_free,
  1148. abituguru_sysfs_pwm_templ[j].dev_attr.attr.name,
  1149. i + 1) + 1;
  1150. data->sysfs_attr[sysfs_attr_i] =
  1151. abituguru_sysfs_pwm_templ[j];
  1152. data->sysfs_attr[sysfs_attr_i].dev_attr.attr.name =
  1153. sysfs_filename;
  1154. data->sysfs_attr[sysfs_attr_i].index = i;
  1155. sysfs_filename += used;
  1156. sysfs_names_free -= used;
  1157. sysfs_attr_i++;
  1158. }
  1159. }
  1160. /* Fail safe check, this should never happen! */
  1161. if (sysfs_names_free < 0) {
  1162. pr_err("Fatal error ran out of space for sysfs attr names. %s %s",
  1163. never_happen, report_this);
  1164. res = -ENAMETOOLONG;
  1165. goto abituguru_probe_error;
  1166. }
  1167. pr_info("found Abit uGuru\n");
  1168. /* Register sysfs hooks */
  1169. for (i = 0; i < sysfs_attr_i; i++)
  1170. if (device_create_file(&pdev->dev,
  1171. &data->sysfs_attr[i].dev_attr))
  1172. goto abituguru_probe_error;
  1173. for (i = 0; i < ARRAY_SIZE(abituguru_sysfs_attr); i++)
  1174. if (device_create_file(&pdev->dev,
  1175. &abituguru_sysfs_attr[i].dev_attr))
  1176. goto abituguru_probe_error;
  1177. data->hwmon_dev = hwmon_device_register(&pdev->dev);
  1178. if (!IS_ERR(data->hwmon_dev))
  1179. return 0; /* success */
  1180. res = PTR_ERR(data->hwmon_dev);
  1181. abituguru_probe_error:
  1182. for (i = 0; data->sysfs_attr[i].dev_attr.attr.name; i++)
  1183. device_remove_file(&pdev->dev, &data->sysfs_attr[i].dev_attr);
  1184. for (i = 0; i < ARRAY_SIZE(abituguru_sysfs_attr); i++)
  1185. device_remove_file(&pdev->dev,
  1186. &abituguru_sysfs_attr[i].dev_attr);
  1187. platform_set_drvdata(pdev, NULL);
  1188. kfree(data);
  1189. return res;
  1190. }
  1191. static int __devexit abituguru_remove(struct platform_device *pdev)
  1192. {
  1193. int i;
  1194. struct abituguru_data *data = platform_get_drvdata(pdev);
  1195. hwmon_device_unregister(data->hwmon_dev);
  1196. for (i = 0; data->sysfs_attr[i].dev_attr.attr.name; i++)
  1197. device_remove_file(&pdev->dev, &data->sysfs_attr[i].dev_attr);
  1198. for (i = 0; i < ARRAY_SIZE(abituguru_sysfs_attr); i++)
  1199. device_remove_file(&pdev->dev,
  1200. &abituguru_sysfs_attr[i].dev_attr);
  1201. platform_set_drvdata(pdev, NULL);
  1202. kfree(data);
  1203. return 0;
  1204. }
  1205. static struct abituguru_data *abituguru_update_device(struct device *dev)
  1206. {
  1207. int i, err;
  1208. struct abituguru_data *data = dev_get_drvdata(dev);
  1209. /* fake a complete successful read if no update necessary. */
  1210. char success = 1;
  1211. mutex_lock(&data->update_lock);
  1212. if (time_after(jiffies, data->last_updated + HZ)) {
  1213. success = 0;
  1214. if ((err = abituguru_read(data, ABIT_UGURU_ALARM_BANK, 0,
  1215. data->alarms, 3, 0)) != 3)
  1216. goto LEAVE_UPDATE;
  1217. for (i = 0; i < ABIT_UGURU_MAX_BANK1_SENSORS; i++) {
  1218. if ((err = abituguru_read(data,
  1219. ABIT_UGURU_SENSOR_BANK1, i,
  1220. &data->bank1_value[i], 1, 0)) != 1)
  1221. goto LEAVE_UPDATE;
  1222. if ((err = abituguru_read(data,
  1223. ABIT_UGURU_SENSOR_BANK1 + 1, i,
  1224. data->bank1_settings[i], 3, 0)) != 3)
  1225. goto LEAVE_UPDATE;
  1226. }
  1227. for (i = 0; i < data->bank2_sensors; i++)
  1228. if ((err = abituguru_read(data,
  1229. ABIT_UGURU_SENSOR_BANK2, i,
  1230. &data->bank2_value[i], 1, 0)) != 1)
  1231. goto LEAVE_UPDATE;
  1232. /* success! */
  1233. success = 1;
  1234. data->update_timeouts = 0;
  1235. LEAVE_UPDATE:
  1236. /* handle timeout condition */
  1237. if (!success && (err == -EBUSY || err >= 0)) {
  1238. /* No overflow please */
  1239. if (data->update_timeouts < 255u)
  1240. data->update_timeouts++;
  1241. if (data->update_timeouts <= ABIT_UGURU_MAX_TIMEOUTS) {
  1242. ABIT_UGURU_DEBUG(3, "timeout exceeded, will "
  1243. "try again next update\n");
  1244. /* Just a timeout, fake a successful read */
  1245. success = 1;
  1246. } else
  1247. ABIT_UGURU_DEBUG(1, "timeout exceeded %d "
  1248. "times waiting for more input state\n",
  1249. (int)data->update_timeouts);
  1250. }
  1251. /* On success set last_updated */
  1252. if (success)
  1253. data->last_updated = jiffies;
  1254. }
  1255. mutex_unlock(&data->update_lock);
  1256. if (success)
  1257. return data;
  1258. else
  1259. return NULL;
  1260. }
  1261. #ifdef CONFIG_PM
  1262. static int abituguru_suspend(struct platform_device *pdev, pm_message_t state)
  1263. {
  1264. struct abituguru_data *data = platform_get_drvdata(pdev);
  1265. /* make sure all communications with the uguru are done and no new
  1266. ones are started */
  1267. mutex_lock(&data->update_lock);
  1268. return 0;
  1269. }
  1270. static int abituguru_resume(struct platform_device *pdev)
  1271. {
  1272. struct abituguru_data *data = platform_get_drvdata(pdev);
  1273. /* See if the uGuru is still ready */
  1274. if (inb_p(data->addr + ABIT_UGURU_DATA) != ABIT_UGURU_STATUS_INPUT)
  1275. data->uguru_ready = 0;
  1276. mutex_unlock(&data->update_lock);
  1277. return 0;
  1278. }
  1279. #else
  1280. #define abituguru_suspend NULL
  1281. #define abituguru_resume NULL
  1282. #endif /* CONFIG_PM */
  1283. static struct platform_driver abituguru_driver = {
  1284. .driver = {
  1285. .owner = THIS_MODULE,
  1286. .name = ABIT_UGURU_NAME,
  1287. },
  1288. .probe = abituguru_probe,
  1289. .remove = __devexit_p(abituguru_remove),
  1290. .suspend = abituguru_suspend,
  1291. .resume = abituguru_resume,
  1292. };
  1293. static int __init abituguru_detect(void)
  1294. {
  1295. /* See if there is an uguru there. After a reboot uGuru will hold 0x00
  1296. at DATA and 0xAC, when this driver has already been loaded once
  1297. DATA will hold 0x08. For most uGuru's CMD will hold 0xAC in either
  1298. scenario but some will hold 0x00.
  1299. Some uGuru's initially hold 0x09 at DATA and will only hold 0x08
  1300. after reading CMD first, so CMD must be read first! */
  1301. u8 cmd_val = inb_p(ABIT_UGURU_BASE + ABIT_UGURU_CMD);
  1302. u8 data_val = inb_p(ABIT_UGURU_BASE + ABIT_UGURU_DATA);
  1303. if (((data_val == 0x00) || (data_val == 0x08)) &&
  1304. ((cmd_val == 0x00) || (cmd_val == 0xAC)))
  1305. return ABIT_UGURU_BASE;
  1306. ABIT_UGURU_DEBUG(2, "no Abit uGuru found, data = 0x%02X, cmd = "
  1307. "0x%02X\n", (unsigned int)data_val, (unsigned int)cmd_val);
  1308. if (force) {
  1309. pr_info("Assuming Abit uGuru is present because of \"force\" parameter\n");
  1310. return ABIT_UGURU_BASE;
  1311. }
  1312. /* No uGuru found */
  1313. return -ENODEV;
  1314. }
  1315. static struct platform_device *abituguru_pdev;
  1316. static int __init abituguru_init(void)
  1317. {
  1318. int address, err;
  1319. struct resource res = { .flags = IORESOURCE_IO };
  1320. const char *board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
  1321. /* safety check, refuse to load on non Abit motherboards */
  1322. if (!force && (!board_vendor ||
  1323. strcmp(board_vendor, "http://www.abit.com.tw/")))
  1324. return -ENODEV;
  1325. address = abituguru_detect();
  1326. if (address < 0)
  1327. return address;
  1328. err = platform_driver_register(&abituguru_driver);
  1329. if (err)
  1330. goto exit;
  1331. abituguru_pdev = platform_device_alloc(ABIT_UGURU_NAME, address);
  1332. if (!abituguru_pdev) {
  1333. pr_err("Device allocation failed\n");
  1334. err = -ENOMEM;
  1335. goto exit_driver_unregister;
  1336. }
  1337. res.start = address;
  1338. res.end = address + ABIT_UGURU_REGION_LENGTH - 1;
  1339. res.name = ABIT_UGURU_NAME;
  1340. err = platform_device_add_resources(abituguru_pdev, &res, 1);
  1341. if (err) {
  1342. pr_err("Device resource addition failed (%d)\n", err);
  1343. goto exit_device_put;
  1344. }
  1345. err = platform_device_add(abituguru_pdev);
  1346. if (err) {
  1347. pr_err("Device addition failed (%d)\n", err);
  1348. goto exit_device_put;
  1349. }
  1350. return 0;
  1351. exit_device_put:
  1352. platform_device_put(abituguru_pdev);
  1353. exit_driver_unregister:
  1354. platform_driver_unregister(&abituguru_driver);
  1355. exit:
  1356. return err;
  1357. }
  1358. static void __exit abituguru_exit(void)
  1359. {
  1360. platform_device_unregister(abituguru_pdev);
  1361. platform_driver_unregister(&abituguru_driver);
  1362. }
  1363. MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
  1364. MODULE_DESCRIPTION("Abit uGuru Sensor device");
  1365. MODULE_LICENSE("GPL");
  1366. module_init(abituguru_init);
  1367. module_exit(abituguru_exit);