lm93.c 84 KB

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