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