lm93.c 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655
  1. /*
  2. lm93.c - Part of lm_sensors, Linux kernel modules for hardware monitoring
  3. Author/Maintainer: Mark M. Hoffman <mhoffman@lightlink.com>
  4. Copyright (c) 2004 Utilitek Systems, Inc.
  5. derived in part from lm78.c:
  6. Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
  7. derived in part from lm85.c:
  8. Copyright (c) 2002, 2003 Philip Pokorny <ppokorny@penguincomputing.com>
  9. Copyright (c) 2003 Margit Schubert-While <margitsw@t-online.de>
  10. derived in part from w83l785ts.c:
  11. Copyright (c) 2003-2004 Jean Delvare <khali@linux-fr.org>
  12. Ported to Linux 2.6 by Eric J. Bowersox <ericb@aspsys.com>
  13. Copyright (c) 2005 Aspen Systems, Inc.
  14. Adapted to 2.6.20 by Carsten Emde <cbe@osadl.org>
  15. Copyright (c) 2006 Carsten Emde, Open Source Automation Development Lab
  16. Modified for mainline integration by Hans J. Koch <hjk@hansjkoch.de>
  17. Copyright (c) 2007 Hans J. Koch, Linutronix GmbH
  18. This program is free software; you can redistribute it and/or modify
  19. it under the terms of the GNU General Public License as published by
  20. the Free Software Foundation; either version 2 of the License, or
  21. (at your option) any later version.
  22. This program is distributed in the hope that it will be useful,
  23. but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. GNU General Public License for more details.
  26. You should have received a copy of the GNU General Public License
  27. along with this program; if not, write to the Free Software
  28. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  29. */
  30. #include <linux/module.h>
  31. #include <linux/init.h>
  32. #include <linux/slab.h>
  33. #include <linux/i2c.h>
  34. #include <linux/hwmon.h>
  35. #include <linux/hwmon-sysfs.h>
  36. #include <linux/hwmon-vid.h>
  37. #include <linux/err.h>
  38. #include <linux/delay.h>
  39. /* LM93 REGISTER ADDRESSES */
  40. /* miscellaneous */
  41. #define LM93_REG_MFR_ID 0x3e
  42. #define LM93_REG_VER 0x3f
  43. #define LM93_REG_STATUS_CONTROL 0xe2
  44. #define LM93_REG_CONFIG 0xe3
  45. #define LM93_REG_SLEEP_CONTROL 0xe4
  46. /* alarm values start here */
  47. #define LM93_REG_HOST_ERROR_1 0x48
  48. /* voltage inputs: in1-in16 (nr => 0-15) */
  49. #define LM93_REG_IN(nr) (0x56 + (nr))
  50. #define LM93_REG_IN_MIN(nr) (0x90 + (nr) * 2)
  51. #define LM93_REG_IN_MAX(nr) (0x91 + (nr) * 2)
  52. /* temperature inputs: temp1-temp4 (nr => 0-3) */
  53. #define LM93_REG_TEMP(nr) (0x50 + (nr))
  54. #define LM93_REG_TEMP_MIN(nr) (0x78 + (nr) * 2)
  55. #define LM93_REG_TEMP_MAX(nr) (0x79 + (nr) * 2)
  56. /* temp[1-4]_auto_boost (nr => 0-3) */
  57. #define LM93_REG_BOOST(nr) (0x80 + (nr))
  58. /* #PROCHOT inputs: prochot1-prochot2 (nr => 0-1) */
  59. #define LM93_REG_PROCHOT_CUR(nr) (0x67 + (nr) * 2)
  60. #define LM93_REG_PROCHOT_AVG(nr) (0x68 + (nr) * 2)
  61. #define LM93_REG_PROCHOT_MAX(nr) (0xb0 + (nr))
  62. /* fan tach inputs: fan1-fan4 (nr => 0-3) */
  63. #define LM93_REG_FAN(nr) (0x6e + (nr) * 2)
  64. #define LM93_REG_FAN_MIN(nr) (0xb4 + (nr) * 2)
  65. /* pwm outputs: pwm1-pwm2 (nr => 0-1, reg => 0-3) */
  66. #define LM93_REG_PWM_CTL(nr,reg) (0xc8 + (reg) + (nr) * 4)
  67. #define LM93_PWM_CTL1 0x0
  68. #define LM93_PWM_CTL2 0x1
  69. #define LM93_PWM_CTL3 0x2
  70. #define LM93_PWM_CTL4 0x3
  71. /* GPIO input state */
  72. #define LM93_REG_GPI 0x6b
  73. /* vid inputs: vid1-vid2 (nr => 0-1) */
  74. #define LM93_REG_VID(nr) (0x6c + (nr))
  75. /* vccp1 & vccp2: VID relative inputs (nr => 0-1) */
  76. #define LM93_REG_VCCP_LIMIT_OFF(nr) (0xb2 + (nr))
  77. /* temp[1-4]_auto_boost_hyst */
  78. #define LM93_REG_BOOST_HYST_12 0xc0
  79. #define LM93_REG_BOOST_HYST_34 0xc1
  80. #define LM93_REG_BOOST_HYST(nr) (0xc0 + (nr)/2)
  81. /* temp[1-4]_auto_pwm_[min|hyst] */
  82. #define LM93_REG_PWM_MIN_HYST_12 0xc3
  83. #define LM93_REG_PWM_MIN_HYST_34 0xc4
  84. #define LM93_REG_PWM_MIN_HYST(nr) (0xc3 + (nr)/2)
  85. /* prochot_override & prochot_interval */
  86. #define LM93_REG_PROCHOT_OVERRIDE 0xc6
  87. #define LM93_REG_PROCHOT_INTERVAL 0xc7
  88. /* temp[1-4]_auto_base (nr => 0-3) */
  89. #define LM93_REG_TEMP_BASE(nr) (0xd0 + (nr))
  90. /* temp[1-4]_auto_offsets (step => 0-11) */
  91. #define LM93_REG_TEMP_OFFSET(step) (0xd4 + (step))
  92. /* #PROCHOT & #VRDHOT PWM ramp control */
  93. #define LM93_REG_PWM_RAMP_CTL 0xbf
  94. /* miscellaneous */
  95. #define LM93_REG_SFC1 0xbc
  96. #define LM93_REG_SFC2 0xbd
  97. #define LM93_REG_GPI_VID_CTL 0xbe
  98. #define LM93_REG_SF_TACH_TO_PWM 0xe0
  99. /* error masks */
  100. #define LM93_REG_GPI_ERR_MASK 0xec
  101. #define LM93_REG_MISC_ERR_MASK 0xed
  102. /* LM93 REGISTER VALUES */
  103. #define LM93_MFR_ID 0x73
  104. #define LM93_MFR_ID_PROTOTYPE 0x72
  105. /* LM94 REGISTER VALUES */
  106. #define LM94_MFR_ID_2 0x7a
  107. #define LM94_MFR_ID 0x79
  108. #define LM94_MFR_ID_PROTOTYPE 0x78
  109. /* SMBus capabilities */
  110. #define LM93_SMBUS_FUNC_FULL (I2C_FUNC_SMBUS_BYTE_DATA | \
  111. I2C_FUNC_SMBUS_WORD_DATA | I2C_FUNC_SMBUS_BLOCK_DATA)
  112. #define LM93_SMBUS_FUNC_MIN (I2C_FUNC_SMBUS_BYTE_DATA | \
  113. I2C_FUNC_SMBUS_WORD_DATA)
  114. /* Addresses to scan */
  115. static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
  116. /* Insmod parameters */
  117. static int disable_block;
  118. module_param(disable_block, bool, 0);
  119. MODULE_PARM_DESC(disable_block,
  120. "Set to non-zero to disable SMBus block data transactions.");
  121. static int init;
  122. module_param(init, bool, 0);
  123. MODULE_PARM_DESC(init, "Set to non-zero to force chip initialization.");
  124. static int vccp_limit_type[2] = {0,0};
  125. module_param_array(vccp_limit_type, int, NULL, 0);
  126. MODULE_PARM_DESC(vccp_limit_type, "Configures in7 and in8 limit modes.");
  127. static int vid_agtl;
  128. module_param(vid_agtl, int, 0);
  129. MODULE_PARM_DESC(vid_agtl, "Configures VID pin input thresholds.");
  130. /* Driver data */
  131. static struct i2c_driver lm93_driver;
  132. /* LM93 BLOCK READ COMMANDS */
  133. static const struct { u8 cmd; u8 len; } lm93_block_read_cmds[12] = {
  134. { 0xf2, 8 },
  135. { 0xf3, 8 },
  136. { 0xf4, 6 },
  137. { 0xf5, 16 },
  138. { 0xf6, 4 },
  139. { 0xf7, 8 },
  140. { 0xf8, 12 },
  141. { 0xf9, 32 },
  142. { 0xfa, 8 },
  143. { 0xfb, 8 },
  144. { 0xfc, 16 },
  145. { 0xfd, 9 },
  146. };
  147. /* ALARMS: SYSCTL format described further below
  148. REG: 64 bits in 8 registers, as immediately below */
  149. struct block1_t {
  150. u8 host_status_1;
  151. u8 host_status_2;
  152. u8 host_status_3;
  153. u8 host_status_4;
  154. u8 p1_prochot_status;
  155. u8 p2_prochot_status;
  156. u8 gpi_status;
  157. u8 fan_status;
  158. };
  159. /*
  160. * Client-specific data
  161. */
  162. struct lm93_data {
  163. struct device *hwmon_dev;
  164. struct mutex update_lock;
  165. unsigned long last_updated; /* In jiffies */
  166. /* client update function */
  167. void (*update)(struct lm93_data *, struct i2c_client *);
  168. char valid; /* !=0 if following fields are valid */
  169. /* register values, arranged by block read groups */
  170. struct block1_t block1;
  171. /* temp1 - temp4: unfiltered readings
  172. temp1 - temp2: filtered readings */
  173. u8 block2[6];
  174. /* vin1 - vin16: readings */
  175. u8 block3[16];
  176. /* prochot1 - prochot2: readings */
  177. struct {
  178. u8 cur;
  179. u8 avg;
  180. } block4[2];
  181. /* fan counts 1-4 => 14-bits, LE, *left* justified */
  182. u16 block5[4];
  183. /* block6 has a lot of data we don't need */
  184. struct {
  185. u8 min;
  186. u8 max;
  187. } temp_lim[4];
  188. /* vin1 - vin16: low and high limits */
  189. struct {
  190. u8 min;
  191. u8 max;
  192. } block7[16];
  193. /* fan count limits 1-4 => same format as block5 */
  194. u16 block8[4];
  195. /* pwm control registers (2 pwms, 4 regs) */
  196. u8 block9[2][4];
  197. /* auto/pwm base temp and offset temp registers */
  198. struct {
  199. u8 base[4];
  200. u8 offset[12];
  201. } block10;
  202. /* master config register */
  203. u8 config;
  204. /* VID1 & VID2 => register format, 6-bits, right justified */
  205. u8 vid[2];
  206. /* prochot1 - prochot2: limits */
  207. u8 prochot_max[2];
  208. /* vccp1 & vccp2 (in7 & in8): VID relative limits (register format) */
  209. u8 vccp_limits[2];
  210. /* GPIO input state (register format, i.e. inverted) */
  211. u8 gpi;
  212. /* #PROCHOT override (register format) */
  213. u8 prochot_override;
  214. /* #PROCHOT intervals (register format) */
  215. u8 prochot_interval;
  216. /* Fan Boost Temperatures (register format) */
  217. u8 boost[4];
  218. /* Fan Boost Hysteresis (register format) */
  219. u8 boost_hyst[2];
  220. /* Temperature Zone Min. PWM & Hysteresis (register format) */
  221. u8 auto_pwm_min_hyst[2];
  222. /* #PROCHOT & #VRDHOT PWM Ramp Control */
  223. u8 pwm_ramp_ctl;
  224. /* miscellaneous setup regs */
  225. u8 sfc1;
  226. u8 sfc2;
  227. u8 sf_tach_to_pwm;
  228. /* The two PWM CTL2 registers can read something other than what was
  229. last written for the OVR_DC field (duty cycle override). So, we
  230. save the user-commanded value here. */
  231. u8 pwm_override[2];
  232. };
  233. /* VID: mV
  234. REG: 6-bits, right justified, *always* using Intel VRM/VRD 10 */
  235. static int LM93_VID_FROM_REG(u8 reg)
  236. {
  237. return vid_from_reg((reg & 0x3f), 100);
  238. }
  239. /* min, max, and nominal register values, per channel (u8) */
  240. static const u8 lm93_vin_reg_min[16] = {
  241. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  242. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xae,
  243. };
  244. static const u8 lm93_vin_reg_max[16] = {
  245. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  246. 0xff, 0xfa, 0xff, 0xff, 0xff, 0xff, 0xff, 0xd1,
  247. };
  248. /* Values from the datasheet. They're here for documentation only.
  249. static const u8 lm93_vin_reg_nom[16] = {
  250. 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0,
  251. 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0x40, 0xc0,
  252. };
  253. */
  254. /* min, max, and nominal voltage readings, per channel (mV)*/
  255. static const unsigned long lm93_vin_val_min[16] = {
  256. 0, 0, 0, 0, 0, 0, 0, 0,
  257. 0, 0, 0, 0, 0, 0, 0, 3000,
  258. };
  259. static const unsigned long lm93_vin_val_max[16] = {
  260. 1236, 1236, 1236, 1600, 2000, 2000, 1600, 1600,
  261. 4400, 6500, 3333, 2625, 1312, 1312, 1236, 3600,
  262. };
  263. /* Values from the datasheet. They're here for documentation only.
  264. static const unsigned long lm93_vin_val_nom[16] = {
  265. 927, 927, 927, 1200, 1500, 1500, 1200, 1200,
  266. 3300, 5000, 2500, 1969, 984, 984, 309, 3300,
  267. };
  268. */
  269. static unsigned LM93_IN_FROM_REG(int nr, u8 reg)
  270. {
  271. const long uV_max = lm93_vin_val_max[nr] * 1000;
  272. const long uV_min = lm93_vin_val_min[nr] * 1000;
  273. const long slope = (uV_max - uV_min) /
  274. (lm93_vin_reg_max[nr] - lm93_vin_reg_min[nr]);
  275. const long intercept = uV_min - slope * lm93_vin_reg_min[nr];
  276. return (slope * reg + intercept + 500) / 1000;
  277. }
  278. /* IN: mV, limits determined by channel nr
  279. REG: scaling determined by channel nr */
  280. static u8 LM93_IN_TO_REG(int nr, unsigned val)
  281. {
  282. /* range limit */
  283. const long mV = SENSORS_LIMIT(val,
  284. lm93_vin_val_min[nr], lm93_vin_val_max[nr]);
  285. /* try not to lose too much precision here */
  286. const long uV = mV * 1000;
  287. const long uV_max = lm93_vin_val_max[nr] * 1000;
  288. const long uV_min = lm93_vin_val_min[nr] * 1000;
  289. /* convert */
  290. const long slope = (uV_max - uV_min) /
  291. (lm93_vin_reg_max[nr] - lm93_vin_reg_min[nr]);
  292. const long intercept = uV_min - slope * lm93_vin_reg_min[nr];
  293. u8 result = ((uV - intercept + (slope/2)) / slope);
  294. result = SENSORS_LIMIT(result,
  295. lm93_vin_reg_min[nr], lm93_vin_reg_max[nr]);
  296. return result;
  297. }
  298. /* vid in mV, upper == 0 indicates low limit, otherwise upper limit */
  299. static unsigned LM93_IN_REL_FROM_REG(u8 reg, int upper, int vid)
  300. {
  301. const long uV_offset = upper ? (((reg >> 4 & 0x0f) + 1) * 12500) :
  302. (((reg >> 0 & 0x0f) + 1) * -25000);
  303. const long uV_vid = vid * 1000;
  304. return (uV_vid + uV_offset + 5000) / 10000;
  305. }
  306. #define LM93_IN_MIN_FROM_REG(reg,vid) LM93_IN_REL_FROM_REG(reg,0,vid)
  307. #define LM93_IN_MAX_FROM_REG(reg,vid) LM93_IN_REL_FROM_REG(reg,1,vid)
  308. /* vid in mV , upper == 0 indicates low limit, otherwise upper limit
  309. upper also determines which nibble of the register is returned
  310. (the other nibble will be 0x0) */
  311. static u8 LM93_IN_REL_TO_REG(unsigned val, int upper, int vid)
  312. {
  313. long uV_offset = vid * 1000 - val * 10000;
  314. if (upper) {
  315. uV_offset = SENSORS_LIMIT(uV_offset, 12500, 200000);
  316. return (u8)((uV_offset / 12500 - 1) << 4);
  317. } else {
  318. uV_offset = SENSORS_LIMIT(uV_offset, -400000, -25000);
  319. return (u8)((uV_offset / -25000 - 1) << 0);
  320. }
  321. }
  322. /* TEMP: 1/1000 degrees C (-128C to +127C)
  323. REG: 1C/bit, two's complement */
  324. static int LM93_TEMP_FROM_REG(u8 reg)
  325. {
  326. return (s8)reg * 1000;
  327. }
  328. #define LM93_TEMP_MIN (-128000)
  329. #define LM93_TEMP_MAX ( 127000)
  330. /* TEMP: 1/1000 degrees C (-128C to +127C)
  331. REG: 1C/bit, two's complement */
  332. static u8 LM93_TEMP_TO_REG(long temp)
  333. {
  334. int ntemp = SENSORS_LIMIT(temp, LM93_TEMP_MIN, LM93_TEMP_MAX);
  335. ntemp += (ntemp<0 ? -500 : 500);
  336. return (u8)(ntemp / 1000);
  337. }
  338. /* Determine 4-bit temperature offset resolution */
  339. static int LM93_TEMP_OFFSET_MODE_FROM_REG(u8 sfc2, int nr)
  340. {
  341. /* mode: 0 => 1C/bit, nonzero => 0.5C/bit */
  342. return sfc2 & (nr < 2 ? 0x10 : 0x20);
  343. }
  344. /* This function is common to all 4-bit temperature offsets
  345. reg is 4 bits right justified
  346. mode 0 => 1C/bit, mode !0 => 0.5C/bit */
  347. static int LM93_TEMP_OFFSET_FROM_REG(u8 reg, int mode)
  348. {
  349. return (reg & 0x0f) * (mode ? 5 : 10);
  350. }
  351. #define LM93_TEMP_OFFSET_MIN ( 0)
  352. #define LM93_TEMP_OFFSET_MAX0 (150)
  353. #define LM93_TEMP_OFFSET_MAX1 ( 75)
  354. /* This function is common to all 4-bit temperature offsets
  355. returns 4 bits right justified
  356. mode 0 => 1C/bit, mode !0 => 0.5C/bit */
  357. static u8 LM93_TEMP_OFFSET_TO_REG(int off, int mode)
  358. {
  359. int factor = mode ? 5 : 10;
  360. off = SENSORS_LIMIT(off, LM93_TEMP_OFFSET_MIN,
  361. mode ? LM93_TEMP_OFFSET_MAX1 : LM93_TEMP_OFFSET_MAX0);
  362. return (u8)((off + factor/2) / factor);
  363. }
  364. /* 0 <= nr <= 3 */
  365. static int LM93_TEMP_AUTO_OFFSET_FROM_REG(u8 reg, int nr, int mode)
  366. {
  367. /* temp1-temp2 (nr=0,1) use lower nibble */
  368. if (nr < 2)
  369. return LM93_TEMP_OFFSET_FROM_REG(reg & 0x0f, mode);
  370. /* temp3-temp4 (nr=2,3) use upper nibble */
  371. else
  372. return LM93_TEMP_OFFSET_FROM_REG(reg >> 4 & 0x0f, mode);
  373. }
  374. /* TEMP: 1/10 degrees C (0C to +15C (mode 0) or +7.5C (mode non-zero))
  375. REG: 1.0C/bit (mode 0) or 0.5C/bit (mode non-zero)
  376. 0 <= nr <= 3 */
  377. static u8 LM93_TEMP_AUTO_OFFSET_TO_REG(u8 old, int off, int nr, int mode)
  378. {
  379. u8 new = LM93_TEMP_OFFSET_TO_REG(off, mode);
  380. /* temp1-temp2 (nr=0,1) use lower nibble */
  381. if (nr < 2)
  382. return (old & 0xf0) | (new & 0x0f);
  383. /* temp3-temp4 (nr=2,3) use upper nibble */
  384. else
  385. return (new << 4 & 0xf0) | (old & 0x0f);
  386. }
  387. static int LM93_AUTO_BOOST_HYST_FROM_REGS(struct lm93_data *data, int nr,
  388. int mode)
  389. {
  390. u8 reg;
  391. switch (nr) {
  392. case 0:
  393. reg = data->boost_hyst[0] & 0x0f;
  394. break;
  395. case 1:
  396. reg = data->boost_hyst[0] >> 4 & 0x0f;
  397. break;
  398. case 2:
  399. reg = data->boost_hyst[1] & 0x0f;
  400. break;
  401. case 3:
  402. default:
  403. reg = data->boost_hyst[1] >> 4 & 0x0f;
  404. break;
  405. }
  406. return LM93_TEMP_FROM_REG(data->boost[nr]) -
  407. LM93_TEMP_OFFSET_FROM_REG(reg, mode);
  408. }
  409. static u8 LM93_AUTO_BOOST_HYST_TO_REG(struct lm93_data *data, long hyst,
  410. int nr, int mode)
  411. {
  412. u8 reg = LM93_TEMP_OFFSET_TO_REG(
  413. (LM93_TEMP_FROM_REG(data->boost[nr]) - hyst), mode);
  414. switch (nr) {
  415. case 0:
  416. reg = (data->boost_hyst[0] & 0xf0) | (reg & 0x0f);
  417. break;
  418. case 1:
  419. reg = (reg << 4 & 0xf0) | (data->boost_hyst[0] & 0x0f);
  420. break;
  421. case 2:
  422. reg = (data->boost_hyst[1] & 0xf0) | (reg & 0x0f);
  423. break;
  424. case 3:
  425. default:
  426. reg = (reg << 4 & 0xf0) | (data->boost_hyst[1] & 0x0f);
  427. break;
  428. }
  429. return reg;
  430. }
  431. /* PWM: 0-255 per sensors documentation
  432. REG: 0-13 as mapped below... right justified */
  433. typedef enum { LM93_PWM_MAP_HI_FREQ, LM93_PWM_MAP_LO_FREQ } pwm_freq_t;
  434. static int lm93_pwm_map[2][16] = {
  435. {
  436. 0x00, /* 0.00% */ 0x40, /* 25.00% */
  437. 0x50, /* 31.25% */ 0x60, /* 37.50% */
  438. 0x70, /* 43.75% */ 0x80, /* 50.00% */
  439. 0x90, /* 56.25% */ 0xa0, /* 62.50% */
  440. 0xb0, /* 68.75% */ 0xc0, /* 75.00% */
  441. 0xd0, /* 81.25% */ 0xe0, /* 87.50% */
  442. 0xf0, /* 93.75% */ 0xff, /* 100.00% */
  443. 0xff, 0xff, /* 14, 15 are reserved and should never occur */
  444. },
  445. {
  446. 0x00, /* 0.00% */ 0x40, /* 25.00% */
  447. 0x49, /* 28.57% */ 0x52, /* 32.14% */
  448. 0x5b, /* 35.71% */ 0x64, /* 39.29% */
  449. 0x6d, /* 42.86% */ 0x76, /* 46.43% */
  450. 0x80, /* 50.00% */ 0x89, /* 53.57% */
  451. 0x92, /* 57.14% */ 0xb6, /* 71.43% */
  452. 0xdb, /* 85.71% */ 0xff, /* 100.00% */
  453. 0xff, 0xff, /* 14, 15 are reserved and should never occur */
  454. },
  455. };
  456. static int LM93_PWM_FROM_REG(u8 reg, pwm_freq_t freq)
  457. {
  458. return lm93_pwm_map[freq][reg & 0x0f];
  459. }
  460. /* round up to nearest match */
  461. static u8 LM93_PWM_TO_REG(int pwm, pwm_freq_t freq)
  462. {
  463. int i;
  464. for (i = 0; i < 13; i++)
  465. if (pwm <= lm93_pwm_map[freq][i])
  466. break;
  467. /* can fall through with i==13 */
  468. return (u8)i;
  469. }
  470. static int LM93_FAN_FROM_REG(u16 regs)
  471. {
  472. const u16 count = le16_to_cpu(regs) >> 2;
  473. return count==0 ? -1 : count==0x3fff ? 0: 1350000 / count;
  474. }
  475. /*
  476. * RPM: (82.5 to 1350000)
  477. * REG: 14-bits, LE, *left* justified
  478. */
  479. static u16 LM93_FAN_TO_REG(long rpm)
  480. {
  481. u16 count, regs;
  482. if (rpm == 0) {
  483. count = 0x3fff;
  484. } else {
  485. rpm = SENSORS_LIMIT(rpm, 1, 1000000);
  486. count = SENSORS_LIMIT((1350000 + rpm) / rpm, 1, 0x3ffe);
  487. }
  488. regs = count << 2;
  489. return cpu_to_le16(regs);
  490. }
  491. /* PWM FREQ: HZ
  492. REG: 0-7 as mapped below */
  493. static int lm93_pwm_freq_map[8] = {
  494. 22500, 96, 84, 72, 60, 48, 36, 12
  495. };
  496. static int LM93_PWM_FREQ_FROM_REG(u8 reg)
  497. {
  498. return lm93_pwm_freq_map[reg & 0x07];
  499. }
  500. /* round up to nearest match */
  501. static u8 LM93_PWM_FREQ_TO_REG(int freq)
  502. {
  503. int i;
  504. for (i = 7; i > 0; i--)
  505. if (freq <= lm93_pwm_freq_map[i])
  506. break;
  507. /* can fall through with i==0 */
  508. return (u8)i;
  509. }
  510. /* TIME: 1/100 seconds
  511. * REG: 0-7 as mapped below */
  512. static int lm93_spinup_time_map[8] = {
  513. 0, 10, 25, 40, 70, 100, 200, 400,
  514. };
  515. static int LM93_SPINUP_TIME_FROM_REG(u8 reg)
  516. {
  517. return lm93_spinup_time_map[reg >> 5 & 0x07];
  518. }
  519. /* round up to nearest match */
  520. static u8 LM93_SPINUP_TIME_TO_REG(int time)
  521. {
  522. int i;
  523. for (i = 0; i < 7; i++)
  524. if (time <= lm93_spinup_time_map[i])
  525. break;
  526. /* can fall through with i==8 */
  527. return (u8)i;
  528. }
  529. #define LM93_RAMP_MIN 0
  530. #define LM93_RAMP_MAX 75
  531. static int LM93_RAMP_FROM_REG(u8 reg)
  532. {
  533. return (reg & 0x0f) * 5;
  534. }
  535. /* RAMP: 1/100 seconds
  536. REG: 50mS/bit 4-bits right justified */
  537. static u8 LM93_RAMP_TO_REG(int ramp)
  538. {
  539. ramp = SENSORS_LIMIT(ramp, LM93_RAMP_MIN, LM93_RAMP_MAX);
  540. return (u8)((ramp + 2) / 5);
  541. }
  542. /* PROCHOT: 0-255, 0 => 0%, 255 => > 96.6%
  543. * REG: (same) */
  544. static u8 LM93_PROCHOT_TO_REG(long prochot)
  545. {
  546. prochot = SENSORS_LIMIT(prochot, 0, 255);
  547. return (u8)prochot;
  548. }
  549. /* PROCHOT-INTERVAL: 73 - 37200 (1/100 seconds)
  550. * REG: 0-9 as mapped below */
  551. static int lm93_interval_map[10] = {
  552. 73, 146, 290, 580, 1170, 2330, 4660, 9320, 18600, 37200,
  553. };
  554. static int LM93_INTERVAL_FROM_REG(u8 reg)
  555. {
  556. return lm93_interval_map[reg & 0x0f];
  557. }
  558. /* round up to nearest match */
  559. static u8 LM93_INTERVAL_TO_REG(long interval)
  560. {
  561. int i;
  562. for (i = 0; i < 9; i++)
  563. if (interval <= lm93_interval_map[i])
  564. break;
  565. /* can fall through with i==9 */
  566. return (u8)i;
  567. }
  568. /* GPIO: 0-255, GPIO0 is LSB
  569. * REG: inverted */
  570. static unsigned LM93_GPI_FROM_REG(u8 reg)
  571. {
  572. return ~reg & 0xff;
  573. }
  574. /* alarm bitmask definitions
  575. The LM93 has nearly 64 bits of error status... I've pared that down to
  576. what I think is a useful subset in order to fit it into 32 bits.
  577. Especially note that the #VRD_HOT alarms are missing because we provide
  578. that information as values in another sysfs file.
  579. If libsensors is extended to support 64 bit values, this could be revisited.
  580. */
  581. #define LM93_ALARM_IN1 0x00000001
  582. #define LM93_ALARM_IN2 0x00000002
  583. #define LM93_ALARM_IN3 0x00000004
  584. #define LM93_ALARM_IN4 0x00000008
  585. #define LM93_ALARM_IN5 0x00000010
  586. #define LM93_ALARM_IN6 0x00000020
  587. #define LM93_ALARM_IN7 0x00000040
  588. #define LM93_ALARM_IN8 0x00000080
  589. #define LM93_ALARM_IN9 0x00000100
  590. #define LM93_ALARM_IN10 0x00000200
  591. #define LM93_ALARM_IN11 0x00000400
  592. #define LM93_ALARM_IN12 0x00000800
  593. #define LM93_ALARM_IN13 0x00001000
  594. #define LM93_ALARM_IN14 0x00002000
  595. #define LM93_ALARM_IN15 0x00004000
  596. #define LM93_ALARM_IN16 0x00008000
  597. #define LM93_ALARM_FAN1 0x00010000
  598. #define LM93_ALARM_FAN2 0x00020000
  599. #define LM93_ALARM_FAN3 0x00040000
  600. #define LM93_ALARM_FAN4 0x00080000
  601. #define LM93_ALARM_PH1_ERR 0x00100000
  602. #define LM93_ALARM_PH2_ERR 0x00200000
  603. #define LM93_ALARM_SCSI1_ERR 0x00400000
  604. #define LM93_ALARM_SCSI2_ERR 0x00800000
  605. #define LM93_ALARM_DVDDP1_ERR 0x01000000
  606. #define LM93_ALARM_DVDDP2_ERR 0x02000000
  607. #define LM93_ALARM_D1_ERR 0x04000000
  608. #define LM93_ALARM_D2_ERR 0x08000000
  609. #define LM93_ALARM_TEMP1 0x10000000
  610. #define LM93_ALARM_TEMP2 0x20000000
  611. #define LM93_ALARM_TEMP3 0x40000000
  612. static unsigned LM93_ALARMS_FROM_REG(struct block1_t b1)
  613. {
  614. unsigned result;
  615. result = b1.host_status_2 & 0x3f;
  616. if (vccp_limit_type[0])
  617. result |= (b1.host_status_4 & 0x10) << 2;
  618. else
  619. result |= b1.host_status_2 & 0x40;
  620. if (vccp_limit_type[1])
  621. result |= (b1.host_status_4 & 0x20) << 2;
  622. else
  623. result |= b1.host_status_2 & 0x80;
  624. result |= b1.host_status_3 << 8;
  625. result |= (b1.fan_status & 0x0f) << 16;
  626. result |= (b1.p1_prochot_status & 0x80) << 13;
  627. result |= (b1.p2_prochot_status & 0x80) << 14;
  628. result |= (b1.host_status_4 & 0xfc) << 20;
  629. result |= (b1.host_status_1 & 0x07) << 28;
  630. return result;
  631. }
  632. #define MAX_RETRIES 5
  633. static u8 lm93_read_byte(struct i2c_client *client, u8 reg)
  634. {
  635. int value, i;
  636. /* retry in case of read errors */
  637. for (i=1; i<=MAX_RETRIES; i++) {
  638. if ((value = i2c_smbus_read_byte_data(client, reg)) >= 0) {
  639. return value;
  640. } else {
  641. dev_warn(&client->dev,"lm93: read byte data failed, "
  642. "address 0x%02x.\n", reg);
  643. mdelay(i + 3);
  644. }
  645. }
  646. /* <TODO> what to return in case of error? */
  647. dev_err(&client->dev,"lm93: All read byte retries failed!!\n");
  648. return 0;
  649. }
  650. static int lm93_write_byte(struct i2c_client *client, u8 reg, u8 value)
  651. {
  652. int result;
  653. /* <TODO> how to handle write errors? */
  654. result = i2c_smbus_write_byte_data(client, reg, value);
  655. if (result < 0)
  656. dev_warn(&client->dev,"lm93: write byte data failed, "
  657. "0x%02x at address 0x%02x.\n", value, reg);
  658. return result;
  659. }
  660. static u16 lm93_read_word(struct i2c_client *client, u8 reg)
  661. {
  662. int value, i;
  663. /* retry in case of read errors */
  664. for (i=1; i<=MAX_RETRIES; i++) {
  665. if ((value = i2c_smbus_read_word_data(client, reg)) >= 0) {
  666. return value;
  667. } else {
  668. dev_warn(&client->dev,"lm93: read word data failed, "
  669. "address 0x%02x.\n", reg);
  670. mdelay(i + 3);
  671. }
  672. }
  673. /* <TODO> what to return in case of error? */
  674. dev_err(&client->dev,"lm93: All read word retries failed!!\n");
  675. return 0;
  676. }
  677. static int lm93_write_word(struct i2c_client *client, u8 reg, u16 value)
  678. {
  679. int result;
  680. /* <TODO> how to handle write errors? */
  681. result = i2c_smbus_write_word_data(client, reg, value);
  682. if (result < 0)
  683. dev_warn(&client->dev,"lm93: write word data failed, "
  684. "0x%04x at address 0x%02x.\n", value, reg);
  685. return result;
  686. }
  687. static u8 lm93_block_buffer[I2C_SMBUS_BLOCK_MAX];
  688. /*
  689. read block data into values, retry if not expected length
  690. fbn => index to lm93_block_read_cmds table
  691. (Fixed Block Number - section 14.5.2 of LM93 datasheet)
  692. */
  693. static void lm93_read_block(struct i2c_client *client, u8 fbn, u8 *values)
  694. {
  695. int i, result=0;
  696. for (i = 1; i <= MAX_RETRIES; i++) {
  697. result = i2c_smbus_read_block_data(client,
  698. lm93_block_read_cmds[fbn].cmd, lm93_block_buffer);
  699. if (result == lm93_block_read_cmds[fbn].len) {
  700. break;
  701. } else {
  702. dev_warn(&client->dev,"lm93: block read data failed, "
  703. "command 0x%02x.\n",
  704. lm93_block_read_cmds[fbn].cmd);
  705. mdelay(i + 3);
  706. }
  707. }
  708. if (result == lm93_block_read_cmds[fbn].len) {
  709. memcpy(values,lm93_block_buffer,lm93_block_read_cmds[fbn].len);
  710. } else {
  711. /* <TODO> what to do in case of error? */
  712. }
  713. }
  714. static struct lm93_data *lm93_update_device(struct device *dev)
  715. {
  716. struct i2c_client *client = to_i2c_client(dev);
  717. struct lm93_data *data = i2c_get_clientdata(client);
  718. const unsigned long interval = HZ + (HZ / 2);
  719. mutex_lock(&data->update_lock);
  720. if (time_after(jiffies, data->last_updated + interval) ||
  721. !data->valid) {
  722. data->update(data, client);
  723. data->last_updated = jiffies;
  724. data->valid = 1;
  725. }
  726. mutex_unlock(&data->update_lock);
  727. return data;
  728. }
  729. /* update routine for data that has no corresponding SMBus block command */
  730. static void lm93_update_client_common(struct lm93_data *data,
  731. struct i2c_client *client)
  732. {
  733. int i;
  734. u8 *ptr;
  735. /* temp1 - temp4: limits */
  736. for (i = 0; i < 4; i++) {
  737. data->temp_lim[i].min =
  738. lm93_read_byte(client, LM93_REG_TEMP_MIN(i));
  739. data->temp_lim[i].max =
  740. lm93_read_byte(client, LM93_REG_TEMP_MAX(i));
  741. }
  742. /* config register */
  743. data->config = lm93_read_byte(client, LM93_REG_CONFIG);
  744. /* vid1 - vid2: values */
  745. for (i = 0; i < 2; i++)
  746. data->vid[i] = lm93_read_byte(client, LM93_REG_VID(i));
  747. /* prochot1 - prochot2: limits */
  748. for (i = 0; i < 2; i++)
  749. data->prochot_max[i] = lm93_read_byte(client,
  750. LM93_REG_PROCHOT_MAX(i));
  751. /* vccp1 - vccp2: VID relative limits */
  752. for (i = 0; i < 2; i++)
  753. data->vccp_limits[i] = lm93_read_byte(client,
  754. LM93_REG_VCCP_LIMIT_OFF(i));
  755. /* GPIO input state */
  756. data->gpi = lm93_read_byte(client, LM93_REG_GPI);
  757. /* #PROCHOT override state */
  758. data->prochot_override = lm93_read_byte(client,
  759. LM93_REG_PROCHOT_OVERRIDE);
  760. /* #PROCHOT intervals */
  761. data->prochot_interval = lm93_read_byte(client,
  762. LM93_REG_PROCHOT_INTERVAL);
  763. /* Fan Boost Temperature registers */
  764. for (i = 0; i < 4; i++)
  765. data->boost[i] = lm93_read_byte(client, LM93_REG_BOOST(i));
  766. /* Fan Boost Temperature Hyst. registers */
  767. data->boost_hyst[0] = lm93_read_byte(client, LM93_REG_BOOST_HYST_12);
  768. data->boost_hyst[1] = lm93_read_byte(client, LM93_REG_BOOST_HYST_34);
  769. /* Temperature Zone Min. PWM & Hysteresis registers */
  770. data->auto_pwm_min_hyst[0] =
  771. lm93_read_byte(client, LM93_REG_PWM_MIN_HYST_12);
  772. data->auto_pwm_min_hyst[1] =
  773. lm93_read_byte(client, LM93_REG_PWM_MIN_HYST_34);
  774. /* #PROCHOT & #VRDHOT PWM Ramp Control register */
  775. data->pwm_ramp_ctl = lm93_read_byte(client, LM93_REG_PWM_RAMP_CTL);
  776. /* misc setup registers */
  777. data->sfc1 = lm93_read_byte(client, LM93_REG_SFC1);
  778. data->sfc2 = lm93_read_byte(client, LM93_REG_SFC2);
  779. data->sf_tach_to_pwm = lm93_read_byte(client,
  780. LM93_REG_SF_TACH_TO_PWM);
  781. /* write back alarm values to clear */
  782. for (i = 0, ptr = (u8 *)(&data->block1); i < 8; i++)
  783. lm93_write_byte(client, LM93_REG_HOST_ERROR_1 + i, *(ptr + i));
  784. }
  785. /* update routine which uses SMBus block data commands */
  786. static void lm93_update_client_full(struct lm93_data *data,
  787. struct i2c_client *client)
  788. {
  789. dev_dbg(&client->dev,"starting device update (block data enabled)\n");
  790. /* in1 - in16: values & limits */
  791. lm93_read_block(client, 3, (u8 *)(data->block3));
  792. lm93_read_block(client, 7, (u8 *)(data->block7));
  793. /* temp1 - temp4: values */
  794. lm93_read_block(client, 2, (u8 *)(data->block2));
  795. /* prochot1 - prochot2: values */
  796. lm93_read_block(client, 4, (u8 *)(data->block4));
  797. /* fan1 - fan4: values & limits */
  798. lm93_read_block(client, 5, (u8 *)(data->block5));
  799. lm93_read_block(client, 8, (u8 *)(data->block8));
  800. /* pmw control registers */
  801. lm93_read_block(client, 9, (u8 *)(data->block9));
  802. /* alarm values */
  803. lm93_read_block(client, 1, (u8 *)(&data->block1));
  804. /* auto/pwm registers */
  805. lm93_read_block(client, 10, (u8 *)(&data->block10));
  806. lm93_update_client_common(data, client);
  807. }
  808. /* update routine which uses SMBus byte/word data commands only */
  809. static void lm93_update_client_min(struct lm93_data *data,
  810. struct i2c_client *client)
  811. {
  812. int i,j;
  813. u8 *ptr;
  814. dev_dbg(&client->dev,"starting device update (block data disabled)\n");
  815. /* in1 - in16: values & limits */
  816. for (i = 0; i < 16; i++) {
  817. data->block3[i] =
  818. lm93_read_byte(client, LM93_REG_IN(i));
  819. data->block7[i].min =
  820. lm93_read_byte(client, LM93_REG_IN_MIN(i));
  821. data->block7[i].max =
  822. lm93_read_byte(client, LM93_REG_IN_MAX(i));
  823. }
  824. /* temp1 - temp4: values */
  825. for (i = 0; i < 4; i++) {
  826. data->block2[i] =
  827. lm93_read_byte(client, LM93_REG_TEMP(i));
  828. }
  829. /* prochot1 - prochot2: values */
  830. for (i = 0; i < 2; i++) {
  831. data->block4[i].cur =
  832. lm93_read_byte(client, LM93_REG_PROCHOT_CUR(i));
  833. data->block4[i].avg =
  834. lm93_read_byte(client, LM93_REG_PROCHOT_AVG(i));
  835. }
  836. /* fan1 - fan4: values & limits */
  837. for (i = 0; i < 4; i++) {
  838. data->block5[i] =
  839. lm93_read_word(client, LM93_REG_FAN(i));
  840. data->block8[i] =
  841. lm93_read_word(client, LM93_REG_FAN_MIN(i));
  842. }
  843. /* pwm control registers */
  844. for (i = 0; i < 2; i++) {
  845. for (j = 0; j < 4; j++) {
  846. data->block9[i][j] =
  847. lm93_read_byte(client, LM93_REG_PWM_CTL(i,j));
  848. }
  849. }
  850. /* alarm values */
  851. for (i = 0, ptr = (u8 *)(&data->block1); i < 8; i++) {
  852. *(ptr + i) =
  853. lm93_read_byte(client, LM93_REG_HOST_ERROR_1 + i);
  854. }
  855. /* auto/pwm (base temp) registers */
  856. for (i = 0; i < 4; i++) {
  857. data->block10.base[i] =
  858. lm93_read_byte(client, LM93_REG_TEMP_BASE(i));
  859. }
  860. /* auto/pwm (offset temp) registers */
  861. for (i = 0; i < 12; i++) {
  862. data->block10.offset[i] =
  863. lm93_read_byte(client, LM93_REG_TEMP_OFFSET(i));
  864. }
  865. lm93_update_client_common(data, client);
  866. }
  867. /* following are the sysfs callback functions */
  868. static ssize_t show_in(struct device *dev, struct device_attribute *attr,
  869. char *buf)
  870. {
  871. int nr = (to_sensor_dev_attr(attr))->index;
  872. struct lm93_data *data = lm93_update_device(dev);
  873. return sprintf(buf, "%d\n", LM93_IN_FROM_REG(nr, data->block3[nr]));
  874. }
  875. static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_in, NULL, 0);
  876. static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_in, NULL, 1);
  877. static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_in, NULL, 2);
  878. static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, show_in, NULL, 3);
  879. static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, show_in, NULL, 4);
  880. static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, show_in, NULL, 5);
  881. static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, show_in, NULL, 6);
  882. static SENSOR_DEVICE_ATTR(in8_input, S_IRUGO, show_in, NULL, 7);
  883. static SENSOR_DEVICE_ATTR(in9_input, S_IRUGO, show_in, NULL, 8);
  884. static SENSOR_DEVICE_ATTR(in10_input, S_IRUGO, show_in, NULL, 9);
  885. static SENSOR_DEVICE_ATTR(in11_input, S_IRUGO, show_in, NULL, 10);
  886. static SENSOR_DEVICE_ATTR(in12_input, S_IRUGO, show_in, NULL, 11);
  887. static SENSOR_DEVICE_ATTR(in13_input, S_IRUGO, show_in, NULL, 12);
  888. static SENSOR_DEVICE_ATTR(in14_input, S_IRUGO, show_in, NULL, 13);
  889. static SENSOR_DEVICE_ATTR(in15_input, S_IRUGO, show_in, NULL, 14);
  890. static SENSOR_DEVICE_ATTR(in16_input, S_IRUGO, show_in, NULL, 15);
  891. static ssize_t show_in_min(struct device *dev,
  892. struct device_attribute *attr, char *buf)
  893. {
  894. int nr = (to_sensor_dev_attr(attr))->index;
  895. struct lm93_data *data = lm93_update_device(dev);
  896. int vccp = nr - 6;
  897. long rc, vid;
  898. if ((nr==6 || nr==7) && (vccp_limit_type[vccp])) {
  899. vid = LM93_VID_FROM_REG(data->vid[vccp]);
  900. rc = LM93_IN_MIN_FROM_REG(data->vccp_limits[vccp], vid);
  901. }
  902. else {
  903. rc = LM93_IN_FROM_REG(nr, data->block7[nr].min); \
  904. }
  905. return sprintf(buf, "%ld\n", rc); \
  906. }
  907. static ssize_t store_in_min(struct device *dev, struct device_attribute *attr,
  908. const char *buf, size_t count)
  909. {
  910. int nr = (to_sensor_dev_attr(attr))->index;
  911. struct i2c_client *client = to_i2c_client(dev);
  912. struct lm93_data *data = i2c_get_clientdata(client);
  913. u32 val = simple_strtoul(buf, NULL, 10);
  914. int vccp = nr - 6;
  915. long vid;
  916. mutex_lock(&data->update_lock);
  917. if ((nr==6 || nr==7) && (vccp_limit_type[vccp])) {
  918. vid = LM93_VID_FROM_REG(data->vid[vccp]);
  919. data->vccp_limits[vccp] = (data->vccp_limits[vccp] & 0xf0) |
  920. LM93_IN_REL_TO_REG(val, 0, vid);
  921. lm93_write_byte(client, LM93_REG_VCCP_LIMIT_OFF(vccp),
  922. data->vccp_limits[vccp]);
  923. }
  924. else {
  925. data->block7[nr].min = LM93_IN_TO_REG(nr,val);
  926. lm93_write_byte(client, LM93_REG_IN_MIN(nr),
  927. data->block7[nr].min);
  928. }
  929. mutex_unlock(&data->update_lock);
  930. return count;
  931. }
  932. static SENSOR_DEVICE_ATTR(in1_min, S_IWUSR | S_IRUGO,
  933. show_in_min, store_in_min, 0);
  934. static SENSOR_DEVICE_ATTR(in2_min, S_IWUSR | S_IRUGO,
  935. show_in_min, store_in_min, 1);
  936. static SENSOR_DEVICE_ATTR(in3_min, S_IWUSR | S_IRUGO,
  937. show_in_min, store_in_min, 2);
  938. static SENSOR_DEVICE_ATTR(in4_min, S_IWUSR | S_IRUGO,
  939. show_in_min, store_in_min, 3);
  940. static SENSOR_DEVICE_ATTR(in5_min, S_IWUSR | S_IRUGO,
  941. show_in_min, store_in_min, 4);
  942. static SENSOR_DEVICE_ATTR(in6_min, S_IWUSR | S_IRUGO,
  943. show_in_min, store_in_min, 5);
  944. static SENSOR_DEVICE_ATTR(in7_min, S_IWUSR | S_IRUGO,
  945. show_in_min, store_in_min, 6);
  946. static SENSOR_DEVICE_ATTR(in8_min, S_IWUSR | S_IRUGO,
  947. show_in_min, store_in_min, 7);
  948. static SENSOR_DEVICE_ATTR(in9_min, S_IWUSR | S_IRUGO,
  949. show_in_min, store_in_min, 8);
  950. static SENSOR_DEVICE_ATTR(in10_min, S_IWUSR | S_IRUGO,
  951. show_in_min, store_in_min, 9);
  952. static SENSOR_DEVICE_ATTR(in11_min, S_IWUSR | S_IRUGO,
  953. show_in_min, store_in_min, 10);
  954. static SENSOR_DEVICE_ATTR(in12_min, S_IWUSR | S_IRUGO,
  955. show_in_min, store_in_min, 11);
  956. static SENSOR_DEVICE_ATTR(in13_min, S_IWUSR | S_IRUGO,
  957. show_in_min, store_in_min, 12);
  958. static SENSOR_DEVICE_ATTR(in14_min, S_IWUSR | S_IRUGO,
  959. show_in_min, store_in_min, 13);
  960. static SENSOR_DEVICE_ATTR(in15_min, S_IWUSR | S_IRUGO,
  961. show_in_min, store_in_min, 14);
  962. static SENSOR_DEVICE_ATTR(in16_min, S_IWUSR | S_IRUGO,
  963. show_in_min, store_in_min, 15);
  964. static ssize_t show_in_max(struct device *dev,
  965. struct device_attribute *attr, char *buf)
  966. {
  967. int nr = (to_sensor_dev_attr(attr))->index;
  968. struct lm93_data *data = lm93_update_device(dev);
  969. int vccp = nr - 6;
  970. long rc, vid;
  971. if ((nr==6 || nr==7) && (vccp_limit_type[vccp])) {
  972. vid = LM93_VID_FROM_REG(data->vid[vccp]);
  973. rc = LM93_IN_MAX_FROM_REG(data->vccp_limits[vccp],vid);
  974. }
  975. else {
  976. rc = LM93_IN_FROM_REG(nr,data->block7[nr].max); \
  977. }
  978. return sprintf(buf,"%ld\n",rc); \
  979. }
  980. static ssize_t store_in_max(struct device *dev, struct device_attribute *attr,
  981. const char *buf, size_t count)
  982. {
  983. int nr = (to_sensor_dev_attr(attr))->index;
  984. struct i2c_client *client = to_i2c_client(dev);
  985. struct lm93_data *data = i2c_get_clientdata(client);
  986. u32 val = simple_strtoul(buf, NULL, 10);
  987. int vccp = nr - 6;
  988. long vid;
  989. mutex_lock(&data->update_lock);
  990. if ((nr==6 || nr==7) && (vccp_limit_type[vccp])) {
  991. vid = LM93_VID_FROM_REG(data->vid[vccp]);
  992. data->vccp_limits[vccp] = (data->vccp_limits[vccp] & 0x0f) |
  993. LM93_IN_REL_TO_REG(val, 1, vid);
  994. lm93_write_byte(client, LM93_REG_VCCP_LIMIT_OFF(vccp),
  995. data->vccp_limits[vccp]);
  996. }
  997. else {
  998. data->block7[nr].max = LM93_IN_TO_REG(nr,val);
  999. lm93_write_byte(client, LM93_REG_IN_MAX(nr),
  1000. data->block7[nr].max);
  1001. }
  1002. mutex_unlock(&data->update_lock);
  1003. return count;
  1004. }
  1005. static SENSOR_DEVICE_ATTR(in1_max, S_IWUSR | S_IRUGO,
  1006. show_in_max, store_in_max, 0);
  1007. static SENSOR_DEVICE_ATTR(in2_max, S_IWUSR | S_IRUGO,
  1008. show_in_max, store_in_max, 1);
  1009. static SENSOR_DEVICE_ATTR(in3_max, S_IWUSR | S_IRUGO,
  1010. show_in_max, store_in_max, 2);
  1011. static SENSOR_DEVICE_ATTR(in4_max, S_IWUSR | S_IRUGO,
  1012. show_in_max, store_in_max, 3);
  1013. static SENSOR_DEVICE_ATTR(in5_max, S_IWUSR | S_IRUGO,
  1014. show_in_max, store_in_max, 4);
  1015. static SENSOR_DEVICE_ATTR(in6_max, S_IWUSR | S_IRUGO,
  1016. show_in_max, store_in_max, 5);
  1017. static SENSOR_DEVICE_ATTR(in7_max, S_IWUSR | S_IRUGO,
  1018. show_in_max, store_in_max, 6);
  1019. static SENSOR_DEVICE_ATTR(in8_max, S_IWUSR | S_IRUGO,
  1020. show_in_max, store_in_max, 7);
  1021. static SENSOR_DEVICE_ATTR(in9_max, S_IWUSR | S_IRUGO,
  1022. show_in_max, store_in_max, 8);
  1023. static SENSOR_DEVICE_ATTR(in10_max, S_IWUSR | S_IRUGO,
  1024. show_in_max, store_in_max, 9);
  1025. static SENSOR_DEVICE_ATTR(in11_max, S_IWUSR | S_IRUGO,
  1026. show_in_max, store_in_max, 10);
  1027. static SENSOR_DEVICE_ATTR(in12_max, S_IWUSR | S_IRUGO,
  1028. show_in_max, store_in_max, 11);
  1029. static SENSOR_DEVICE_ATTR(in13_max, S_IWUSR | S_IRUGO,
  1030. show_in_max, store_in_max, 12);
  1031. static SENSOR_DEVICE_ATTR(in14_max, S_IWUSR | S_IRUGO,
  1032. show_in_max, store_in_max, 13);
  1033. static SENSOR_DEVICE_ATTR(in15_max, S_IWUSR | S_IRUGO,
  1034. show_in_max, store_in_max, 14);
  1035. static SENSOR_DEVICE_ATTR(in16_max, S_IWUSR | S_IRUGO,
  1036. show_in_max, store_in_max, 15);
  1037. static ssize_t show_temp(struct device *dev,
  1038. struct device_attribute *attr, char *buf)
  1039. {
  1040. int nr = (to_sensor_dev_attr(attr))->index;
  1041. struct lm93_data *data = lm93_update_device(dev);
  1042. return sprintf(buf,"%d\n",LM93_TEMP_FROM_REG(data->block2[nr]));
  1043. }
  1044. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
  1045. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
  1046. static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
  1047. static ssize_t show_temp_min(struct device *dev,
  1048. struct device_attribute *attr, char *buf)
  1049. {
  1050. int nr = (to_sensor_dev_attr(attr))->index;
  1051. struct lm93_data *data = lm93_update_device(dev);
  1052. return sprintf(buf,"%d\n",LM93_TEMP_FROM_REG(data->temp_lim[nr].min));
  1053. }
  1054. static ssize_t store_temp_min(struct device *dev, struct device_attribute *attr,
  1055. const char *buf, size_t count)
  1056. {
  1057. int nr = (to_sensor_dev_attr(attr))->index;
  1058. struct i2c_client *client = to_i2c_client(dev);
  1059. struct lm93_data *data = i2c_get_clientdata(client);
  1060. long val = simple_strtol(buf, NULL, 10);
  1061. mutex_lock(&data->update_lock);
  1062. data->temp_lim[nr].min = LM93_TEMP_TO_REG(val);
  1063. lm93_write_byte(client, LM93_REG_TEMP_MIN(nr), data->temp_lim[nr].min);
  1064. mutex_unlock(&data->update_lock);
  1065. return count;
  1066. }
  1067. static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO,
  1068. show_temp_min, store_temp_min, 0);
  1069. static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO,
  1070. show_temp_min, store_temp_min, 1);
  1071. static SENSOR_DEVICE_ATTR(temp3_min, S_IWUSR | S_IRUGO,
  1072. show_temp_min, store_temp_min, 2);
  1073. static ssize_t show_temp_max(struct device *dev,
  1074. struct device_attribute *attr, char *buf)
  1075. {
  1076. int nr = (to_sensor_dev_attr(attr))->index;
  1077. struct lm93_data *data = lm93_update_device(dev);
  1078. return sprintf(buf,"%d\n",LM93_TEMP_FROM_REG(data->temp_lim[nr].max));
  1079. }
  1080. static ssize_t store_temp_max(struct device *dev, struct device_attribute *attr,
  1081. const char *buf, size_t count)
  1082. {
  1083. int nr = (to_sensor_dev_attr(attr))->index;
  1084. struct i2c_client *client = to_i2c_client(dev);
  1085. struct lm93_data *data = i2c_get_clientdata(client);
  1086. long val = simple_strtol(buf, NULL, 10);
  1087. mutex_lock(&data->update_lock);
  1088. data->temp_lim[nr].max = LM93_TEMP_TO_REG(val);
  1089. lm93_write_byte(client, LM93_REG_TEMP_MAX(nr), data->temp_lim[nr].max);
  1090. mutex_unlock(&data->update_lock);
  1091. return count;
  1092. }
  1093. static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
  1094. show_temp_max, store_temp_max, 0);
  1095. static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO,
  1096. show_temp_max, store_temp_max, 1);
  1097. static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO,
  1098. show_temp_max, store_temp_max, 2);
  1099. static ssize_t show_temp_auto_base(struct device *dev,
  1100. struct device_attribute *attr, char *buf)
  1101. {
  1102. int nr = (to_sensor_dev_attr(attr))->index;
  1103. struct lm93_data *data = lm93_update_device(dev);
  1104. return sprintf(buf,"%d\n",LM93_TEMP_FROM_REG(data->block10.base[nr]));
  1105. }
  1106. static ssize_t store_temp_auto_base(struct device *dev,
  1107. struct device_attribute *attr,
  1108. const char *buf, size_t count)
  1109. {
  1110. int nr = (to_sensor_dev_attr(attr))->index;
  1111. struct i2c_client *client = to_i2c_client(dev);
  1112. struct lm93_data *data = i2c_get_clientdata(client);
  1113. long val = simple_strtol(buf, NULL, 10);
  1114. mutex_lock(&data->update_lock);
  1115. data->block10.base[nr] = LM93_TEMP_TO_REG(val);
  1116. lm93_write_byte(client, LM93_REG_TEMP_BASE(nr), data->block10.base[nr]);
  1117. mutex_unlock(&data->update_lock);
  1118. return count;
  1119. }
  1120. static SENSOR_DEVICE_ATTR(temp1_auto_base, S_IWUSR | S_IRUGO,
  1121. show_temp_auto_base, store_temp_auto_base, 0);
  1122. static SENSOR_DEVICE_ATTR(temp2_auto_base, S_IWUSR | S_IRUGO,
  1123. show_temp_auto_base, store_temp_auto_base, 1);
  1124. static SENSOR_DEVICE_ATTR(temp3_auto_base, S_IWUSR | S_IRUGO,
  1125. show_temp_auto_base, store_temp_auto_base, 2);
  1126. static ssize_t show_temp_auto_boost(struct device *dev,
  1127. struct device_attribute *attr,char *buf)
  1128. {
  1129. int nr = (to_sensor_dev_attr(attr))->index;
  1130. struct lm93_data *data = lm93_update_device(dev);
  1131. return sprintf(buf,"%d\n",LM93_TEMP_FROM_REG(data->boost[nr]));
  1132. }
  1133. static ssize_t store_temp_auto_boost(struct device *dev,
  1134. struct device_attribute *attr,
  1135. const char *buf, size_t count)
  1136. {
  1137. int nr = (to_sensor_dev_attr(attr))->index;
  1138. struct i2c_client *client = to_i2c_client(dev);
  1139. struct lm93_data *data = i2c_get_clientdata(client);
  1140. long val = simple_strtol(buf, NULL, 10);
  1141. mutex_lock(&data->update_lock);
  1142. data->boost[nr] = LM93_TEMP_TO_REG(val);
  1143. lm93_write_byte(client, LM93_REG_BOOST(nr), data->boost[nr]);
  1144. mutex_unlock(&data->update_lock);
  1145. return count;
  1146. }
  1147. static SENSOR_DEVICE_ATTR(temp1_auto_boost, S_IWUSR | S_IRUGO,
  1148. show_temp_auto_boost, store_temp_auto_boost, 0);
  1149. static SENSOR_DEVICE_ATTR(temp2_auto_boost, S_IWUSR | S_IRUGO,
  1150. show_temp_auto_boost, store_temp_auto_boost, 1);
  1151. static SENSOR_DEVICE_ATTR(temp3_auto_boost, S_IWUSR | S_IRUGO,
  1152. show_temp_auto_boost, store_temp_auto_boost, 2);
  1153. static ssize_t show_temp_auto_boost_hyst(struct device *dev,
  1154. struct device_attribute *attr,
  1155. char *buf)
  1156. {
  1157. int nr = (to_sensor_dev_attr(attr))->index;
  1158. struct lm93_data *data = lm93_update_device(dev);
  1159. int mode = LM93_TEMP_OFFSET_MODE_FROM_REG(data->sfc2, nr);
  1160. return sprintf(buf,"%d\n",
  1161. LM93_AUTO_BOOST_HYST_FROM_REGS(data, nr, mode));
  1162. }
  1163. static ssize_t store_temp_auto_boost_hyst(struct device *dev,
  1164. struct device_attribute *attr,
  1165. const char *buf, size_t count)
  1166. {
  1167. int nr = (to_sensor_dev_attr(attr))->index;
  1168. struct i2c_client *client = to_i2c_client(dev);
  1169. struct lm93_data *data = i2c_get_clientdata(client);
  1170. u32 val = simple_strtoul(buf, NULL, 10);
  1171. mutex_lock(&data->update_lock);
  1172. /* force 0.5C/bit mode */
  1173. data->sfc2 = lm93_read_byte(client, LM93_REG_SFC2);
  1174. data->sfc2 |= ((nr < 2) ? 0x10 : 0x20);
  1175. lm93_write_byte(client, LM93_REG_SFC2, data->sfc2);
  1176. data->boost_hyst[nr/2] = LM93_AUTO_BOOST_HYST_TO_REG(data, val, nr, 1);
  1177. lm93_write_byte(client, LM93_REG_BOOST_HYST(nr),
  1178. data->boost_hyst[nr/2]);
  1179. mutex_unlock(&data->update_lock);
  1180. return count;
  1181. }
  1182. static SENSOR_DEVICE_ATTR(temp1_auto_boost_hyst, S_IWUSR | S_IRUGO,
  1183. show_temp_auto_boost_hyst,
  1184. store_temp_auto_boost_hyst, 0);
  1185. static SENSOR_DEVICE_ATTR(temp2_auto_boost_hyst, S_IWUSR | S_IRUGO,
  1186. show_temp_auto_boost_hyst,
  1187. store_temp_auto_boost_hyst, 1);
  1188. static SENSOR_DEVICE_ATTR(temp3_auto_boost_hyst, S_IWUSR | S_IRUGO,
  1189. show_temp_auto_boost_hyst,
  1190. store_temp_auto_boost_hyst, 2);
  1191. static ssize_t show_temp_auto_offset(struct device *dev,
  1192. struct device_attribute *attr, char *buf)
  1193. {
  1194. struct sensor_device_attribute_2 *s_attr = to_sensor_dev_attr_2(attr);
  1195. int nr = s_attr->index;
  1196. int ofs = s_attr->nr;
  1197. struct lm93_data *data = lm93_update_device(dev);
  1198. int mode = LM93_TEMP_OFFSET_MODE_FROM_REG(data->sfc2, nr);
  1199. return sprintf(buf,"%d\n",
  1200. LM93_TEMP_AUTO_OFFSET_FROM_REG(data->block10.offset[ofs],
  1201. nr,mode));
  1202. }
  1203. static ssize_t store_temp_auto_offset(struct device *dev,
  1204. struct device_attribute *attr,
  1205. const char *buf, size_t count)
  1206. {
  1207. struct sensor_device_attribute_2 *s_attr = to_sensor_dev_attr_2(attr);
  1208. int nr = s_attr->index;
  1209. int ofs = s_attr->nr;
  1210. struct i2c_client *client = to_i2c_client(dev);
  1211. struct lm93_data *data = i2c_get_clientdata(client);
  1212. u32 val = simple_strtoul(buf, NULL, 10);
  1213. mutex_lock(&data->update_lock);
  1214. /* force 0.5C/bit mode */
  1215. data->sfc2 = lm93_read_byte(client, LM93_REG_SFC2);
  1216. data->sfc2 |= ((nr < 2) ? 0x10 : 0x20);
  1217. lm93_write_byte(client, LM93_REG_SFC2, data->sfc2);
  1218. data->block10.offset[ofs] = LM93_TEMP_AUTO_OFFSET_TO_REG(
  1219. data->block10.offset[ofs], val, nr, 1);
  1220. lm93_write_byte(client, LM93_REG_TEMP_OFFSET(ofs),
  1221. data->block10.offset[ofs]);
  1222. mutex_unlock(&data->update_lock);
  1223. return count;
  1224. }
  1225. static SENSOR_DEVICE_ATTR_2(temp1_auto_offset1, S_IWUSR | S_IRUGO,
  1226. show_temp_auto_offset, store_temp_auto_offset, 0, 0);
  1227. static SENSOR_DEVICE_ATTR_2(temp1_auto_offset2, S_IWUSR | S_IRUGO,
  1228. show_temp_auto_offset, store_temp_auto_offset, 1, 0);
  1229. static SENSOR_DEVICE_ATTR_2(temp1_auto_offset3, S_IWUSR | S_IRUGO,
  1230. show_temp_auto_offset, store_temp_auto_offset, 2, 0);
  1231. static SENSOR_DEVICE_ATTR_2(temp1_auto_offset4, S_IWUSR | S_IRUGO,
  1232. show_temp_auto_offset, store_temp_auto_offset, 3, 0);
  1233. static SENSOR_DEVICE_ATTR_2(temp1_auto_offset5, S_IWUSR | S_IRUGO,
  1234. show_temp_auto_offset, store_temp_auto_offset, 4, 0);
  1235. static SENSOR_DEVICE_ATTR_2(temp1_auto_offset6, S_IWUSR | S_IRUGO,
  1236. show_temp_auto_offset, store_temp_auto_offset, 5, 0);
  1237. static SENSOR_DEVICE_ATTR_2(temp1_auto_offset7, S_IWUSR | S_IRUGO,
  1238. show_temp_auto_offset, store_temp_auto_offset, 6, 0);
  1239. static SENSOR_DEVICE_ATTR_2(temp1_auto_offset8, S_IWUSR | S_IRUGO,
  1240. show_temp_auto_offset, store_temp_auto_offset, 7, 0);
  1241. static SENSOR_DEVICE_ATTR_2(temp1_auto_offset9, S_IWUSR | S_IRUGO,
  1242. show_temp_auto_offset, store_temp_auto_offset, 8, 0);
  1243. static SENSOR_DEVICE_ATTR_2(temp1_auto_offset10, S_IWUSR | S_IRUGO,
  1244. show_temp_auto_offset, store_temp_auto_offset, 9, 0);
  1245. static SENSOR_DEVICE_ATTR_2(temp1_auto_offset11, S_IWUSR | S_IRUGO,
  1246. show_temp_auto_offset, store_temp_auto_offset, 10, 0);
  1247. static SENSOR_DEVICE_ATTR_2(temp1_auto_offset12, S_IWUSR | S_IRUGO,
  1248. show_temp_auto_offset, store_temp_auto_offset, 11, 0);
  1249. static SENSOR_DEVICE_ATTR_2(temp2_auto_offset1, S_IWUSR | S_IRUGO,
  1250. show_temp_auto_offset, store_temp_auto_offset, 0, 1);
  1251. static SENSOR_DEVICE_ATTR_2(temp2_auto_offset2, S_IWUSR | S_IRUGO,
  1252. show_temp_auto_offset, store_temp_auto_offset, 1, 1);
  1253. static SENSOR_DEVICE_ATTR_2(temp2_auto_offset3, S_IWUSR | S_IRUGO,
  1254. show_temp_auto_offset, store_temp_auto_offset, 2, 1);
  1255. static SENSOR_DEVICE_ATTR_2(temp2_auto_offset4, S_IWUSR | S_IRUGO,
  1256. show_temp_auto_offset, store_temp_auto_offset, 3, 1);
  1257. static SENSOR_DEVICE_ATTR_2(temp2_auto_offset5, S_IWUSR | S_IRUGO,
  1258. show_temp_auto_offset, store_temp_auto_offset, 4, 1);
  1259. static SENSOR_DEVICE_ATTR_2(temp2_auto_offset6, S_IWUSR | S_IRUGO,
  1260. show_temp_auto_offset, store_temp_auto_offset, 5, 1);
  1261. static SENSOR_DEVICE_ATTR_2(temp2_auto_offset7, S_IWUSR | S_IRUGO,
  1262. show_temp_auto_offset, store_temp_auto_offset, 6, 1);
  1263. static SENSOR_DEVICE_ATTR_2(temp2_auto_offset8, S_IWUSR | S_IRUGO,
  1264. show_temp_auto_offset, store_temp_auto_offset, 7, 1);
  1265. static SENSOR_DEVICE_ATTR_2(temp2_auto_offset9, S_IWUSR | S_IRUGO,
  1266. show_temp_auto_offset, store_temp_auto_offset, 8, 1);
  1267. static SENSOR_DEVICE_ATTR_2(temp2_auto_offset10, S_IWUSR | S_IRUGO,
  1268. show_temp_auto_offset, store_temp_auto_offset, 9, 1);
  1269. static SENSOR_DEVICE_ATTR_2(temp2_auto_offset11, S_IWUSR | S_IRUGO,
  1270. show_temp_auto_offset, store_temp_auto_offset, 10, 1);
  1271. static SENSOR_DEVICE_ATTR_2(temp2_auto_offset12, S_IWUSR | S_IRUGO,
  1272. show_temp_auto_offset, store_temp_auto_offset, 11, 1);
  1273. static SENSOR_DEVICE_ATTR_2(temp3_auto_offset1, S_IWUSR | S_IRUGO,
  1274. show_temp_auto_offset, store_temp_auto_offset, 0, 2);
  1275. static SENSOR_DEVICE_ATTR_2(temp3_auto_offset2, S_IWUSR | S_IRUGO,
  1276. show_temp_auto_offset, store_temp_auto_offset, 1, 2);
  1277. static SENSOR_DEVICE_ATTR_2(temp3_auto_offset3, S_IWUSR | S_IRUGO,
  1278. show_temp_auto_offset, store_temp_auto_offset, 2, 2);
  1279. static SENSOR_DEVICE_ATTR_2(temp3_auto_offset4, S_IWUSR | S_IRUGO,
  1280. show_temp_auto_offset, store_temp_auto_offset, 3, 2);
  1281. static SENSOR_DEVICE_ATTR_2(temp3_auto_offset5, S_IWUSR | S_IRUGO,
  1282. show_temp_auto_offset, store_temp_auto_offset, 4, 2);
  1283. static SENSOR_DEVICE_ATTR_2(temp3_auto_offset6, S_IWUSR | S_IRUGO,
  1284. show_temp_auto_offset, store_temp_auto_offset, 5, 2);
  1285. static SENSOR_DEVICE_ATTR_2(temp3_auto_offset7, S_IWUSR | S_IRUGO,
  1286. show_temp_auto_offset, store_temp_auto_offset, 6, 2);
  1287. static SENSOR_DEVICE_ATTR_2(temp3_auto_offset8, S_IWUSR | S_IRUGO,
  1288. show_temp_auto_offset, store_temp_auto_offset, 7, 2);
  1289. static SENSOR_DEVICE_ATTR_2(temp3_auto_offset9, S_IWUSR | S_IRUGO,
  1290. show_temp_auto_offset, store_temp_auto_offset, 8, 2);
  1291. static SENSOR_DEVICE_ATTR_2(temp3_auto_offset10, S_IWUSR | S_IRUGO,
  1292. show_temp_auto_offset, store_temp_auto_offset, 9, 2);
  1293. static SENSOR_DEVICE_ATTR_2(temp3_auto_offset11, S_IWUSR | S_IRUGO,
  1294. show_temp_auto_offset, store_temp_auto_offset, 10, 2);
  1295. static SENSOR_DEVICE_ATTR_2(temp3_auto_offset12, S_IWUSR | S_IRUGO,
  1296. show_temp_auto_offset, store_temp_auto_offset, 11, 2);
  1297. static ssize_t show_temp_auto_pwm_min(struct device *dev,
  1298. struct device_attribute *attr, char *buf)
  1299. {
  1300. int nr = (to_sensor_dev_attr(attr))->index;
  1301. u8 reg, ctl4;
  1302. struct lm93_data *data = lm93_update_device(dev);
  1303. reg = data->auto_pwm_min_hyst[nr/2] >> 4 & 0x0f;
  1304. ctl4 = data->block9[nr][LM93_PWM_CTL4];
  1305. return sprintf(buf,"%d\n",LM93_PWM_FROM_REG(reg, (ctl4 & 0x07) ?
  1306. LM93_PWM_MAP_LO_FREQ : LM93_PWM_MAP_HI_FREQ));
  1307. }
  1308. static ssize_t store_temp_auto_pwm_min(struct device *dev,
  1309. struct device_attribute *attr,
  1310. const char *buf, size_t count)
  1311. {
  1312. int nr = (to_sensor_dev_attr(attr))->index;
  1313. struct i2c_client *client = to_i2c_client(dev);
  1314. struct lm93_data *data = i2c_get_clientdata(client);
  1315. u32 val = simple_strtoul(buf, NULL, 10);
  1316. u8 reg, ctl4;
  1317. mutex_lock(&data->update_lock);
  1318. reg = lm93_read_byte(client, LM93_REG_PWM_MIN_HYST(nr));
  1319. ctl4 = lm93_read_byte(client, LM93_REG_PWM_CTL(nr,LM93_PWM_CTL4));
  1320. reg = (reg & 0x0f) |
  1321. LM93_PWM_TO_REG(val, (ctl4 & 0x07) ?
  1322. LM93_PWM_MAP_LO_FREQ :
  1323. LM93_PWM_MAP_HI_FREQ) << 4;
  1324. data->auto_pwm_min_hyst[nr/2] = reg;
  1325. lm93_write_byte(client, LM93_REG_PWM_MIN_HYST(nr), reg);
  1326. mutex_unlock(&data->update_lock);
  1327. return count;
  1328. }
  1329. static SENSOR_DEVICE_ATTR(temp1_auto_pwm_min, S_IWUSR | S_IRUGO,
  1330. show_temp_auto_pwm_min,
  1331. store_temp_auto_pwm_min, 0);
  1332. static SENSOR_DEVICE_ATTR(temp2_auto_pwm_min, S_IWUSR | S_IRUGO,
  1333. show_temp_auto_pwm_min,
  1334. store_temp_auto_pwm_min, 1);
  1335. static SENSOR_DEVICE_ATTR(temp3_auto_pwm_min, S_IWUSR | S_IRUGO,
  1336. show_temp_auto_pwm_min,
  1337. store_temp_auto_pwm_min, 2);
  1338. static ssize_t show_temp_auto_offset_hyst(struct device *dev,
  1339. struct device_attribute *attr, char *buf)
  1340. {
  1341. int nr = (to_sensor_dev_attr(attr))->index;
  1342. struct lm93_data *data = lm93_update_device(dev);
  1343. int mode = LM93_TEMP_OFFSET_MODE_FROM_REG(data->sfc2, nr);
  1344. return sprintf(buf,"%d\n",LM93_TEMP_OFFSET_FROM_REG(
  1345. data->auto_pwm_min_hyst[nr/2], mode));
  1346. }
  1347. static ssize_t store_temp_auto_offset_hyst(struct device *dev,
  1348. struct device_attribute *attr,
  1349. const char *buf, size_t count)
  1350. {
  1351. int nr = (to_sensor_dev_attr(attr))->index;
  1352. struct i2c_client *client = to_i2c_client(dev);
  1353. struct lm93_data *data = i2c_get_clientdata(client);
  1354. u32 val = simple_strtoul(buf, NULL, 10);
  1355. u8 reg;
  1356. mutex_lock(&data->update_lock);
  1357. /* force 0.5C/bit mode */
  1358. data->sfc2 = lm93_read_byte(client, LM93_REG_SFC2);
  1359. data->sfc2 |= ((nr < 2) ? 0x10 : 0x20);
  1360. lm93_write_byte(client, LM93_REG_SFC2, data->sfc2);
  1361. reg = data->auto_pwm_min_hyst[nr/2];
  1362. reg = (reg & 0xf0) | (LM93_TEMP_OFFSET_TO_REG(val, 1) & 0x0f);
  1363. data->auto_pwm_min_hyst[nr/2] = reg;
  1364. lm93_write_byte(client, LM93_REG_PWM_MIN_HYST(nr), reg);
  1365. mutex_unlock(&data->update_lock);
  1366. return count;
  1367. }
  1368. static SENSOR_DEVICE_ATTR(temp1_auto_offset_hyst, S_IWUSR | S_IRUGO,
  1369. show_temp_auto_offset_hyst,
  1370. store_temp_auto_offset_hyst, 0);
  1371. static SENSOR_DEVICE_ATTR(temp2_auto_offset_hyst, S_IWUSR | S_IRUGO,
  1372. show_temp_auto_offset_hyst,
  1373. store_temp_auto_offset_hyst, 1);
  1374. static SENSOR_DEVICE_ATTR(temp3_auto_offset_hyst, S_IWUSR | S_IRUGO,
  1375. show_temp_auto_offset_hyst,
  1376. store_temp_auto_offset_hyst, 2);
  1377. static ssize_t show_fan_input(struct device *dev,
  1378. struct device_attribute *attr, char *buf)
  1379. {
  1380. struct sensor_device_attribute *s_attr = to_sensor_dev_attr(attr);
  1381. int nr = s_attr->index;
  1382. struct lm93_data *data = lm93_update_device(dev);
  1383. return sprintf(buf,"%d\n",LM93_FAN_FROM_REG(data->block5[nr]));
  1384. }
  1385. static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan_input, NULL, 0);
  1386. static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan_input, NULL, 1);
  1387. static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan_input, NULL, 2);
  1388. static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, show_fan_input, NULL, 3);
  1389. static ssize_t show_fan_min(struct device *dev,
  1390. struct device_attribute *attr, char *buf)
  1391. {
  1392. int nr = (to_sensor_dev_attr(attr))->index;
  1393. struct lm93_data *data = lm93_update_device(dev);
  1394. return sprintf(buf,"%d\n",LM93_FAN_FROM_REG(data->block8[nr]));
  1395. }
  1396. static ssize_t store_fan_min(struct device *dev, struct device_attribute *attr,
  1397. const char *buf, size_t count)
  1398. {
  1399. int nr = (to_sensor_dev_attr(attr))->index;
  1400. struct i2c_client *client = to_i2c_client(dev);
  1401. struct lm93_data *data = i2c_get_clientdata(client);
  1402. u32 val = simple_strtoul(buf, NULL, 10);
  1403. mutex_lock(&data->update_lock);
  1404. data->block8[nr] = LM93_FAN_TO_REG(val);
  1405. lm93_write_word(client,LM93_REG_FAN_MIN(nr),data->block8[nr]);
  1406. mutex_unlock(&data->update_lock);
  1407. return count;
  1408. }
  1409. static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO,
  1410. show_fan_min, store_fan_min, 0);
  1411. static SENSOR_DEVICE_ATTR(fan2_min, S_IWUSR | S_IRUGO,
  1412. show_fan_min, store_fan_min, 1);
  1413. static SENSOR_DEVICE_ATTR(fan3_min, S_IWUSR | S_IRUGO,
  1414. show_fan_min, store_fan_min, 2);
  1415. static SENSOR_DEVICE_ATTR(fan4_min, S_IWUSR | S_IRUGO,
  1416. show_fan_min, store_fan_min, 3);
  1417. /* some tedious bit-twiddling here to deal with the register format:
  1418. data->sf_tach_to_pwm: (tach to pwm mapping bits)
  1419. bit | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0
  1420. T4:P2 T4:P1 T3:P2 T3:P1 T2:P2 T2:P1 T1:P2 T1:P1
  1421. data->sfc2: (enable bits)
  1422. bit | 3 | 2 | 1 | 0
  1423. T4 T3 T2 T1
  1424. */
  1425. static ssize_t show_fan_smart_tach(struct device *dev,
  1426. struct device_attribute *attr, char *buf)
  1427. {
  1428. int nr = (to_sensor_dev_attr(attr))->index;
  1429. struct lm93_data *data = lm93_update_device(dev);
  1430. long rc = 0;
  1431. int mapping;
  1432. /* extract the relevant mapping */
  1433. mapping = (data->sf_tach_to_pwm >> (nr * 2)) & 0x03;
  1434. /* if there's a mapping and it's enabled */
  1435. if (mapping && ((data->sfc2 >> nr) & 0x01))
  1436. rc = mapping;
  1437. return sprintf(buf,"%ld\n",rc);
  1438. }
  1439. /* helper function - must grab data->update_lock before calling
  1440. fan is 0-3, indicating fan1-fan4 */
  1441. static void lm93_write_fan_smart_tach(struct i2c_client *client,
  1442. struct lm93_data *data, int fan, long value)
  1443. {
  1444. /* insert the new mapping and write it out */
  1445. data->sf_tach_to_pwm = lm93_read_byte(client, LM93_REG_SF_TACH_TO_PWM);
  1446. data->sf_tach_to_pwm &= ~(0x3 << fan * 2);
  1447. data->sf_tach_to_pwm |= value << fan * 2;
  1448. lm93_write_byte(client, LM93_REG_SF_TACH_TO_PWM, data->sf_tach_to_pwm);
  1449. /* insert the enable bit and write it out */
  1450. data->sfc2 = lm93_read_byte(client, LM93_REG_SFC2);
  1451. if (value)
  1452. data->sfc2 |= 1 << fan;
  1453. else
  1454. data->sfc2 &= ~(1 << fan);
  1455. lm93_write_byte(client, LM93_REG_SFC2, data->sfc2);
  1456. }
  1457. static ssize_t store_fan_smart_tach(struct device *dev,
  1458. struct device_attribute *attr,
  1459. const char *buf, size_t count)
  1460. {
  1461. int nr = (to_sensor_dev_attr(attr))->index;
  1462. struct i2c_client *client = to_i2c_client(dev);
  1463. struct lm93_data *data = i2c_get_clientdata(client);
  1464. u32 val = simple_strtoul(buf, NULL, 10);
  1465. mutex_lock(&data->update_lock);
  1466. /* sanity test, ignore the write otherwise */
  1467. if (0 <= val && val <= 2) {
  1468. /* can't enable if pwm freq is 22.5KHz */
  1469. if (val) {
  1470. u8 ctl4 = lm93_read_byte(client,
  1471. LM93_REG_PWM_CTL(val-1,LM93_PWM_CTL4));
  1472. if ((ctl4 & 0x07) == 0)
  1473. val = 0;
  1474. }
  1475. lm93_write_fan_smart_tach(client, data, nr, val);
  1476. }
  1477. mutex_unlock(&data->update_lock);
  1478. return count;
  1479. }
  1480. static SENSOR_DEVICE_ATTR(fan1_smart_tach, S_IWUSR | S_IRUGO,
  1481. show_fan_smart_tach, store_fan_smart_tach, 0);
  1482. static SENSOR_DEVICE_ATTR(fan2_smart_tach, S_IWUSR | S_IRUGO,
  1483. show_fan_smart_tach, store_fan_smart_tach, 1);
  1484. static SENSOR_DEVICE_ATTR(fan3_smart_tach, S_IWUSR | S_IRUGO,
  1485. show_fan_smart_tach, store_fan_smart_tach, 2);
  1486. static SENSOR_DEVICE_ATTR(fan4_smart_tach, S_IWUSR | S_IRUGO,
  1487. show_fan_smart_tach, store_fan_smart_tach, 3);
  1488. static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
  1489. char *buf)
  1490. {
  1491. int nr = (to_sensor_dev_attr(attr))->index;
  1492. struct lm93_data *data = lm93_update_device(dev);
  1493. u8 ctl2, ctl4;
  1494. long rc;
  1495. ctl2 = data->block9[nr][LM93_PWM_CTL2];
  1496. ctl4 = data->block9[nr][LM93_PWM_CTL4];
  1497. if (ctl2 & 0x01) /* show user commanded value if enabled */
  1498. rc = data->pwm_override[nr];
  1499. else /* show present h/w value if manual pwm disabled */
  1500. rc = LM93_PWM_FROM_REG(ctl2 >> 4, (ctl4 & 0x07) ?
  1501. LM93_PWM_MAP_LO_FREQ : LM93_PWM_MAP_HI_FREQ);
  1502. return sprintf(buf,"%ld\n",rc);
  1503. }
  1504. static ssize_t store_pwm(struct device *dev, struct device_attribute *attr,
  1505. const char *buf, size_t count)
  1506. {
  1507. int nr = (to_sensor_dev_attr(attr))->index;
  1508. struct i2c_client *client = to_i2c_client(dev);
  1509. struct lm93_data *data = i2c_get_clientdata(client);
  1510. u32 val = simple_strtoul(buf, NULL, 10);
  1511. u8 ctl2, ctl4;
  1512. mutex_lock(&data->update_lock);
  1513. ctl2 = lm93_read_byte(client,LM93_REG_PWM_CTL(nr,LM93_PWM_CTL2));
  1514. ctl4 = lm93_read_byte(client, LM93_REG_PWM_CTL(nr,LM93_PWM_CTL4));
  1515. ctl2 = (ctl2 & 0x0f) | LM93_PWM_TO_REG(val,(ctl4 & 0x07) ?
  1516. LM93_PWM_MAP_LO_FREQ : LM93_PWM_MAP_HI_FREQ) << 4;
  1517. /* save user commanded value */
  1518. data->pwm_override[nr] = LM93_PWM_FROM_REG(ctl2 >> 4,
  1519. (ctl4 & 0x07) ? LM93_PWM_MAP_LO_FREQ :
  1520. LM93_PWM_MAP_HI_FREQ);
  1521. lm93_write_byte(client,LM93_REG_PWM_CTL(nr,LM93_PWM_CTL2),ctl2);
  1522. mutex_unlock(&data->update_lock);
  1523. return count;
  1524. }
  1525. static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 0);
  1526. static SENSOR_DEVICE_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 1);
  1527. static ssize_t show_pwm_enable(struct device *dev,
  1528. struct device_attribute *attr, char *buf)
  1529. {
  1530. int nr = (to_sensor_dev_attr(attr))->index;
  1531. struct lm93_data *data = lm93_update_device(dev);
  1532. u8 ctl2;
  1533. long rc;
  1534. ctl2 = data->block9[nr][LM93_PWM_CTL2];
  1535. if (ctl2 & 0x01) /* manual override enabled ? */
  1536. rc = ((ctl2 & 0xF0) == 0xF0) ? 0 : 1;
  1537. else
  1538. rc = 2;
  1539. return sprintf(buf,"%ld\n",rc);
  1540. }
  1541. static ssize_t store_pwm_enable(struct device *dev,
  1542. struct device_attribute *attr,
  1543. const char *buf, size_t count)
  1544. {
  1545. int nr = (to_sensor_dev_attr(attr))->index;
  1546. struct i2c_client *client = to_i2c_client(dev);
  1547. struct lm93_data *data = i2c_get_clientdata(client);
  1548. u32 val = simple_strtoul(buf, NULL, 10);
  1549. u8 ctl2;
  1550. mutex_lock(&data->update_lock);
  1551. ctl2 = lm93_read_byte(client,LM93_REG_PWM_CTL(nr,LM93_PWM_CTL2));
  1552. switch (val) {
  1553. case 0:
  1554. ctl2 |= 0xF1; /* enable manual override, set PWM to max */
  1555. break;
  1556. case 1: ctl2 |= 0x01; /* enable manual override */
  1557. break;
  1558. case 2: ctl2 &= ~0x01; /* disable manual override */
  1559. break;
  1560. default:
  1561. mutex_unlock(&data->update_lock);
  1562. return -EINVAL;
  1563. }
  1564. lm93_write_byte(client,LM93_REG_PWM_CTL(nr,LM93_PWM_CTL2),ctl2);
  1565. mutex_unlock(&data->update_lock);
  1566. return count;
  1567. }
  1568. static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
  1569. show_pwm_enable, store_pwm_enable, 0);
  1570. static SENSOR_DEVICE_ATTR(pwm2_enable, S_IWUSR | S_IRUGO,
  1571. show_pwm_enable, store_pwm_enable, 1);
  1572. static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
  1573. char *buf)
  1574. {
  1575. int nr = (to_sensor_dev_attr(attr))->index;
  1576. struct lm93_data *data = lm93_update_device(dev);
  1577. u8 ctl4;
  1578. ctl4 = data->block9[nr][LM93_PWM_CTL4];
  1579. return sprintf(buf,"%d\n",LM93_PWM_FREQ_FROM_REG(ctl4));
  1580. }
  1581. /* helper function - must grab data->update_lock before calling
  1582. pwm is 0-1, indicating pwm1-pwm2
  1583. this disables smart tach for all tach channels bound to the given pwm */
  1584. static void lm93_disable_fan_smart_tach(struct i2c_client *client,
  1585. struct lm93_data *data, int pwm)
  1586. {
  1587. int mapping = lm93_read_byte(client, LM93_REG_SF_TACH_TO_PWM);
  1588. int mask;
  1589. /* collapse the mapping into a mask of enable bits */
  1590. mapping = (mapping >> pwm) & 0x55;
  1591. mask = mapping & 0x01;
  1592. mask |= (mapping & 0x04) >> 1;
  1593. mask |= (mapping & 0x10) >> 2;
  1594. mask |= (mapping & 0x40) >> 3;
  1595. /* disable smart tach according to the mask */
  1596. data->sfc2 = lm93_read_byte(client, LM93_REG_SFC2);
  1597. data->sfc2 &= ~mask;
  1598. lm93_write_byte(client, LM93_REG_SFC2, data->sfc2);
  1599. }
  1600. static ssize_t store_pwm_freq(struct device *dev,
  1601. struct device_attribute *attr,
  1602. const char *buf, size_t count)
  1603. {
  1604. int nr = (to_sensor_dev_attr(attr))->index;
  1605. struct i2c_client *client = to_i2c_client(dev);
  1606. struct lm93_data *data = i2c_get_clientdata(client);
  1607. u32 val = simple_strtoul(buf, NULL, 10);
  1608. u8 ctl4;
  1609. mutex_lock(&data->update_lock);
  1610. ctl4 = lm93_read_byte(client,LM93_REG_PWM_CTL(nr,LM93_PWM_CTL4));
  1611. ctl4 = (ctl4 & 0xf8) | LM93_PWM_FREQ_TO_REG(val);
  1612. data->block9[nr][LM93_PWM_CTL4] = ctl4;
  1613. /* ctl4 == 0 -> 22.5KHz -> disable smart tach */
  1614. if (!ctl4)
  1615. lm93_disable_fan_smart_tach(client, data, nr);
  1616. lm93_write_byte(client, LM93_REG_PWM_CTL(nr,LM93_PWM_CTL4), ctl4);
  1617. mutex_unlock(&data->update_lock);
  1618. return count;
  1619. }
  1620. static SENSOR_DEVICE_ATTR(pwm1_freq, S_IWUSR | S_IRUGO,
  1621. show_pwm_freq, store_pwm_freq, 0);
  1622. static SENSOR_DEVICE_ATTR(pwm2_freq, S_IWUSR | S_IRUGO,
  1623. show_pwm_freq, store_pwm_freq, 1);
  1624. static ssize_t show_pwm_auto_channels(struct device *dev,
  1625. struct device_attribute *attr, char *buf)
  1626. {
  1627. int nr = (to_sensor_dev_attr(attr))->index;
  1628. struct lm93_data *data = lm93_update_device(dev);
  1629. return sprintf(buf,"%d\n",data->block9[nr][LM93_PWM_CTL1]);
  1630. }
  1631. static ssize_t store_pwm_auto_channels(struct device *dev,
  1632. struct device_attribute *attr,
  1633. const char *buf, size_t count)
  1634. {
  1635. int nr = (to_sensor_dev_attr(attr))->index;
  1636. struct i2c_client *client = to_i2c_client(dev);
  1637. struct lm93_data *data = i2c_get_clientdata(client);
  1638. u32 val = simple_strtoul(buf, NULL, 10);
  1639. mutex_lock(&data->update_lock);
  1640. data->block9[nr][LM93_PWM_CTL1] = SENSORS_LIMIT(val, 0, 255);
  1641. lm93_write_byte(client, LM93_REG_PWM_CTL(nr,LM93_PWM_CTL1),
  1642. data->block9[nr][LM93_PWM_CTL1]);
  1643. mutex_unlock(&data->update_lock);
  1644. return count;
  1645. }
  1646. static SENSOR_DEVICE_ATTR(pwm1_auto_channels, S_IWUSR | S_IRUGO,
  1647. show_pwm_auto_channels, store_pwm_auto_channels, 0);
  1648. static SENSOR_DEVICE_ATTR(pwm2_auto_channels, S_IWUSR | S_IRUGO,
  1649. show_pwm_auto_channels, store_pwm_auto_channels, 1);
  1650. static ssize_t show_pwm_auto_spinup_min(struct device *dev,
  1651. struct device_attribute *attr,char *buf)
  1652. {
  1653. int nr = (to_sensor_dev_attr(attr))->index;
  1654. struct lm93_data *data = lm93_update_device(dev);
  1655. u8 ctl3, ctl4;
  1656. ctl3 = data->block9[nr][LM93_PWM_CTL3];
  1657. ctl4 = data->block9[nr][LM93_PWM_CTL4];
  1658. return sprintf(buf,"%d\n",
  1659. LM93_PWM_FROM_REG(ctl3 & 0x0f, (ctl4 & 0x07) ?
  1660. LM93_PWM_MAP_LO_FREQ : LM93_PWM_MAP_HI_FREQ));
  1661. }
  1662. static ssize_t store_pwm_auto_spinup_min(struct device *dev,
  1663. struct device_attribute *attr,
  1664. const char *buf, size_t count)
  1665. {
  1666. int nr = (to_sensor_dev_attr(attr))->index;
  1667. struct i2c_client *client = to_i2c_client(dev);
  1668. struct lm93_data *data = i2c_get_clientdata(client);
  1669. u32 val = simple_strtoul(buf, NULL, 10);
  1670. u8 ctl3, ctl4;
  1671. mutex_lock(&data->update_lock);
  1672. ctl3 = lm93_read_byte(client,LM93_REG_PWM_CTL(nr, LM93_PWM_CTL3));
  1673. ctl4 = lm93_read_byte(client,LM93_REG_PWM_CTL(nr, LM93_PWM_CTL4));
  1674. ctl3 = (ctl3 & 0xf0) | LM93_PWM_TO_REG(val, (ctl4 & 0x07) ?
  1675. LM93_PWM_MAP_LO_FREQ :
  1676. LM93_PWM_MAP_HI_FREQ);
  1677. data->block9[nr][LM93_PWM_CTL3] = ctl3;
  1678. lm93_write_byte(client,LM93_REG_PWM_CTL(nr, LM93_PWM_CTL3), ctl3);
  1679. mutex_unlock(&data->update_lock);
  1680. return count;
  1681. }
  1682. static SENSOR_DEVICE_ATTR(pwm1_auto_spinup_min, S_IWUSR | S_IRUGO,
  1683. show_pwm_auto_spinup_min,
  1684. store_pwm_auto_spinup_min, 0);
  1685. static SENSOR_DEVICE_ATTR(pwm2_auto_spinup_min, S_IWUSR | S_IRUGO,
  1686. show_pwm_auto_spinup_min,
  1687. store_pwm_auto_spinup_min, 1);
  1688. static ssize_t show_pwm_auto_spinup_time(struct device *dev,
  1689. struct device_attribute *attr, char *buf)
  1690. {
  1691. int nr = (to_sensor_dev_attr(attr))->index;
  1692. struct lm93_data *data = lm93_update_device(dev);
  1693. return sprintf(buf,"%d\n",LM93_SPINUP_TIME_FROM_REG(
  1694. data->block9[nr][LM93_PWM_CTL3]));
  1695. }
  1696. static ssize_t store_pwm_auto_spinup_time(struct device *dev,
  1697. struct device_attribute *attr,
  1698. const char *buf, size_t count)
  1699. {
  1700. int nr = (to_sensor_dev_attr(attr))->index;
  1701. struct i2c_client *client = to_i2c_client(dev);
  1702. struct lm93_data *data = i2c_get_clientdata(client);
  1703. u32 val = simple_strtoul(buf, NULL, 10);
  1704. u8 ctl3;
  1705. mutex_lock(&data->update_lock);
  1706. ctl3 = lm93_read_byte(client,LM93_REG_PWM_CTL(nr, LM93_PWM_CTL3));
  1707. ctl3 = (ctl3 & 0x1f) | (LM93_SPINUP_TIME_TO_REG(val) << 5 & 0xe0);
  1708. data->block9[nr][LM93_PWM_CTL3] = ctl3;
  1709. lm93_write_byte(client,LM93_REG_PWM_CTL(nr, LM93_PWM_CTL3), ctl3);
  1710. mutex_unlock(&data->update_lock);
  1711. return count;
  1712. }
  1713. static SENSOR_DEVICE_ATTR(pwm1_auto_spinup_time, S_IWUSR | S_IRUGO,
  1714. show_pwm_auto_spinup_time,
  1715. store_pwm_auto_spinup_time, 0);
  1716. static SENSOR_DEVICE_ATTR(pwm2_auto_spinup_time, S_IWUSR | S_IRUGO,
  1717. show_pwm_auto_spinup_time,
  1718. store_pwm_auto_spinup_time, 1);
  1719. static ssize_t show_pwm_auto_prochot_ramp(struct device *dev,
  1720. struct device_attribute *attr, char *buf)
  1721. {
  1722. struct lm93_data *data = lm93_update_device(dev);
  1723. return sprintf(buf,"%d\n",
  1724. LM93_RAMP_FROM_REG(data->pwm_ramp_ctl >> 4 & 0x0f));
  1725. }
  1726. static ssize_t store_pwm_auto_prochot_ramp(struct device *dev,
  1727. struct device_attribute *attr,
  1728. const char *buf, size_t count)
  1729. {
  1730. struct i2c_client *client = to_i2c_client(dev);
  1731. struct lm93_data *data = i2c_get_clientdata(client);
  1732. u32 val = simple_strtoul(buf, NULL, 10);
  1733. u8 ramp;
  1734. mutex_lock(&data->update_lock);
  1735. ramp = lm93_read_byte(client, LM93_REG_PWM_RAMP_CTL);
  1736. ramp = (ramp & 0x0f) | (LM93_RAMP_TO_REG(val) << 4 & 0xf0);
  1737. lm93_write_byte(client, LM93_REG_PWM_RAMP_CTL, ramp);
  1738. mutex_unlock(&data->update_lock);
  1739. return count;
  1740. }
  1741. static DEVICE_ATTR(pwm_auto_prochot_ramp, S_IRUGO | S_IWUSR,
  1742. show_pwm_auto_prochot_ramp,
  1743. store_pwm_auto_prochot_ramp);
  1744. static ssize_t show_pwm_auto_vrdhot_ramp(struct device *dev,
  1745. struct device_attribute *attr, char *buf)
  1746. {
  1747. struct lm93_data *data = lm93_update_device(dev);
  1748. return sprintf(buf,"%d\n",
  1749. LM93_RAMP_FROM_REG(data->pwm_ramp_ctl & 0x0f));
  1750. }
  1751. static ssize_t store_pwm_auto_vrdhot_ramp(struct device *dev,
  1752. struct device_attribute *attr,
  1753. const char *buf, size_t count)
  1754. {
  1755. struct i2c_client *client = to_i2c_client(dev);
  1756. struct lm93_data *data = i2c_get_clientdata(client);
  1757. u32 val = simple_strtoul(buf, NULL, 10);
  1758. u8 ramp;
  1759. mutex_lock(&data->update_lock);
  1760. ramp = lm93_read_byte(client, LM93_REG_PWM_RAMP_CTL);
  1761. ramp = (ramp & 0xf0) | (LM93_RAMP_TO_REG(val) & 0x0f);
  1762. lm93_write_byte(client, LM93_REG_PWM_RAMP_CTL, ramp);
  1763. mutex_unlock(&data->update_lock);
  1764. return 0;
  1765. }
  1766. static DEVICE_ATTR(pwm_auto_vrdhot_ramp, S_IRUGO | S_IWUSR,
  1767. show_pwm_auto_vrdhot_ramp,
  1768. store_pwm_auto_vrdhot_ramp);
  1769. static ssize_t show_vid(struct device *dev, struct device_attribute *attr,
  1770. char *buf)
  1771. {
  1772. int nr = (to_sensor_dev_attr(attr))->index;
  1773. struct lm93_data *data = lm93_update_device(dev);
  1774. return sprintf(buf,"%d\n",LM93_VID_FROM_REG(data->vid[nr]));
  1775. }
  1776. static SENSOR_DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL, 0);
  1777. static SENSOR_DEVICE_ATTR(cpu1_vid, S_IRUGO, show_vid, NULL, 1);
  1778. static ssize_t show_prochot(struct device *dev, struct device_attribute *attr,
  1779. char *buf)
  1780. {
  1781. int nr = (to_sensor_dev_attr(attr))->index;
  1782. struct lm93_data *data = lm93_update_device(dev);
  1783. return sprintf(buf,"%d\n",data->block4[nr].cur);
  1784. }
  1785. static SENSOR_DEVICE_ATTR(prochot1, S_IRUGO, show_prochot, NULL, 0);
  1786. static SENSOR_DEVICE_ATTR(prochot2, S_IRUGO, show_prochot, NULL, 1);
  1787. static ssize_t show_prochot_avg(struct device *dev,
  1788. struct device_attribute *attr, char *buf)
  1789. {
  1790. int nr = (to_sensor_dev_attr(attr))->index;
  1791. struct lm93_data *data = lm93_update_device(dev);
  1792. return sprintf(buf,"%d\n",data->block4[nr].avg);
  1793. }
  1794. static SENSOR_DEVICE_ATTR(prochot1_avg, S_IRUGO, show_prochot_avg, NULL, 0);
  1795. static SENSOR_DEVICE_ATTR(prochot2_avg, S_IRUGO, show_prochot_avg, NULL, 1);
  1796. static ssize_t show_prochot_max(struct device *dev,
  1797. struct device_attribute *attr, char *buf)
  1798. {
  1799. int nr = (to_sensor_dev_attr(attr))->index;
  1800. struct lm93_data *data = lm93_update_device(dev);
  1801. return sprintf(buf,"%d\n",data->prochot_max[nr]);
  1802. }
  1803. static ssize_t store_prochot_max(struct device *dev,
  1804. struct device_attribute *attr,
  1805. const char *buf, size_t count)
  1806. {
  1807. int nr = (to_sensor_dev_attr(attr))->index;
  1808. struct i2c_client *client = to_i2c_client(dev);
  1809. struct lm93_data *data = i2c_get_clientdata(client);
  1810. u32 val = simple_strtoul(buf, NULL, 10);
  1811. mutex_lock(&data->update_lock);
  1812. data->prochot_max[nr] = LM93_PROCHOT_TO_REG(val);
  1813. lm93_write_byte(client, LM93_REG_PROCHOT_MAX(nr),
  1814. data->prochot_max[nr]);
  1815. mutex_unlock(&data->update_lock);
  1816. return count;
  1817. }
  1818. static SENSOR_DEVICE_ATTR(prochot1_max, S_IWUSR | S_IRUGO,
  1819. show_prochot_max, store_prochot_max, 0);
  1820. static SENSOR_DEVICE_ATTR(prochot2_max, S_IWUSR | S_IRUGO,
  1821. show_prochot_max, store_prochot_max, 1);
  1822. static const u8 prochot_override_mask[] = { 0x80, 0x40 };
  1823. static ssize_t show_prochot_override(struct device *dev,
  1824. struct device_attribute *attr, char *buf)
  1825. {
  1826. int nr = (to_sensor_dev_attr(attr))->index;
  1827. struct lm93_data *data = lm93_update_device(dev);
  1828. return sprintf(buf,"%d\n",
  1829. (data->prochot_override & prochot_override_mask[nr]) ? 1 : 0);
  1830. }
  1831. static ssize_t store_prochot_override(struct device *dev,
  1832. struct device_attribute *attr,
  1833. const char *buf, size_t count)
  1834. {
  1835. int nr = (to_sensor_dev_attr(attr))->index;
  1836. struct i2c_client *client = to_i2c_client(dev);
  1837. struct lm93_data *data = i2c_get_clientdata(client);
  1838. u32 val = simple_strtoul(buf, NULL, 10);
  1839. mutex_lock(&data->update_lock);
  1840. if (val)
  1841. data->prochot_override |= prochot_override_mask[nr];
  1842. else
  1843. data->prochot_override &= (~prochot_override_mask[nr]);
  1844. lm93_write_byte(client, LM93_REG_PROCHOT_OVERRIDE,
  1845. data->prochot_override);
  1846. mutex_unlock(&data->update_lock);
  1847. return count;
  1848. }
  1849. static SENSOR_DEVICE_ATTR(prochot1_override, S_IWUSR | S_IRUGO,
  1850. show_prochot_override, store_prochot_override, 0);
  1851. static SENSOR_DEVICE_ATTR(prochot2_override, S_IWUSR | S_IRUGO,
  1852. show_prochot_override, store_prochot_override, 1);
  1853. static ssize_t show_prochot_interval(struct device *dev,
  1854. struct device_attribute *attr, char *buf)
  1855. {
  1856. int nr = (to_sensor_dev_attr(attr))->index;
  1857. struct lm93_data *data = lm93_update_device(dev);
  1858. u8 tmp;
  1859. if (nr==1)
  1860. tmp = (data->prochot_interval & 0xf0) >> 4;
  1861. else
  1862. tmp = data->prochot_interval & 0x0f;
  1863. return sprintf(buf,"%d\n",LM93_INTERVAL_FROM_REG(tmp));
  1864. }
  1865. static ssize_t store_prochot_interval(struct device *dev,
  1866. struct device_attribute *attr,
  1867. const char *buf, size_t count)
  1868. {
  1869. int nr = (to_sensor_dev_attr(attr))->index;
  1870. struct i2c_client *client = to_i2c_client(dev);
  1871. struct lm93_data *data = i2c_get_clientdata(client);
  1872. u32 val = simple_strtoul(buf, NULL, 10);
  1873. u8 tmp;
  1874. mutex_lock(&data->update_lock);
  1875. tmp = lm93_read_byte(client, LM93_REG_PROCHOT_INTERVAL);
  1876. if (nr==1)
  1877. tmp = (tmp & 0x0f) | (LM93_INTERVAL_TO_REG(val) << 4);
  1878. else
  1879. tmp = (tmp & 0xf0) | LM93_INTERVAL_TO_REG(val);
  1880. data->prochot_interval = tmp;
  1881. lm93_write_byte(client, LM93_REG_PROCHOT_INTERVAL, tmp);
  1882. mutex_unlock(&data->update_lock);
  1883. return count;
  1884. }
  1885. static SENSOR_DEVICE_ATTR(prochot1_interval, S_IWUSR | S_IRUGO,
  1886. show_prochot_interval, store_prochot_interval, 0);
  1887. static SENSOR_DEVICE_ATTR(prochot2_interval, S_IWUSR | S_IRUGO,
  1888. show_prochot_interval, store_prochot_interval, 1);
  1889. static ssize_t show_prochot_override_duty_cycle(struct device *dev,
  1890. struct device_attribute *attr,
  1891. char *buf)
  1892. {
  1893. struct lm93_data *data = lm93_update_device(dev);
  1894. return sprintf(buf,"%d\n",data->prochot_override & 0x0f);
  1895. }
  1896. static ssize_t store_prochot_override_duty_cycle(struct device *dev,
  1897. struct device_attribute *attr,
  1898. const char *buf, size_t count)
  1899. {
  1900. struct i2c_client *client = to_i2c_client(dev);
  1901. struct lm93_data *data = i2c_get_clientdata(client);
  1902. u32 val = simple_strtoul(buf, NULL, 10);
  1903. mutex_lock(&data->update_lock);
  1904. data->prochot_override = (data->prochot_override & 0xf0) |
  1905. SENSORS_LIMIT(val, 0, 15);
  1906. lm93_write_byte(client, LM93_REG_PROCHOT_OVERRIDE,
  1907. data->prochot_override);
  1908. mutex_unlock(&data->update_lock);
  1909. return count;
  1910. }
  1911. static DEVICE_ATTR(prochot_override_duty_cycle, S_IRUGO | S_IWUSR,
  1912. show_prochot_override_duty_cycle,
  1913. store_prochot_override_duty_cycle);
  1914. static ssize_t show_prochot_short(struct device *dev,
  1915. struct device_attribute *attr, char *buf)
  1916. {
  1917. struct lm93_data *data = lm93_update_device(dev);
  1918. return sprintf(buf,"%d\n",(data->config & 0x10) ? 1 : 0);
  1919. }
  1920. static ssize_t store_prochot_short(struct device *dev,
  1921. struct device_attribute *attr,
  1922. const char *buf, size_t count)
  1923. {
  1924. struct i2c_client *client = to_i2c_client(dev);
  1925. struct lm93_data *data = i2c_get_clientdata(client);
  1926. u32 val = simple_strtoul(buf, NULL, 10);
  1927. mutex_lock(&data->update_lock);
  1928. if (val)
  1929. data->config |= 0x10;
  1930. else
  1931. data->config &= ~0x10;
  1932. lm93_write_byte(client, LM93_REG_CONFIG, data->config);
  1933. mutex_unlock(&data->update_lock);
  1934. return count;
  1935. }
  1936. static DEVICE_ATTR(prochot_short, S_IRUGO | S_IWUSR,
  1937. show_prochot_short, store_prochot_short);
  1938. static ssize_t show_vrdhot(struct device *dev, struct device_attribute *attr,
  1939. char *buf)
  1940. {
  1941. int nr = (to_sensor_dev_attr(attr))->index;
  1942. struct lm93_data *data = lm93_update_device(dev);
  1943. return sprintf(buf,"%d\n",
  1944. data->block1.host_status_1 & (1 << (nr+4)) ? 1 : 0);
  1945. }
  1946. static SENSOR_DEVICE_ATTR(vrdhot1, S_IRUGO, show_vrdhot, NULL, 0);
  1947. static SENSOR_DEVICE_ATTR(vrdhot2, S_IRUGO, show_vrdhot, NULL, 1);
  1948. static ssize_t show_gpio(struct device *dev, struct device_attribute *attr,
  1949. char *buf)
  1950. {
  1951. struct lm93_data *data = lm93_update_device(dev);
  1952. return sprintf(buf,"%d\n",LM93_GPI_FROM_REG(data->gpi));
  1953. }
  1954. static DEVICE_ATTR(gpio, S_IRUGO, show_gpio, NULL);
  1955. static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
  1956. char *buf)
  1957. {
  1958. struct lm93_data *data = lm93_update_device(dev);
  1959. return sprintf(buf,"%d\n",LM93_ALARMS_FROM_REG(data->block1));
  1960. }
  1961. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  1962. static struct attribute *lm93_attrs[] = {
  1963. &sensor_dev_attr_in1_input.dev_attr.attr,
  1964. &sensor_dev_attr_in2_input.dev_attr.attr,
  1965. &sensor_dev_attr_in3_input.dev_attr.attr,
  1966. &sensor_dev_attr_in4_input.dev_attr.attr,
  1967. &sensor_dev_attr_in5_input.dev_attr.attr,
  1968. &sensor_dev_attr_in6_input.dev_attr.attr,
  1969. &sensor_dev_attr_in7_input.dev_attr.attr,
  1970. &sensor_dev_attr_in8_input.dev_attr.attr,
  1971. &sensor_dev_attr_in9_input.dev_attr.attr,
  1972. &sensor_dev_attr_in10_input.dev_attr.attr,
  1973. &sensor_dev_attr_in11_input.dev_attr.attr,
  1974. &sensor_dev_attr_in12_input.dev_attr.attr,
  1975. &sensor_dev_attr_in13_input.dev_attr.attr,
  1976. &sensor_dev_attr_in14_input.dev_attr.attr,
  1977. &sensor_dev_attr_in15_input.dev_attr.attr,
  1978. &sensor_dev_attr_in16_input.dev_attr.attr,
  1979. &sensor_dev_attr_in1_min.dev_attr.attr,
  1980. &sensor_dev_attr_in2_min.dev_attr.attr,
  1981. &sensor_dev_attr_in3_min.dev_attr.attr,
  1982. &sensor_dev_attr_in4_min.dev_attr.attr,
  1983. &sensor_dev_attr_in5_min.dev_attr.attr,
  1984. &sensor_dev_attr_in6_min.dev_attr.attr,
  1985. &sensor_dev_attr_in7_min.dev_attr.attr,
  1986. &sensor_dev_attr_in8_min.dev_attr.attr,
  1987. &sensor_dev_attr_in9_min.dev_attr.attr,
  1988. &sensor_dev_attr_in10_min.dev_attr.attr,
  1989. &sensor_dev_attr_in11_min.dev_attr.attr,
  1990. &sensor_dev_attr_in12_min.dev_attr.attr,
  1991. &sensor_dev_attr_in13_min.dev_attr.attr,
  1992. &sensor_dev_attr_in14_min.dev_attr.attr,
  1993. &sensor_dev_attr_in15_min.dev_attr.attr,
  1994. &sensor_dev_attr_in16_min.dev_attr.attr,
  1995. &sensor_dev_attr_in1_max.dev_attr.attr,
  1996. &sensor_dev_attr_in2_max.dev_attr.attr,
  1997. &sensor_dev_attr_in3_max.dev_attr.attr,
  1998. &sensor_dev_attr_in4_max.dev_attr.attr,
  1999. &sensor_dev_attr_in5_max.dev_attr.attr,
  2000. &sensor_dev_attr_in6_max.dev_attr.attr,
  2001. &sensor_dev_attr_in7_max.dev_attr.attr,
  2002. &sensor_dev_attr_in8_max.dev_attr.attr,
  2003. &sensor_dev_attr_in9_max.dev_attr.attr,
  2004. &sensor_dev_attr_in10_max.dev_attr.attr,
  2005. &sensor_dev_attr_in11_max.dev_attr.attr,
  2006. &sensor_dev_attr_in12_max.dev_attr.attr,
  2007. &sensor_dev_attr_in13_max.dev_attr.attr,
  2008. &sensor_dev_attr_in14_max.dev_attr.attr,
  2009. &sensor_dev_attr_in15_max.dev_attr.attr,
  2010. &sensor_dev_attr_in16_max.dev_attr.attr,
  2011. &sensor_dev_attr_temp1_input.dev_attr.attr,
  2012. &sensor_dev_attr_temp2_input.dev_attr.attr,
  2013. &sensor_dev_attr_temp3_input.dev_attr.attr,
  2014. &sensor_dev_attr_temp1_min.dev_attr.attr,
  2015. &sensor_dev_attr_temp2_min.dev_attr.attr,
  2016. &sensor_dev_attr_temp3_min.dev_attr.attr,
  2017. &sensor_dev_attr_temp1_max.dev_attr.attr,
  2018. &sensor_dev_attr_temp2_max.dev_attr.attr,
  2019. &sensor_dev_attr_temp3_max.dev_attr.attr,
  2020. &sensor_dev_attr_temp1_auto_base.dev_attr.attr,
  2021. &sensor_dev_attr_temp2_auto_base.dev_attr.attr,
  2022. &sensor_dev_attr_temp3_auto_base.dev_attr.attr,
  2023. &sensor_dev_attr_temp1_auto_boost.dev_attr.attr,
  2024. &sensor_dev_attr_temp2_auto_boost.dev_attr.attr,
  2025. &sensor_dev_attr_temp3_auto_boost.dev_attr.attr,
  2026. &sensor_dev_attr_temp1_auto_boost_hyst.dev_attr.attr,
  2027. &sensor_dev_attr_temp2_auto_boost_hyst.dev_attr.attr,
  2028. &sensor_dev_attr_temp3_auto_boost_hyst.dev_attr.attr,
  2029. &sensor_dev_attr_temp1_auto_offset1.dev_attr.attr,
  2030. &sensor_dev_attr_temp1_auto_offset2.dev_attr.attr,
  2031. &sensor_dev_attr_temp1_auto_offset3.dev_attr.attr,
  2032. &sensor_dev_attr_temp1_auto_offset4.dev_attr.attr,
  2033. &sensor_dev_attr_temp1_auto_offset5.dev_attr.attr,
  2034. &sensor_dev_attr_temp1_auto_offset6.dev_attr.attr,
  2035. &sensor_dev_attr_temp1_auto_offset7.dev_attr.attr,
  2036. &sensor_dev_attr_temp1_auto_offset8.dev_attr.attr,
  2037. &sensor_dev_attr_temp1_auto_offset9.dev_attr.attr,
  2038. &sensor_dev_attr_temp1_auto_offset10.dev_attr.attr,
  2039. &sensor_dev_attr_temp1_auto_offset11.dev_attr.attr,
  2040. &sensor_dev_attr_temp1_auto_offset12.dev_attr.attr,
  2041. &sensor_dev_attr_temp2_auto_offset1.dev_attr.attr,
  2042. &sensor_dev_attr_temp2_auto_offset2.dev_attr.attr,
  2043. &sensor_dev_attr_temp2_auto_offset3.dev_attr.attr,
  2044. &sensor_dev_attr_temp2_auto_offset4.dev_attr.attr,
  2045. &sensor_dev_attr_temp2_auto_offset5.dev_attr.attr,
  2046. &sensor_dev_attr_temp2_auto_offset6.dev_attr.attr,
  2047. &sensor_dev_attr_temp2_auto_offset7.dev_attr.attr,
  2048. &sensor_dev_attr_temp2_auto_offset8.dev_attr.attr,
  2049. &sensor_dev_attr_temp2_auto_offset9.dev_attr.attr,
  2050. &sensor_dev_attr_temp2_auto_offset10.dev_attr.attr,
  2051. &sensor_dev_attr_temp2_auto_offset11.dev_attr.attr,
  2052. &sensor_dev_attr_temp2_auto_offset12.dev_attr.attr,
  2053. &sensor_dev_attr_temp3_auto_offset1.dev_attr.attr,
  2054. &sensor_dev_attr_temp3_auto_offset2.dev_attr.attr,
  2055. &sensor_dev_attr_temp3_auto_offset3.dev_attr.attr,
  2056. &sensor_dev_attr_temp3_auto_offset4.dev_attr.attr,
  2057. &sensor_dev_attr_temp3_auto_offset5.dev_attr.attr,
  2058. &sensor_dev_attr_temp3_auto_offset6.dev_attr.attr,
  2059. &sensor_dev_attr_temp3_auto_offset7.dev_attr.attr,
  2060. &sensor_dev_attr_temp3_auto_offset8.dev_attr.attr,
  2061. &sensor_dev_attr_temp3_auto_offset9.dev_attr.attr,
  2062. &sensor_dev_attr_temp3_auto_offset10.dev_attr.attr,
  2063. &sensor_dev_attr_temp3_auto_offset11.dev_attr.attr,
  2064. &sensor_dev_attr_temp3_auto_offset12.dev_attr.attr,
  2065. &sensor_dev_attr_temp1_auto_pwm_min.dev_attr.attr,
  2066. &sensor_dev_attr_temp2_auto_pwm_min.dev_attr.attr,
  2067. &sensor_dev_attr_temp3_auto_pwm_min.dev_attr.attr,
  2068. &sensor_dev_attr_temp1_auto_offset_hyst.dev_attr.attr,
  2069. &sensor_dev_attr_temp2_auto_offset_hyst.dev_attr.attr,
  2070. &sensor_dev_attr_temp3_auto_offset_hyst.dev_attr.attr,
  2071. &sensor_dev_attr_fan1_input.dev_attr.attr,
  2072. &sensor_dev_attr_fan2_input.dev_attr.attr,
  2073. &sensor_dev_attr_fan3_input.dev_attr.attr,
  2074. &sensor_dev_attr_fan4_input.dev_attr.attr,
  2075. &sensor_dev_attr_fan1_min.dev_attr.attr,
  2076. &sensor_dev_attr_fan2_min.dev_attr.attr,
  2077. &sensor_dev_attr_fan3_min.dev_attr.attr,
  2078. &sensor_dev_attr_fan4_min.dev_attr.attr,
  2079. &sensor_dev_attr_fan1_smart_tach.dev_attr.attr,
  2080. &sensor_dev_attr_fan2_smart_tach.dev_attr.attr,
  2081. &sensor_dev_attr_fan3_smart_tach.dev_attr.attr,
  2082. &sensor_dev_attr_fan4_smart_tach.dev_attr.attr,
  2083. &sensor_dev_attr_pwm1.dev_attr.attr,
  2084. &sensor_dev_attr_pwm2.dev_attr.attr,
  2085. &sensor_dev_attr_pwm1_enable.dev_attr.attr,
  2086. &sensor_dev_attr_pwm2_enable.dev_attr.attr,
  2087. &sensor_dev_attr_pwm1_freq.dev_attr.attr,
  2088. &sensor_dev_attr_pwm2_freq.dev_attr.attr,
  2089. &sensor_dev_attr_pwm1_auto_channels.dev_attr.attr,
  2090. &sensor_dev_attr_pwm2_auto_channels.dev_attr.attr,
  2091. &sensor_dev_attr_pwm1_auto_spinup_min.dev_attr.attr,
  2092. &sensor_dev_attr_pwm2_auto_spinup_min.dev_attr.attr,
  2093. &sensor_dev_attr_pwm1_auto_spinup_time.dev_attr.attr,
  2094. &sensor_dev_attr_pwm2_auto_spinup_time.dev_attr.attr,
  2095. &dev_attr_pwm_auto_prochot_ramp.attr,
  2096. &dev_attr_pwm_auto_vrdhot_ramp.attr,
  2097. &sensor_dev_attr_cpu0_vid.dev_attr.attr,
  2098. &sensor_dev_attr_cpu1_vid.dev_attr.attr,
  2099. &sensor_dev_attr_prochot1.dev_attr.attr,
  2100. &sensor_dev_attr_prochot2.dev_attr.attr,
  2101. &sensor_dev_attr_prochot1_avg.dev_attr.attr,
  2102. &sensor_dev_attr_prochot2_avg.dev_attr.attr,
  2103. &sensor_dev_attr_prochot1_max.dev_attr.attr,
  2104. &sensor_dev_attr_prochot2_max.dev_attr.attr,
  2105. &sensor_dev_attr_prochot1_override.dev_attr.attr,
  2106. &sensor_dev_attr_prochot2_override.dev_attr.attr,
  2107. &sensor_dev_attr_prochot1_interval.dev_attr.attr,
  2108. &sensor_dev_attr_prochot2_interval.dev_attr.attr,
  2109. &dev_attr_prochot_override_duty_cycle.attr,
  2110. &dev_attr_prochot_short.attr,
  2111. &sensor_dev_attr_vrdhot1.dev_attr.attr,
  2112. &sensor_dev_attr_vrdhot2.dev_attr.attr,
  2113. &dev_attr_gpio.attr,
  2114. &dev_attr_alarms.attr,
  2115. NULL
  2116. };
  2117. static struct attribute_group lm93_attr_grp = {
  2118. .attrs = lm93_attrs,
  2119. };
  2120. static void lm93_init_client(struct i2c_client *client)
  2121. {
  2122. int i;
  2123. u8 reg;
  2124. /* configure VID pin input thresholds */
  2125. reg = lm93_read_byte(client, LM93_REG_GPI_VID_CTL);
  2126. lm93_write_byte(client, LM93_REG_GPI_VID_CTL,
  2127. reg | (vid_agtl ? 0x03 : 0x00));
  2128. if (init) {
  2129. /* enable #ALERT pin */
  2130. reg = lm93_read_byte(client, LM93_REG_CONFIG);
  2131. lm93_write_byte(client, LM93_REG_CONFIG, reg | 0x08);
  2132. /* enable ASF mode for BMC status registers */
  2133. reg = lm93_read_byte(client, LM93_REG_STATUS_CONTROL);
  2134. lm93_write_byte(client, LM93_REG_STATUS_CONTROL, reg | 0x02);
  2135. /* set sleep state to S0 */
  2136. lm93_write_byte(client, LM93_REG_SLEEP_CONTROL, 0);
  2137. /* unmask #VRDHOT and dynamic VCCP (if nec) error events */
  2138. reg = lm93_read_byte(client, LM93_REG_MISC_ERR_MASK);
  2139. reg &= ~0x03;
  2140. reg &= ~(vccp_limit_type[0] ? 0x10 : 0);
  2141. reg &= ~(vccp_limit_type[1] ? 0x20 : 0);
  2142. lm93_write_byte(client, LM93_REG_MISC_ERR_MASK, reg);
  2143. }
  2144. /* start monitoring */
  2145. reg = lm93_read_byte(client, LM93_REG_CONFIG);
  2146. lm93_write_byte(client, LM93_REG_CONFIG, reg | 0x01);
  2147. /* spin until ready */
  2148. for (i=0; i<20; i++) {
  2149. msleep(10);
  2150. if ((lm93_read_byte(client, LM93_REG_CONFIG) & 0x80) == 0x80)
  2151. return;
  2152. }
  2153. dev_warn(&client->dev,"timed out waiting for sensor "
  2154. "chip to signal ready!\n");
  2155. }
  2156. /* Return 0 if detection is successful, -ENODEV otherwise */
  2157. static int lm93_detect(struct i2c_client *client, struct i2c_board_info *info)
  2158. {
  2159. struct i2c_adapter *adapter = client->adapter;
  2160. int mfr, ver;
  2161. const char *name;
  2162. if (!i2c_check_functionality(adapter, LM93_SMBUS_FUNC_MIN))
  2163. return -ENODEV;
  2164. /* detection */
  2165. mfr = lm93_read_byte(client, LM93_REG_MFR_ID);
  2166. if (mfr != 0x01) {
  2167. dev_dbg(&adapter->dev,
  2168. "detect failed, bad manufacturer id 0x%02x!\n", mfr);
  2169. return -ENODEV;
  2170. }
  2171. ver = lm93_read_byte(client, LM93_REG_VER);
  2172. switch (ver) {
  2173. case LM93_MFR_ID:
  2174. case LM93_MFR_ID_PROTOTYPE:
  2175. name = "lm93";
  2176. break;
  2177. case LM94_MFR_ID_2:
  2178. case LM94_MFR_ID:
  2179. case LM94_MFR_ID_PROTOTYPE:
  2180. name = "lm94";
  2181. break;
  2182. default:
  2183. dev_dbg(&adapter->dev,
  2184. "detect failed, bad version id 0x%02x!\n", ver);
  2185. return -ENODEV;
  2186. }
  2187. strlcpy(info->type, name, I2C_NAME_SIZE);
  2188. dev_dbg(&adapter->dev,"loading %s at %d,0x%02x\n",
  2189. client->name, i2c_adapter_id(client->adapter),
  2190. client->addr);
  2191. return 0;
  2192. }
  2193. static int lm93_probe(struct i2c_client *client,
  2194. const struct i2c_device_id *id)
  2195. {
  2196. struct lm93_data *data;
  2197. int err, func;
  2198. void (*update)(struct lm93_data *, struct i2c_client *);
  2199. /* choose update routine based on bus capabilities */
  2200. func = i2c_get_functionality(client->adapter);
  2201. if (((LM93_SMBUS_FUNC_FULL & func) == LM93_SMBUS_FUNC_FULL) &&
  2202. (!disable_block)) {
  2203. dev_dbg(&client->dev, "using SMBus block data transactions\n");
  2204. update = lm93_update_client_full;
  2205. } else if ((LM93_SMBUS_FUNC_MIN & func) == LM93_SMBUS_FUNC_MIN) {
  2206. dev_dbg(&client->dev, "disabled SMBus block data "
  2207. "transactions\n");
  2208. update = lm93_update_client_min;
  2209. } else {
  2210. dev_dbg(&client->dev, "detect failed, "
  2211. "smbus byte and/or word data not supported!\n");
  2212. err = -ENODEV;
  2213. goto err_out;
  2214. }
  2215. data = kzalloc(sizeof(struct lm93_data), GFP_KERNEL);
  2216. if (!data) {
  2217. dev_dbg(&client->dev, "out of memory!\n");
  2218. err = -ENOMEM;
  2219. goto err_out;
  2220. }
  2221. i2c_set_clientdata(client, data);
  2222. /* housekeeping */
  2223. data->valid = 0;
  2224. data->update = update;
  2225. mutex_init(&data->update_lock);
  2226. /* initialize the chip */
  2227. lm93_init_client(client);
  2228. err = sysfs_create_group(&client->dev.kobj, &lm93_attr_grp);
  2229. if (err)
  2230. goto err_free;
  2231. /* Register hwmon driver class */
  2232. data->hwmon_dev = hwmon_device_register(&client->dev);
  2233. if ( !IS_ERR(data->hwmon_dev))
  2234. return 0;
  2235. err = PTR_ERR(data->hwmon_dev);
  2236. dev_err(&client->dev, "error registering hwmon device.\n");
  2237. sysfs_remove_group(&client->dev.kobj, &lm93_attr_grp);
  2238. err_free:
  2239. kfree(data);
  2240. err_out:
  2241. return err;
  2242. }
  2243. static int lm93_remove(struct i2c_client *client)
  2244. {
  2245. struct lm93_data *data = i2c_get_clientdata(client);
  2246. hwmon_device_unregister(data->hwmon_dev);
  2247. sysfs_remove_group(&client->dev.kobj, &lm93_attr_grp);
  2248. kfree(data);
  2249. return 0;
  2250. }
  2251. static const struct i2c_device_id lm93_id[] = {
  2252. { "lm93", 0 },
  2253. { "lm94", 0 },
  2254. { }
  2255. };
  2256. MODULE_DEVICE_TABLE(i2c, lm93_id);
  2257. static struct i2c_driver lm93_driver = {
  2258. .class = I2C_CLASS_HWMON,
  2259. .driver = {
  2260. .name = "lm93",
  2261. },
  2262. .probe = lm93_probe,
  2263. .remove = lm93_remove,
  2264. .id_table = lm93_id,
  2265. .detect = lm93_detect,
  2266. .address_list = normal_i2c,
  2267. };
  2268. static int __init lm93_init(void)
  2269. {
  2270. return i2c_add_driver(&lm93_driver);
  2271. }
  2272. static void __exit lm93_exit(void)
  2273. {
  2274. i2c_del_driver(&lm93_driver);
  2275. }
  2276. MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>, "
  2277. "Hans J. Koch <hjk@hansjkoch.de>");
  2278. MODULE_DESCRIPTION("LM93 driver");
  2279. MODULE_LICENSE("GPL");
  2280. module_init(lm93_init);
  2281. module_exit(lm93_exit);