abituguru.c 53 KB

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