abituguru.c 52 KB

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