lm93.c 83 KB

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