adm1026.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860
  1. /*
  2. * adm1026.c - Part of lm_sensors, Linux kernel modules for hardware
  3. * monitoring
  4. * Copyright (C) 2002, 2003 Philip Pokorny <ppokorny@penguincomputing.com>
  5. * Copyright (C) 2004 Justin Thiessen <jthiessen@penguincomputing.com>
  6. *
  7. * Chip details at:
  8. *
  9. * <http://www.onsemi.com/PowerSolutions/product.do?id=ADM1026>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. */
  25. #include <linux/module.h>
  26. #include <linux/init.h>
  27. #include <linux/slab.h>
  28. #include <linux/jiffies.h>
  29. #include <linux/i2c.h>
  30. #include <linux/hwmon.h>
  31. #include <linux/hwmon-sysfs.h>
  32. #include <linux/hwmon-vid.h>
  33. #include <linux/err.h>
  34. #include <linux/mutex.h>
  35. /* Addresses to scan */
  36. static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
  37. static int gpio_input[17] = { -1, -1, -1, -1, -1, -1, -1, -1, -1,
  38. -1, -1, -1, -1, -1, -1, -1, -1 };
  39. static int gpio_output[17] = { -1, -1, -1, -1, -1, -1, -1, -1, -1,
  40. -1, -1, -1, -1, -1, -1, -1, -1 };
  41. static int gpio_inverted[17] = { -1, -1, -1, -1, -1, -1, -1, -1, -1,
  42. -1, -1, -1, -1, -1, -1, -1, -1 };
  43. static int gpio_normal[17] = { -1, -1, -1, -1, -1, -1, -1, -1, -1,
  44. -1, -1, -1, -1, -1, -1, -1, -1 };
  45. static int gpio_fan[8] = { -1, -1, -1, -1, -1, -1, -1, -1 };
  46. module_param_array(gpio_input, int, NULL, 0);
  47. MODULE_PARM_DESC(gpio_input, "List of GPIO pins (0-16) to program as inputs");
  48. module_param_array(gpio_output, int, NULL, 0);
  49. MODULE_PARM_DESC(gpio_output,
  50. "List of GPIO pins (0-16) to program as outputs");
  51. module_param_array(gpio_inverted, int, NULL, 0);
  52. MODULE_PARM_DESC(gpio_inverted,
  53. "List of GPIO pins (0-16) to program as inverted");
  54. module_param_array(gpio_normal, int, NULL, 0);
  55. MODULE_PARM_DESC(gpio_normal,
  56. "List of GPIO pins (0-16) to program as normal/non-inverted");
  57. module_param_array(gpio_fan, int, NULL, 0);
  58. MODULE_PARM_DESC(gpio_fan, "List of GPIO pins (0-7) to program as fan tachs");
  59. /* Many ADM1026 constants specified below */
  60. /* The ADM1026 registers */
  61. #define ADM1026_REG_CONFIG1 0x00
  62. #define CFG1_MONITOR 0x01
  63. #define CFG1_INT_ENABLE 0x02
  64. #define CFG1_INT_CLEAR 0x04
  65. #define CFG1_AIN8_9 0x08
  66. #define CFG1_THERM_HOT 0x10
  67. #define CFG1_DAC_AFC 0x20
  68. #define CFG1_PWM_AFC 0x40
  69. #define CFG1_RESET 0x80
  70. #define ADM1026_REG_CONFIG2 0x01
  71. /* CONFIG2 controls FAN0/GPIO0 through FAN7/GPIO7 */
  72. #define ADM1026_REG_CONFIG3 0x07
  73. #define CFG3_GPIO16_ENABLE 0x01
  74. #define CFG3_CI_CLEAR 0x02
  75. #define CFG3_VREF_250 0x04
  76. #define CFG3_GPIO16_DIR 0x40
  77. #define CFG3_GPIO16_POL 0x80
  78. #define ADM1026_REG_E2CONFIG 0x13
  79. #define E2CFG_READ 0x01
  80. #define E2CFG_WRITE 0x02
  81. #define E2CFG_ERASE 0x04
  82. #define E2CFG_ROM 0x08
  83. #define E2CFG_CLK_EXT 0x80
  84. /*
  85. * There are 10 general analog inputs and 7 dedicated inputs
  86. * They are:
  87. * 0 - 9 = AIN0 - AIN9
  88. * 10 = Vbat
  89. * 11 = 3.3V Standby
  90. * 12 = 3.3V Main
  91. * 13 = +5V
  92. * 14 = Vccp (CPU core voltage)
  93. * 15 = +12V
  94. * 16 = -12V
  95. */
  96. static u16 ADM1026_REG_IN[] = {
  97. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35,
  98. 0x36, 0x37, 0x27, 0x29, 0x26, 0x2a,
  99. 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
  100. };
  101. static u16 ADM1026_REG_IN_MIN[] = {
  102. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d,
  103. 0x5e, 0x5f, 0x6d, 0x49, 0x6b, 0x4a,
  104. 0x4b, 0x4c, 0x4d, 0x4e, 0x4f
  105. };
  106. static u16 ADM1026_REG_IN_MAX[] = {
  107. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55,
  108. 0x56, 0x57, 0x6c, 0x41, 0x6a, 0x42,
  109. 0x43, 0x44, 0x45, 0x46, 0x47
  110. };
  111. /*
  112. * Temperatures are:
  113. * 0 - Internal
  114. * 1 - External 1
  115. * 2 - External 2
  116. */
  117. static u16 ADM1026_REG_TEMP[] = { 0x1f, 0x28, 0x29 };
  118. static u16 ADM1026_REG_TEMP_MIN[] = { 0x69, 0x48, 0x49 };
  119. static u16 ADM1026_REG_TEMP_MAX[] = { 0x68, 0x40, 0x41 };
  120. static u16 ADM1026_REG_TEMP_TMIN[] = { 0x10, 0x11, 0x12 };
  121. static u16 ADM1026_REG_TEMP_THERM[] = { 0x0d, 0x0e, 0x0f };
  122. static u16 ADM1026_REG_TEMP_OFFSET[] = { 0x1e, 0x6e, 0x6f };
  123. #define ADM1026_REG_FAN(nr) (0x38 + (nr))
  124. #define ADM1026_REG_FAN_MIN(nr) (0x60 + (nr))
  125. #define ADM1026_REG_FAN_DIV_0_3 0x02
  126. #define ADM1026_REG_FAN_DIV_4_7 0x03
  127. #define ADM1026_REG_DAC 0x04
  128. #define ADM1026_REG_PWM 0x05
  129. #define ADM1026_REG_GPIO_CFG_0_3 0x08
  130. #define ADM1026_REG_GPIO_CFG_4_7 0x09
  131. #define ADM1026_REG_GPIO_CFG_8_11 0x0a
  132. #define ADM1026_REG_GPIO_CFG_12_15 0x0b
  133. /* CFG_16 in REG_CFG3 */
  134. #define ADM1026_REG_GPIO_STATUS_0_7 0x24
  135. #define ADM1026_REG_GPIO_STATUS_8_15 0x25
  136. /* STATUS_16 in REG_STATUS4 */
  137. #define ADM1026_REG_GPIO_MASK_0_7 0x1c
  138. #define ADM1026_REG_GPIO_MASK_8_15 0x1d
  139. /* MASK_16 in REG_MASK4 */
  140. #define ADM1026_REG_COMPANY 0x16
  141. #define ADM1026_REG_VERSTEP 0x17
  142. /* These are the recognized values for the above regs */
  143. #define ADM1026_COMPANY_ANALOG_DEV 0x41
  144. #define ADM1026_VERSTEP_GENERIC 0x40
  145. #define ADM1026_VERSTEP_ADM1026 0x44
  146. #define ADM1026_REG_MASK1 0x18
  147. #define ADM1026_REG_MASK2 0x19
  148. #define ADM1026_REG_MASK3 0x1a
  149. #define ADM1026_REG_MASK4 0x1b
  150. #define ADM1026_REG_STATUS1 0x20
  151. #define ADM1026_REG_STATUS2 0x21
  152. #define ADM1026_REG_STATUS3 0x22
  153. #define ADM1026_REG_STATUS4 0x23
  154. #define ADM1026_FAN_ACTIVATION_TEMP_HYST -6
  155. #define ADM1026_FAN_CONTROL_TEMP_RANGE 20
  156. #define ADM1026_PWM_MAX 255
  157. /*
  158. * Conversions. Rounding and limit checking is only done on the TO_REG
  159. * variants. Note that you should be a bit careful with which arguments
  160. * these macros are called: arguments may be evaluated more than once.
  161. */
  162. /*
  163. * IN are scaled according to built-in resistors. These are the
  164. * voltages corresponding to 3/4 of full scale (192 or 0xc0)
  165. * NOTE: The -12V input needs an additional factor to account
  166. * for the Vref pullup resistor.
  167. * NEG12_OFFSET = SCALE * Vref / V-192 - Vref
  168. * = 13875 * 2.50 / 1.875 - 2500
  169. * = 16000
  170. *
  171. * The values in this table are based on Table II, page 15 of the
  172. * datasheet.
  173. */
  174. static int adm1026_scaling[] = { /* .001 Volts */
  175. 2250, 2250, 2250, 2250, 2250, 2250,
  176. 1875, 1875, 1875, 1875, 3000, 3330,
  177. 3330, 4995, 2250, 12000, 13875
  178. };
  179. #define NEG12_OFFSET 16000
  180. #define SCALE(val, from, to) (((val)*(to) + ((from)/2))/(from))
  181. #define INS_TO_REG(n, val) (clamp_val(SCALE(val, adm1026_scaling[n], 192),\
  182. 0, 255))
  183. #define INS_FROM_REG(n, val) (SCALE(val, 192, adm1026_scaling[n]))
  184. /*
  185. * FAN speed is measured using 22.5kHz clock and counts for 2 pulses
  186. * and we assume a 2 pulse-per-rev fan tach signal
  187. * 22500 kHz * 60 (sec/min) * 2 (pulse) / 2 (pulse/rev) == 1350000
  188. */
  189. #define FAN_TO_REG(val, div) ((val) <= 0 ? 0xff : \
  190. clamp_val(1350000 / ((val) * (div)), \
  191. 1, 254))
  192. #define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : (val) == 0xff ? 0 : \
  193. 1350000 / ((val) * (div)))
  194. #define DIV_FROM_REG(val) (1 << (val))
  195. #define DIV_TO_REG(val) ((val) >= 8 ? 3 : (val) >= 4 ? 2 : (val) >= 2 ? 1 : 0)
  196. /* Temperature is reported in 1 degC increments */
  197. #define TEMP_TO_REG(val) (clamp_val(((val) + ((val) < 0 ? -500 : 500)) \
  198. / 1000, -127, 127))
  199. #define TEMP_FROM_REG(val) ((val) * 1000)
  200. #define OFFSET_TO_REG(val) (clamp_val(((val) + ((val) < 0 ? -500 : 500)) \
  201. / 1000, -127, 127))
  202. #define OFFSET_FROM_REG(val) ((val) * 1000)
  203. #define PWM_TO_REG(val) (clamp_val(val, 0, 255))
  204. #define PWM_FROM_REG(val) (val)
  205. #define PWM_MIN_TO_REG(val) ((val) & 0xf0)
  206. #define PWM_MIN_FROM_REG(val) (((val) & 0xf0) + ((val) >> 4))
  207. /*
  208. * Analog output is a voltage, and scaled to millivolts. The datasheet
  209. * indicates that the DAC could be used to drive the fans, but in our
  210. * example board (Arima HDAMA) it isn't connected to the fans at all.
  211. */
  212. #define DAC_TO_REG(val) (clamp_val(((((val) * 255) + 500) / 2500), 0, 255))
  213. #define DAC_FROM_REG(val) (((val) * 2500) / 255)
  214. /*
  215. * Chip sampling rates
  216. *
  217. * Some sensors are not updated more frequently than once per second
  218. * so it doesn't make sense to read them more often than that.
  219. * We cache the results and return the saved data if the driver
  220. * is called again before a second has elapsed.
  221. *
  222. * Also, there is significant configuration data for this chip
  223. * So, we keep the config data up to date in the cache
  224. * when it is written and only sample it once every 5 *minutes*
  225. */
  226. #define ADM1026_DATA_INTERVAL (1 * HZ)
  227. #define ADM1026_CONFIG_INTERVAL (5 * 60 * HZ)
  228. /*
  229. * We allow for multiple chips in a single system.
  230. *
  231. * For each registered ADM1026, we need to keep state information
  232. * at client->data. The adm1026_data structure is dynamically
  233. * allocated, when a new client structure is allocated.
  234. */
  235. struct pwm_data {
  236. u8 pwm;
  237. u8 enable;
  238. u8 auto_pwm_min;
  239. };
  240. struct adm1026_data {
  241. struct i2c_client *client;
  242. const struct attribute_group *groups[3];
  243. struct mutex update_lock;
  244. int valid; /* !=0 if following fields are valid */
  245. unsigned long last_reading; /* In jiffies */
  246. unsigned long last_config; /* In jiffies */
  247. u8 in[17]; /* Register value */
  248. u8 in_max[17]; /* Register value */
  249. u8 in_min[17]; /* Register value */
  250. s8 temp[3]; /* Register value */
  251. s8 temp_min[3]; /* Register value */
  252. s8 temp_max[3]; /* Register value */
  253. s8 temp_tmin[3]; /* Register value */
  254. s8 temp_crit[3]; /* Register value */
  255. s8 temp_offset[3]; /* Register value */
  256. u8 fan[8]; /* Register value */
  257. u8 fan_min[8]; /* Register value */
  258. u8 fan_div[8]; /* Decoded value */
  259. struct pwm_data pwm1; /* Pwm control values */
  260. u8 vrm; /* VRM version */
  261. u8 analog_out; /* Register value (DAC) */
  262. long alarms; /* Register encoding, combined */
  263. long alarm_mask; /* Register encoding, combined */
  264. long gpio; /* Register encoding, combined */
  265. long gpio_mask; /* Register encoding, combined */
  266. u8 gpio_config[17]; /* Decoded value */
  267. u8 config1; /* Register value */
  268. u8 config2; /* Register value */
  269. u8 config3; /* Register value */
  270. };
  271. static int adm1026_read_value(struct i2c_client *client, u8 reg)
  272. {
  273. int res;
  274. if (reg < 0x80) {
  275. /* "RAM" locations */
  276. res = i2c_smbus_read_byte_data(client, reg) & 0xff;
  277. } else {
  278. /* EEPROM, do nothing */
  279. res = 0;
  280. }
  281. return res;
  282. }
  283. static int adm1026_write_value(struct i2c_client *client, u8 reg, int value)
  284. {
  285. int res;
  286. if (reg < 0x80) {
  287. /* "RAM" locations */
  288. res = i2c_smbus_write_byte_data(client, reg, value);
  289. } else {
  290. /* EEPROM, do nothing */
  291. res = 0;
  292. }
  293. return res;
  294. }
  295. static struct adm1026_data *adm1026_update_device(struct device *dev)
  296. {
  297. struct adm1026_data *data = dev_get_drvdata(dev);
  298. struct i2c_client *client = data->client;
  299. int i;
  300. long value, alarms, gpio;
  301. mutex_lock(&data->update_lock);
  302. if (!data->valid
  303. || time_after(jiffies,
  304. data->last_reading + ADM1026_DATA_INTERVAL)) {
  305. /* Things that change quickly */
  306. dev_dbg(&client->dev, "Reading sensor values\n");
  307. for (i = 0; i <= 16; ++i) {
  308. data->in[i] =
  309. adm1026_read_value(client, ADM1026_REG_IN[i]);
  310. }
  311. for (i = 0; i <= 7; ++i) {
  312. data->fan[i] =
  313. adm1026_read_value(client, ADM1026_REG_FAN(i));
  314. }
  315. for (i = 0; i <= 2; ++i) {
  316. /*
  317. * NOTE: temp[] is s8 and we assume 2's complement
  318. * "conversion" in the assignment
  319. */
  320. data->temp[i] =
  321. adm1026_read_value(client, ADM1026_REG_TEMP[i]);
  322. }
  323. data->pwm1.pwm = adm1026_read_value(client,
  324. ADM1026_REG_PWM);
  325. data->analog_out = adm1026_read_value(client,
  326. ADM1026_REG_DAC);
  327. /* GPIO16 is MSbit of alarms, move it to gpio */
  328. alarms = adm1026_read_value(client, ADM1026_REG_STATUS4);
  329. gpio = alarms & 0x80 ? 0x0100 : 0; /* GPIO16 */
  330. alarms &= 0x7f;
  331. alarms <<= 8;
  332. alarms |= adm1026_read_value(client, ADM1026_REG_STATUS3);
  333. alarms <<= 8;
  334. alarms |= adm1026_read_value(client, ADM1026_REG_STATUS2);
  335. alarms <<= 8;
  336. alarms |= adm1026_read_value(client, ADM1026_REG_STATUS1);
  337. data->alarms = alarms;
  338. /* Read the GPIO values */
  339. gpio |= adm1026_read_value(client,
  340. ADM1026_REG_GPIO_STATUS_8_15);
  341. gpio <<= 8;
  342. gpio |= adm1026_read_value(client,
  343. ADM1026_REG_GPIO_STATUS_0_7);
  344. data->gpio = gpio;
  345. data->last_reading = jiffies;
  346. } /* last_reading */
  347. if (!data->valid ||
  348. time_after(jiffies, data->last_config + ADM1026_CONFIG_INTERVAL)) {
  349. /* Things that don't change often */
  350. dev_dbg(&client->dev, "Reading config values\n");
  351. for (i = 0; i <= 16; ++i) {
  352. data->in_min[i] = adm1026_read_value(client,
  353. ADM1026_REG_IN_MIN[i]);
  354. data->in_max[i] = adm1026_read_value(client,
  355. ADM1026_REG_IN_MAX[i]);
  356. }
  357. value = adm1026_read_value(client, ADM1026_REG_FAN_DIV_0_3)
  358. | (adm1026_read_value(client, ADM1026_REG_FAN_DIV_4_7)
  359. << 8);
  360. for (i = 0; i <= 7; ++i) {
  361. data->fan_min[i] = adm1026_read_value(client,
  362. ADM1026_REG_FAN_MIN(i));
  363. data->fan_div[i] = DIV_FROM_REG(value & 0x03);
  364. value >>= 2;
  365. }
  366. for (i = 0; i <= 2; ++i) {
  367. /*
  368. * NOTE: temp_xxx[] are s8 and we assume 2's
  369. * complement "conversion" in the assignment
  370. */
  371. data->temp_min[i] = adm1026_read_value(client,
  372. ADM1026_REG_TEMP_MIN[i]);
  373. data->temp_max[i] = adm1026_read_value(client,
  374. ADM1026_REG_TEMP_MAX[i]);
  375. data->temp_tmin[i] = adm1026_read_value(client,
  376. ADM1026_REG_TEMP_TMIN[i]);
  377. data->temp_crit[i] = adm1026_read_value(client,
  378. ADM1026_REG_TEMP_THERM[i]);
  379. data->temp_offset[i] = adm1026_read_value(client,
  380. ADM1026_REG_TEMP_OFFSET[i]);
  381. }
  382. /* Read the STATUS/alarm masks */
  383. alarms = adm1026_read_value(client, ADM1026_REG_MASK4);
  384. gpio = alarms & 0x80 ? 0x0100 : 0; /* GPIO16 */
  385. alarms = (alarms & 0x7f) << 8;
  386. alarms |= adm1026_read_value(client, ADM1026_REG_MASK3);
  387. alarms <<= 8;
  388. alarms |= adm1026_read_value(client, ADM1026_REG_MASK2);
  389. alarms <<= 8;
  390. alarms |= adm1026_read_value(client, ADM1026_REG_MASK1);
  391. data->alarm_mask = alarms;
  392. /* Read the GPIO values */
  393. gpio |= adm1026_read_value(client,
  394. ADM1026_REG_GPIO_MASK_8_15);
  395. gpio <<= 8;
  396. gpio |= adm1026_read_value(client, ADM1026_REG_GPIO_MASK_0_7);
  397. data->gpio_mask = gpio;
  398. /* Read various values from CONFIG1 */
  399. data->config1 = adm1026_read_value(client,
  400. ADM1026_REG_CONFIG1);
  401. if (data->config1 & CFG1_PWM_AFC) {
  402. data->pwm1.enable = 2;
  403. data->pwm1.auto_pwm_min =
  404. PWM_MIN_FROM_REG(data->pwm1.pwm);
  405. }
  406. /* Read the GPIO config */
  407. data->config2 = adm1026_read_value(client,
  408. ADM1026_REG_CONFIG2);
  409. data->config3 = adm1026_read_value(client,
  410. ADM1026_REG_CONFIG3);
  411. data->gpio_config[16] = (data->config3 >> 6) & 0x03;
  412. value = 0;
  413. for (i = 0; i <= 15; ++i) {
  414. if ((i & 0x03) == 0) {
  415. value = adm1026_read_value(client,
  416. ADM1026_REG_GPIO_CFG_0_3 + i/4);
  417. }
  418. data->gpio_config[i] = value & 0x03;
  419. value >>= 2;
  420. }
  421. data->last_config = jiffies;
  422. } /* last_config */
  423. data->valid = 1;
  424. mutex_unlock(&data->update_lock);
  425. return data;
  426. }
  427. static ssize_t show_in(struct device *dev, struct device_attribute *attr,
  428. char *buf)
  429. {
  430. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  431. int nr = sensor_attr->index;
  432. struct adm1026_data *data = adm1026_update_device(dev);
  433. return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in[nr]));
  434. }
  435. static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
  436. char *buf)
  437. {
  438. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  439. int nr = sensor_attr->index;
  440. struct adm1026_data *data = adm1026_update_device(dev);
  441. return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_min[nr]));
  442. }
  443. static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
  444. const char *buf, size_t count)
  445. {
  446. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  447. int nr = sensor_attr->index;
  448. struct adm1026_data *data = dev_get_drvdata(dev);
  449. struct i2c_client *client = data->client;
  450. long val;
  451. int err;
  452. err = kstrtol(buf, 10, &val);
  453. if (err)
  454. return err;
  455. mutex_lock(&data->update_lock);
  456. data->in_min[nr] = INS_TO_REG(nr, val);
  457. adm1026_write_value(client, ADM1026_REG_IN_MIN[nr], data->in_min[nr]);
  458. mutex_unlock(&data->update_lock);
  459. return count;
  460. }
  461. static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
  462. char *buf)
  463. {
  464. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  465. int nr = sensor_attr->index;
  466. struct adm1026_data *data = adm1026_update_device(dev);
  467. return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_max[nr]));
  468. }
  469. static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
  470. const char *buf, size_t count)
  471. {
  472. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  473. int nr = sensor_attr->index;
  474. struct adm1026_data *data = dev_get_drvdata(dev);
  475. struct i2c_client *client = data->client;
  476. long val;
  477. int err;
  478. err = kstrtol(buf, 10, &val);
  479. if (err)
  480. return err;
  481. mutex_lock(&data->update_lock);
  482. data->in_max[nr] = INS_TO_REG(nr, val);
  483. adm1026_write_value(client, ADM1026_REG_IN_MAX[nr], data->in_max[nr]);
  484. mutex_unlock(&data->update_lock);
  485. return count;
  486. }
  487. #define in_reg(offset) \
  488. static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, show_in, \
  489. NULL, offset); \
  490. static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
  491. show_in_min, set_in_min, offset); \
  492. static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
  493. show_in_max, set_in_max, offset);
  494. in_reg(0);
  495. in_reg(1);
  496. in_reg(2);
  497. in_reg(3);
  498. in_reg(4);
  499. in_reg(5);
  500. in_reg(6);
  501. in_reg(7);
  502. in_reg(8);
  503. in_reg(9);
  504. in_reg(10);
  505. in_reg(11);
  506. in_reg(12);
  507. in_reg(13);
  508. in_reg(14);
  509. in_reg(15);
  510. static ssize_t show_in16(struct device *dev, struct device_attribute *attr,
  511. char *buf)
  512. {
  513. struct adm1026_data *data = adm1026_update_device(dev);
  514. return sprintf(buf, "%d\n", INS_FROM_REG(16, data->in[16]) -
  515. NEG12_OFFSET);
  516. }
  517. static ssize_t show_in16_min(struct device *dev, struct device_attribute *attr,
  518. char *buf)
  519. {
  520. struct adm1026_data *data = adm1026_update_device(dev);
  521. return sprintf(buf, "%d\n", INS_FROM_REG(16, data->in_min[16])
  522. - NEG12_OFFSET);
  523. }
  524. static ssize_t set_in16_min(struct device *dev, struct device_attribute *attr,
  525. const char *buf, size_t count)
  526. {
  527. struct adm1026_data *data = dev_get_drvdata(dev);
  528. struct i2c_client *client = data->client;
  529. long val;
  530. int err;
  531. err = kstrtol(buf, 10, &val);
  532. if (err)
  533. return err;
  534. mutex_lock(&data->update_lock);
  535. data->in_min[16] = INS_TO_REG(16, val + NEG12_OFFSET);
  536. adm1026_write_value(client, ADM1026_REG_IN_MIN[16], data->in_min[16]);
  537. mutex_unlock(&data->update_lock);
  538. return count;
  539. }
  540. static ssize_t show_in16_max(struct device *dev, struct device_attribute *attr,
  541. char *buf)
  542. {
  543. struct adm1026_data *data = adm1026_update_device(dev);
  544. return sprintf(buf, "%d\n", INS_FROM_REG(16, data->in_max[16])
  545. - NEG12_OFFSET);
  546. }
  547. static ssize_t set_in16_max(struct device *dev, struct device_attribute *attr,
  548. const char *buf, size_t count)
  549. {
  550. struct adm1026_data *data = dev_get_drvdata(dev);
  551. struct i2c_client *client = data->client;
  552. long val;
  553. int err;
  554. err = kstrtol(buf, 10, &val);
  555. if (err)
  556. return err;
  557. mutex_lock(&data->update_lock);
  558. data->in_max[16] = INS_TO_REG(16, val+NEG12_OFFSET);
  559. adm1026_write_value(client, ADM1026_REG_IN_MAX[16], data->in_max[16]);
  560. mutex_unlock(&data->update_lock);
  561. return count;
  562. }
  563. static SENSOR_DEVICE_ATTR(in16_input, S_IRUGO, show_in16, NULL, 16);
  564. static SENSOR_DEVICE_ATTR(in16_min, S_IRUGO | S_IWUSR, show_in16_min,
  565. set_in16_min, 16);
  566. static SENSOR_DEVICE_ATTR(in16_max, S_IRUGO | S_IWUSR, show_in16_max,
  567. set_in16_max, 16);
  568. /* Now add fan read/write functions */
  569. static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
  570. char *buf)
  571. {
  572. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  573. int nr = sensor_attr->index;
  574. struct adm1026_data *data = adm1026_update_device(dev);
  575. return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
  576. data->fan_div[nr]));
  577. }
  578. static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
  579. char *buf)
  580. {
  581. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  582. int nr = sensor_attr->index;
  583. struct adm1026_data *data = adm1026_update_device(dev);
  584. return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
  585. data->fan_div[nr]));
  586. }
  587. static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
  588. const char *buf, size_t count)
  589. {
  590. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  591. int nr = sensor_attr->index;
  592. struct adm1026_data *data = dev_get_drvdata(dev);
  593. struct i2c_client *client = data->client;
  594. long val;
  595. int err;
  596. err = kstrtol(buf, 10, &val);
  597. if (err)
  598. return err;
  599. mutex_lock(&data->update_lock);
  600. data->fan_min[nr] = FAN_TO_REG(val, data->fan_div[nr]);
  601. adm1026_write_value(client, ADM1026_REG_FAN_MIN(nr),
  602. data->fan_min[nr]);
  603. mutex_unlock(&data->update_lock);
  604. return count;
  605. }
  606. #define fan_offset(offset) \
  607. static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan, NULL, \
  608. offset - 1); \
  609. static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
  610. show_fan_min, set_fan_min, offset - 1);
  611. fan_offset(1);
  612. fan_offset(2);
  613. fan_offset(3);
  614. fan_offset(4);
  615. fan_offset(5);
  616. fan_offset(6);
  617. fan_offset(7);
  618. fan_offset(8);
  619. /* Adjust fan_min to account for new fan divisor */
  620. static void fixup_fan_min(struct device *dev, int fan, int old_div)
  621. {
  622. struct adm1026_data *data = dev_get_drvdata(dev);
  623. struct i2c_client *client = data->client;
  624. int new_min;
  625. int new_div = data->fan_div[fan];
  626. /* 0 and 0xff are special. Don't adjust them */
  627. if (data->fan_min[fan] == 0 || data->fan_min[fan] == 0xff)
  628. return;
  629. new_min = data->fan_min[fan] * old_div / new_div;
  630. new_min = clamp_val(new_min, 1, 254);
  631. data->fan_min[fan] = new_min;
  632. adm1026_write_value(client, ADM1026_REG_FAN_MIN(fan), new_min);
  633. }
  634. /* Now add fan_div read/write functions */
  635. static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
  636. char *buf)
  637. {
  638. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  639. int nr = sensor_attr->index;
  640. struct adm1026_data *data = adm1026_update_device(dev);
  641. return sprintf(buf, "%d\n", data->fan_div[nr]);
  642. }
  643. static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
  644. const char *buf, size_t count)
  645. {
  646. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  647. int nr = sensor_attr->index;
  648. struct adm1026_data *data = dev_get_drvdata(dev);
  649. struct i2c_client *client = data->client;
  650. long val;
  651. int orig_div, new_div;
  652. int err;
  653. err = kstrtol(buf, 10, &val);
  654. if (err)
  655. return err;
  656. new_div = DIV_TO_REG(val);
  657. mutex_lock(&data->update_lock);
  658. orig_div = data->fan_div[nr];
  659. data->fan_div[nr] = DIV_FROM_REG(new_div);
  660. if (nr < 4) { /* 0 <= nr < 4 */
  661. adm1026_write_value(client, ADM1026_REG_FAN_DIV_0_3,
  662. (DIV_TO_REG(data->fan_div[0]) << 0) |
  663. (DIV_TO_REG(data->fan_div[1]) << 2) |
  664. (DIV_TO_REG(data->fan_div[2]) << 4) |
  665. (DIV_TO_REG(data->fan_div[3]) << 6));
  666. } else { /* 3 < nr < 8 */
  667. adm1026_write_value(client, ADM1026_REG_FAN_DIV_4_7,
  668. (DIV_TO_REG(data->fan_div[4]) << 0) |
  669. (DIV_TO_REG(data->fan_div[5]) << 2) |
  670. (DIV_TO_REG(data->fan_div[6]) << 4) |
  671. (DIV_TO_REG(data->fan_div[7]) << 6));
  672. }
  673. if (data->fan_div[nr] != orig_div)
  674. fixup_fan_min(dev, nr, orig_div);
  675. mutex_unlock(&data->update_lock);
  676. return count;
  677. }
  678. #define fan_offset_div(offset) \
  679. static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
  680. show_fan_div, set_fan_div, offset - 1);
  681. fan_offset_div(1);
  682. fan_offset_div(2);
  683. fan_offset_div(3);
  684. fan_offset_div(4);
  685. fan_offset_div(5);
  686. fan_offset_div(6);
  687. fan_offset_div(7);
  688. fan_offset_div(8);
  689. /* Temps */
  690. static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
  691. char *buf)
  692. {
  693. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  694. int nr = sensor_attr->index;
  695. struct adm1026_data *data = adm1026_update_device(dev);
  696. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
  697. }
  698. static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
  699. char *buf)
  700. {
  701. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  702. int nr = sensor_attr->index;
  703. struct adm1026_data *data = adm1026_update_device(dev);
  704. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
  705. }
  706. static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
  707. const char *buf, size_t count)
  708. {
  709. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  710. int nr = sensor_attr->index;
  711. struct adm1026_data *data = dev_get_drvdata(dev);
  712. struct i2c_client *client = data->client;
  713. long val;
  714. int err;
  715. err = kstrtol(buf, 10, &val);
  716. if (err)
  717. return err;
  718. mutex_lock(&data->update_lock);
  719. data->temp_min[nr] = TEMP_TO_REG(val);
  720. adm1026_write_value(client, ADM1026_REG_TEMP_MIN[nr],
  721. data->temp_min[nr]);
  722. mutex_unlock(&data->update_lock);
  723. return count;
  724. }
  725. static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
  726. char *buf)
  727. {
  728. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  729. int nr = sensor_attr->index;
  730. struct adm1026_data *data = adm1026_update_device(dev);
  731. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
  732. }
  733. static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
  734. const char *buf, size_t count)
  735. {
  736. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  737. int nr = sensor_attr->index;
  738. struct adm1026_data *data = dev_get_drvdata(dev);
  739. struct i2c_client *client = data->client;
  740. long val;
  741. int err;
  742. err = kstrtol(buf, 10, &val);
  743. if (err)
  744. return err;
  745. mutex_lock(&data->update_lock);
  746. data->temp_max[nr] = TEMP_TO_REG(val);
  747. adm1026_write_value(client, ADM1026_REG_TEMP_MAX[nr],
  748. data->temp_max[nr]);
  749. mutex_unlock(&data->update_lock);
  750. return count;
  751. }
  752. #define temp_reg(offset) \
  753. static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp, \
  754. NULL, offset - 1); \
  755. static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
  756. show_temp_min, set_temp_min, offset - 1); \
  757. static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
  758. show_temp_max, set_temp_max, offset - 1);
  759. temp_reg(1);
  760. temp_reg(2);
  761. temp_reg(3);
  762. static ssize_t show_temp_offset(struct device *dev,
  763. struct device_attribute *attr, char *buf)
  764. {
  765. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  766. int nr = sensor_attr->index;
  767. struct adm1026_data *data = adm1026_update_device(dev);
  768. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_offset[nr]));
  769. }
  770. static ssize_t set_temp_offset(struct device *dev,
  771. struct device_attribute *attr, const char *buf,
  772. size_t count)
  773. {
  774. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  775. int nr = sensor_attr->index;
  776. struct adm1026_data *data = dev_get_drvdata(dev);
  777. struct i2c_client *client = data->client;
  778. long val;
  779. int err;
  780. err = kstrtol(buf, 10, &val);
  781. if (err)
  782. return err;
  783. mutex_lock(&data->update_lock);
  784. data->temp_offset[nr] = TEMP_TO_REG(val);
  785. adm1026_write_value(client, ADM1026_REG_TEMP_OFFSET[nr],
  786. data->temp_offset[nr]);
  787. mutex_unlock(&data->update_lock);
  788. return count;
  789. }
  790. #define temp_offset_reg(offset) \
  791. static SENSOR_DEVICE_ATTR(temp##offset##_offset, S_IRUGO | S_IWUSR, \
  792. show_temp_offset, set_temp_offset, offset - 1);
  793. temp_offset_reg(1);
  794. temp_offset_reg(2);
  795. temp_offset_reg(3);
  796. static ssize_t show_temp_auto_point1_temp_hyst(struct device *dev,
  797. struct device_attribute *attr, char *buf)
  798. {
  799. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  800. int nr = sensor_attr->index;
  801. struct adm1026_data *data = adm1026_update_device(dev);
  802. return sprintf(buf, "%d\n", TEMP_FROM_REG(
  803. ADM1026_FAN_ACTIVATION_TEMP_HYST + data->temp_tmin[nr]));
  804. }
  805. static ssize_t show_temp_auto_point2_temp(struct device *dev,
  806. struct device_attribute *attr, char *buf)
  807. {
  808. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  809. int nr = sensor_attr->index;
  810. struct adm1026_data *data = adm1026_update_device(dev);
  811. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_tmin[nr] +
  812. ADM1026_FAN_CONTROL_TEMP_RANGE));
  813. }
  814. static ssize_t show_temp_auto_point1_temp(struct device *dev,
  815. struct device_attribute *attr, char *buf)
  816. {
  817. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  818. int nr = sensor_attr->index;
  819. struct adm1026_data *data = adm1026_update_device(dev);
  820. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_tmin[nr]));
  821. }
  822. static ssize_t set_temp_auto_point1_temp(struct device *dev,
  823. struct device_attribute *attr, const char *buf, size_t count)
  824. {
  825. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  826. int nr = sensor_attr->index;
  827. struct adm1026_data *data = dev_get_drvdata(dev);
  828. struct i2c_client *client = data->client;
  829. long val;
  830. int err;
  831. err = kstrtol(buf, 10, &val);
  832. if (err)
  833. return err;
  834. mutex_lock(&data->update_lock);
  835. data->temp_tmin[nr] = TEMP_TO_REG(val);
  836. adm1026_write_value(client, ADM1026_REG_TEMP_TMIN[nr],
  837. data->temp_tmin[nr]);
  838. mutex_unlock(&data->update_lock);
  839. return count;
  840. }
  841. #define temp_auto_point(offset) \
  842. static SENSOR_DEVICE_ATTR(temp##offset##_auto_point1_temp, \
  843. S_IRUGO | S_IWUSR, show_temp_auto_point1_temp, \
  844. set_temp_auto_point1_temp, offset - 1); \
  845. static SENSOR_DEVICE_ATTR(temp##offset##_auto_point1_temp_hyst, S_IRUGO,\
  846. show_temp_auto_point1_temp_hyst, NULL, offset - 1); \
  847. static SENSOR_DEVICE_ATTR(temp##offset##_auto_point2_temp, S_IRUGO, \
  848. show_temp_auto_point2_temp, NULL, offset - 1);
  849. temp_auto_point(1);
  850. temp_auto_point(2);
  851. temp_auto_point(3);
  852. static ssize_t show_temp_crit_enable(struct device *dev,
  853. struct device_attribute *attr, char *buf)
  854. {
  855. struct adm1026_data *data = adm1026_update_device(dev);
  856. return sprintf(buf, "%d\n", (data->config1 & CFG1_THERM_HOT) >> 4);
  857. }
  858. static ssize_t set_temp_crit_enable(struct device *dev,
  859. struct device_attribute *attr, const char *buf, size_t count)
  860. {
  861. struct adm1026_data *data = dev_get_drvdata(dev);
  862. struct i2c_client *client = data->client;
  863. unsigned long val;
  864. int err;
  865. err = kstrtoul(buf, 10, &val);
  866. if (err)
  867. return err;
  868. if (val > 1)
  869. return -EINVAL;
  870. mutex_lock(&data->update_lock);
  871. data->config1 = (data->config1 & ~CFG1_THERM_HOT) | (val << 4);
  872. adm1026_write_value(client, ADM1026_REG_CONFIG1, data->config1);
  873. mutex_unlock(&data->update_lock);
  874. return count;
  875. }
  876. #define temp_crit_enable(offset) \
  877. static DEVICE_ATTR(temp##offset##_crit_enable, S_IRUGO | S_IWUSR, \
  878. show_temp_crit_enable, set_temp_crit_enable);
  879. temp_crit_enable(1);
  880. temp_crit_enable(2);
  881. temp_crit_enable(3);
  882. static ssize_t show_temp_crit(struct device *dev,
  883. struct device_attribute *attr, char *buf)
  884. {
  885. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  886. int nr = sensor_attr->index;
  887. struct adm1026_data *data = adm1026_update_device(dev);
  888. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit[nr]));
  889. }
  890. static ssize_t set_temp_crit(struct device *dev, struct device_attribute *attr,
  891. const char *buf, size_t count)
  892. {
  893. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  894. int nr = sensor_attr->index;
  895. struct adm1026_data *data = dev_get_drvdata(dev);
  896. struct i2c_client *client = data->client;
  897. long val;
  898. int err;
  899. err = kstrtol(buf, 10, &val);
  900. if (err)
  901. return err;
  902. mutex_lock(&data->update_lock);
  903. data->temp_crit[nr] = TEMP_TO_REG(val);
  904. adm1026_write_value(client, ADM1026_REG_TEMP_THERM[nr],
  905. data->temp_crit[nr]);
  906. mutex_unlock(&data->update_lock);
  907. return count;
  908. }
  909. #define temp_crit_reg(offset) \
  910. static SENSOR_DEVICE_ATTR(temp##offset##_crit, S_IRUGO | S_IWUSR, \
  911. show_temp_crit, set_temp_crit, offset - 1);
  912. temp_crit_reg(1);
  913. temp_crit_reg(2);
  914. temp_crit_reg(3);
  915. static ssize_t show_analog_out_reg(struct device *dev,
  916. struct device_attribute *attr, char *buf)
  917. {
  918. struct adm1026_data *data = adm1026_update_device(dev);
  919. return sprintf(buf, "%d\n", DAC_FROM_REG(data->analog_out));
  920. }
  921. static ssize_t set_analog_out_reg(struct device *dev,
  922. struct device_attribute *attr,
  923. const char *buf, size_t count)
  924. {
  925. struct adm1026_data *data = dev_get_drvdata(dev);
  926. struct i2c_client *client = data->client;
  927. long val;
  928. int err;
  929. err = kstrtol(buf, 10, &val);
  930. if (err)
  931. return err;
  932. mutex_lock(&data->update_lock);
  933. data->analog_out = DAC_TO_REG(val);
  934. adm1026_write_value(client, ADM1026_REG_DAC, data->analog_out);
  935. mutex_unlock(&data->update_lock);
  936. return count;
  937. }
  938. static DEVICE_ATTR(analog_out, S_IRUGO | S_IWUSR, show_analog_out_reg,
  939. set_analog_out_reg);
  940. static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
  941. char *buf)
  942. {
  943. struct adm1026_data *data = adm1026_update_device(dev);
  944. int vid = (data->gpio >> 11) & 0x1f;
  945. dev_dbg(dev, "Setting VID from GPIO11-15.\n");
  946. return sprintf(buf, "%d\n", vid_from_reg(vid, data->vrm));
  947. }
  948. static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
  949. static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
  950. char *buf)
  951. {
  952. struct adm1026_data *data = dev_get_drvdata(dev);
  953. return sprintf(buf, "%d\n", data->vrm);
  954. }
  955. static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
  956. const char *buf, size_t count)
  957. {
  958. struct adm1026_data *data = dev_get_drvdata(dev);
  959. unsigned long val;
  960. int err;
  961. err = kstrtoul(buf, 10, &val);
  962. if (err)
  963. return err;
  964. if (val > 255)
  965. return -EINVAL;
  966. data->vrm = val;
  967. return count;
  968. }
  969. static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
  970. static ssize_t show_alarms_reg(struct device *dev,
  971. struct device_attribute *attr, char *buf)
  972. {
  973. struct adm1026_data *data = adm1026_update_device(dev);
  974. return sprintf(buf, "%ld\n", data->alarms);
  975. }
  976. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
  977. static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
  978. char *buf)
  979. {
  980. struct adm1026_data *data = adm1026_update_device(dev);
  981. int bitnr = to_sensor_dev_attr(attr)->index;
  982. return sprintf(buf, "%ld\n", (data->alarms >> bitnr) & 1);
  983. }
  984. static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 0);
  985. static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 1);
  986. static SENSOR_DEVICE_ATTR(in9_alarm, S_IRUGO, show_alarm, NULL, 1);
  987. static SENSOR_DEVICE_ATTR(in11_alarm, S_IRUGO, show_alarm, NULL, 2);
  988. static SENSOR_DEVICE_ATTR(in12_alarm, S_IRUGO, show_alarm, NULL, 3);
  989. static SENSOR_DEVICE_ATTR(in13_alarm, S_IRUGO, show_alarm, NULL, 4);
  990. static SENSOR_DEVICE_ATTR(in14_alarm, S_IRUGO, show_alarm, NULL, 5);
  991. static SENSOR_DEVICE_ATTR(in15_alarm, S_IRUGO, show_alarm, NULL, 6);
  992. static SENSOR_DEVICE_ATTR(in16_alarm, S_IRUGO, show_alarm, NULL, 7);
  993. static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
  994. static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
  995. static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
  996. static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
  997. static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
  998. static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
  999. static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
  1000. static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
  1001. static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 16);
  1002. static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 17);
  1003. static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 18);
  1004. static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 19);
  1005. static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 20);
  1006. static SENSOR_DEVICE_ATTR(fan6_alarm, S_IRUGO, show_alarm, NULL, 21);
  1007. static SENSOR_DEVICE_ATTR(fan7_alarm, S_IRUGO, show_alarm, NULL, 22);
  1008. static SENSOR_DEVICE_ATTR(fan8_alarm, S_IRUGO, show_alarm, NULL, 23);
  1009. static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 24);
  1010. static SENSOR_DEVICE_ATTR(in10_alarm, S_IRUGO, show_alarm, NULL, 25);
  1011. static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 26);
  1012. static ssize_t show_alarm_mask(struct device *dev,
  1013. struct device_attribute *attr, char *buf)
  1014. {
  1015. struct adm1026_data *data = adm1026_update_device(dev);
  1016. return sprintf(buf, "%ld\n", data->alarm_mask);
  1017. }
  1018. static ssize_t set_alarm_mask(struct device *dev, struct device_attribute *attr,
  1019. const char *buf, size_t count)
  1020. {
  1021. struct adm1026_data *data = dev_get_drvdata(dev);
  1022. struct i2c_client *client = data->client;
  1023. unsigned long mask;
  1024. long val;
  1025. int err;
  1026. err = kstrtol(buf, 10, &val);
  1027. if (err)
  1028. return err;
  1029. mutex_lock(&data->update_lock);
  1030. data->alarm_mask = val & 0x7fffffff;
  1031. mask = data->alarm_mask
  1032. | (data->gpio_mask & 0x10000 ? 0x80000000 : 0);
  1033. adm1026_write_value(client, ADM1026_REG_MASK1,
  1034. mask & 0xff);
  1035. mask >>= 8;
  1036. adm1026_write_value(client, ADM1026_REG_MASK2,
  1037. mask & 0xff);
  1038. mask >>= 8;
  1039. adm1026_write_value(client, ADM1026_REG_MASK3,
  1040. mask & 0xff);
  1041. mask >>= 8;
  1042. adm1026_write_value(client, ADM1026_REG_MASK4,
  1043. mask & 0xff);
  1044. mutex_unlock(&data->update_lock);
  1045. return count;
  1046. }
  1047. static DEVICE_ATTR(alarm_mask, S_IRUGO | S_IWUSR, show_alarm_mask,
  1048. set_alarm_mask);
  1049. static ssize_t show_gpio(struct device *dev, struct device_attribute *attr,
  1050. char *buf)
  1051. {
  1052. struct adm1026_data *data = adm1026_update_device(dev);
  1053. return sprintf(buf, "%ld\n", data->gpio);
  1054. }
  1055. static ssize_t set_gpio(struct device *dev, struct device_attribute *attr,
  1056. const char *buf, size_t count)
  1057. {
  1058. struct adm1026_data *data = dev_get_drvdata(dev);
  1059. struct i2c_client *client = data->client;
  1060. long gpio;
  1061. long val;
  1062. int err;
  1063. err = kstrtol(buf, 10, &val);
  1064. if (err)
  1065. return err;
  1066. mutex_lock(&data->update_lock);
  1067. data->gpio = val & 0x1ffff;
  1068. gpio = data->gpio;
  1069. adm1026_write_value(client, ADM1026_REG_GPIO_STATUS_0_7, gpio & 0xff);
  1070. gpio >>= 8;
  1071. adm1026_write_value(client, ADM1026_REG_GPIO_STATUS_8_15, gpio & 0xff);
  1072. gpio = ((gpio >> 1) & 0x80) | (data->alarms >> 24 & 0x7f);
  1073. adm1026_write_value(client, ADM1026_REG_STATUS4, gpio & 0xff);
  1074. mutex_unlock(&data->update_lock);
  1075. return count;
  1076. }
  1077. static DEVICE_ATTR(gpio, S_IRUGO | S_IWUSR, show_gpio, set_gpio);
  1078. static ssize_t show_gpio_mask(struct device *dev, struct device_attribute *attr,
  1079. char *buf)
  1080. {
  1081. struct adm1026_data *data = adm1026_update_device(dev);
  1082. return sprintf(buf, "%ld\n", data->gpio_mask);
  1083. }
  1084. static ssize_t set_gpio_mask(struct device *dev, struct device_attribute *attr,
  1085. const char *buf, size_t count)
  1086. {
  1087. struct adm1026_data *data = dev_get_drvdata(dev);
  1088. struct i2c_client *client = data->client;
  1089. long mask;
  1090. long val;
  1091. int err;
  1092. err = kstrtol(buf, 10, &val);
  1093. if (err)
  1094. return err;
  1095. mutex_lock(&data->update_lock);
  1096. data->gpio_mask = val & 0x1ffff;
  1097. mask = data->gpio_mask;
  1098. adm1026_write_value(client, ADM1026_REG_GPIO_MASK_0_7, mask & 0xff);
  1099. mask >>= 8;
  1100. adm1026_write_value(client, ADM1026_REG_GPIO_MASK_8_15, mask & 0xff);
  1101. mask = ((mask >> 1) & 0x80) | (data->alarm_mask >> 24 & 0x7f);
  1102. adm1026_write_value(client, ADM1026_REG_MASK1, mask & 0xff);
  1103. mutex_unlock(&data->update_lock);
  1104. return count;
  1105. }
  1106. static DEVICE_ATTR(gpio_mask, S_IRUGO | S_IWUSR, show_gpio_mask, set_gpio_mask);
  1107. static ssize_t show_pwm_reg(struct device *dev, struct device_attribute *attr,
  1108. char *buf)
  1109. {
  1110. struct adm1026_data *data = adm1026_update_device(dev);
  1111. return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm1.pwm));
  1112. }
  1113. static ssize_t set_pwm_reg(struct device *dev, struct device_attribute *attr,
  1114. const char *buf, size_t count)
  1115. {
  1116. struct adm1026_data *data = dev_get_drvdata(dev);
  1117. struct i2c_client *client = data->client;
  1118. if (data->pwm1.enable == 1) {
  1119. long val;
  1120. int err;
  1121. err = kstrtol(buf, 10, &val);
  1122. if (err)
  1123. return err;
  1124. mutex_lock(&data->update_lock);
  1125. data->pwm1.pwm = PWM_TO_REG(val);
  1126. adm1026_write_value(client, ADM1026_REG_PWM, data->pwm1.pwm);
  1127. mutex_unlock(&data->update_lock);
  1128. }
  1129. return count;
  1130. }
  1131. static ssize_t show_auto_pwm_min(struct device *dev,
  1132. struct device_attribute *attr, char *buf)
  1133. {
  1134. struct adm1026_data *data = adm1026_update_device(dev);
  1135. return sprintf(buf, "%d\n", data->pwm1.auto_pwm_min);
  1136. }
  1137. static ssize_t set_auto_pwm_min(struct device *dev,
  1138. struct device_attribute *attr, const char *buf,
  1139. size_t count)
  1140. {
  1141. struct adm1026_data *data = dev_get_drvdata(dev);
  1142. struct i2c_client *client = data->client;
  1143. unsigned long val;
  1144. int err;
  1145. err = kstrtoul(buf, 10, &val);
  1146. if (err)
  1147. return err;
  1148. mutex_lock(&data->update_lock);
  1149. data->pwm1.auto_pwm_min = clamp_val(val, 0, 255);
  1150. if (data->pwm1.enable == 2) { /* apply immediately */
  1151. data->pwm1.pwm = PWM_TO_REG((data->pwm1.pwm & 0x0f) |
  1152. PWM_MIN_TO_REG(data->pwm1.auto_pwm_min));
  1153. adm1026_write_value(client, ADM1026_REG_PWM, data->pwm1.pwm);
  1154. }
  1155. mutex_unlock(&data->update_lock);
  1156. return count;
  1157. }
  1158. static ssize_t show_auto_pwm_max(struct device *dev,
  1159. struct device_attribute *attr, char *buf)
  1160. {
  1161. return sprintf(buf, "%d\n", ADM1026_PWM_MAX);
  1162. }
  1163. static ssize_t show_pwm_enable(struct device *dev,
  1164. struct device_attribute *attr, char *buf)
  1165. {
  1166. struct adm1026_data *data = adm1026_update_device(dev);
  1167. return sprintf(buf, "%d\n", data->pwm1.enable);
  1168. }
  1169. static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *attr,
  1170. const char *buf, size_t count)
  1171. {
  1172. struct adm1026_data *data = dev_get_drvdata(dev);
  1173. struct i2c_client *client = data->client;
  1174. int old_enable;
  1175. unsigned long val;
  1176. int err;
  1177. err = kstrtoul(buf, 10, &val);
  1178. if (err)
  1179. return err;
  1180. if (val >= 3)
  1181. return -EINVAL;
  1182. mutex_lock(&data->update_lock);
  1183. old_enable = data->pwm1.enable;
  1184. data->pwm1.enable = val;
  1185. data->config1 = (data->config1 & ~CFG1_PWM_AFC)
  1186. | ((val == 2) ? CFG1_PWM_AFC : 0);
  1187. adm1026_write_value(client, ADM1026_REG_CONFIG1, data->config1);
  1188. if (val == 2) { /* apply pwm1_auto_pwm_min to pwm1 */
  1189. data->pwm1.pwm = PWM_TO_REG((data->pwm1.pwm & 0x0f) |
  1190. PWM_MIN_TO_REG(data->pwm1.auto_pwm_min));
  1191. adm1026_write_value(client, ADM1026_REG_PWM, data->pwm1.pwm);
  1192. } else if (!((old_enable == 1) && (val == 1))) {
  1193. /* set pwm to safe value */
  1194. data->pwm1.pwm = 255;
  1195. adm1026_write_value(client, ADM1026_REG_PWM, data->pwm1.pwm);
  1196. }
  1197. mutex_unlock(&data->update_lock);
  1198. return count;
  1199. }
  1200. /* enable PWM fan control */
  1201. static DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm_reg, set_pwm_reg);
  1202. static DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm_reg, set_pwm_reg);
  1203. static DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm_reg, set_pwm_reg);
  1204. static DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, show_pwm_enable,
  1205. set_pwm_enable);
  1206. static DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR, show_pwm_enable,
  1207. set_pwm_enable);
  1208. static DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR, show_pwm_enable,
  1209. set_pwm_enable);
  1210. static DEVICE_ATTR(temp1_auto_point1_pwm, S_IRUGO | S_IWUSR,
  1211. show_auto_pwm_min, set_auto_pwm_min);
  1212. static DEVICE_ATTR(temp2_auto_point1_pwm, S_IRUGO | S_IWUSR,
  1213. show_auto_pwm_min, set_auto_pwm_min);
  1214. static DEVICE_ATTR(temp3_auto_point1_pwm, S_IRUGO | S_IWUSR,
  1215. show_auto_pwm_min, set_auto_pwm_min);
  1216. static DEVICE_ATTR(temp1_auto_point2_pwm, S_IRUGO, show_auto_pwm_max, NULL);
  1217. static DEVICE_ATTR(temp2_auto_point2_pwm, S_IRUGO, show_auto_pwm_max, NULL);
  1218. static DEVICE_ATTR(temp3_auto_point2_pwm, S_IRUGO, show_auto_pwm_max, NULL);
  1219. static struct attribute *adm1026_attributes[] = {
  1220. &sensor_dev_attr_in0_input.dev_attr.attr,
  1221. &sensor_dev_attr_in0_max.dev_attr.attr,
  1222. &sensor_dev_attr_in0_min.dev_attr.attr,
  1223. &sensor_dev_attr_in0_alarm.dev_attr.attr,
  1224. &sensor_dev_attr_in1_input.dev_attr.attr,
  1225. &sensor_dev_attr_in1_max.dev_attr.attr,
  1226. &sensor_dev_attr_in1_min.dev_attr.attr,
  1227. &sensor_dev_attr_in1_alarm.dev_attr.attr,
  1228. &sensor_dev_attr_in2_input.dev_attr.attr,
  1229. &sensor_dev_attr_in2_max.dev_attr.attr,
  1230. &sensor_dev_attr_in2_min.dev_attr.attr,
  1231. &sensor_dev_attr_in2_alarm.dev_attr.attr,
  1232. &sensor_dev_attr_in3_input.dev_attr.attr,
  1233. &sensor_dev_attr_in3_max.dev_attr.attr,
  1234. &sensor_dev_attr_in3_min.dev_attr.attr,
  1235. &sensor_dev_attr_in3_alarm.dev_attr.attr,
  1236. &sensor_dev_attr_in4_input.dev_attr.attr,
  1237. &sensor_dev_attr_in4_max.dev_attr.attr,
  1238. &sensor_dev_attr_in4_min.dev_attr.attr,
  1239. &sensor_dev_attr_in4_alarm.dev_attr.attr,
  1240. &sensor_dev_attr_in5_input.dev_attr.attr,
  1241. &sensor_dev_attr_in5_max.dev_attr.attr,
  1242. &sensor_dev_attr_in5_min.dev_attr.attr,
  1243. &sensor_dev_attr_in5_alarm.dev_attr.attr,
  1244. &sensor_dev_attr_in6_input.dev_attr.attr,
  1245. &sensor_dev_attr_in6_max.dev_attr.attr,
  1246. &sensor_dev_attr_in6_min.dev_attr.attr,
  1247. &sensor_dev_attr_in6_alarm.dev_attr.attr,
  1248. &sensor_dev_attr_in7_input.dev_attr.attr,
  1249. &sensor_dev_attr_in7_max.dev_attr.attr,
  1250. &sensor_dev_attr_in7_min.dev_attr.attr,
  1251. &sensor_dev_attr_in7_alarm.dev_attr.attr,
  1252. &sensor_dev_attr_in10_input.dev_attr.attr,
  1253. &sensor_dev_attr_in10_max.dev_attr.attr,
  1254. &sensor_dev_attr_in10_min.dev_attr.attr,
  1255. &sensor_dev_attr_in10_alarm.dev_attr.attr,
  1256. &sensor_dev_attr_in11_input.dev_attr.attr,
  1257. &sensor_dev_attr_in11_max.dev_attr.attr,
  1258. &sensor_dev_attr_in11_min.dev_attr.attr,
  1259. &sensor_dev_attr_in11_alarm.dev_attr.attr,
  1260. &sensor_dev_attr_in12_input.dev_attr.attr,
  1261. &sensor_dev_attr_in12_max.dev_attr.attr,
  1262. &sensor_dev_attr_in12_min.dev_attr.attr,
  1263. &sensor_dev_attr_in12_alarm.dev_attr.attr,
  1264. &sensor_dev_attr_in13_input.dev_attr.attr,
  1265. &sensor_dev_attr_in13_max.dev_attr.attr,
  1266. &sensor_dev_attr_in13_min.dev_attr.attr,
  1267. &sensor_dev_attr_in13_alarm.dev_attr.attr,
  1268. &sensor_dev_attr_in14_input.dev_attr.attr,
  1269. &sensor_dev_attr_in14_max.dev_attr.attr,
  1270. &sensor_dev_attr_in14_min.dev_attr.attr,
  1271. &sensor_dev_attr_in14_alarm.dev_attr.attr,
  1272. &sensor_dev_attr_in15_input.dev_attr.attr,
  1273. &sensor_dev_attr_in15_max.dev_attr.attr,
  1274. &sensor_dev_attr_in15_min.dev_attr.attr,
  1275. &sensor_dev_attr_in15_alarm.dev_attr.attr,
  1276. &sensor_dev_attr_in16_input.dev_attr.attr,
  1277. &sensor_dev_attr_in16_max.dev_attr.attr,
  1278. &sensor_dev_attr_in16_min.dev_attr.attr,
  1279. &sensor_dev_attr_in16_alarm.dev_attr.attr,
  1280. &sensor_dev_attr_fan1_input.dev_attr.attr,
  1281. &sensor_dev_attr_fan1_div.dev_attr.attr,
  1282. &sensor_dev_attr_fan1_min.dev_attr.attr,
  1283. &sensor_dev_attr_fan1_alarm.dev_attr.attr,
  1284. &sensor_dev_attr_fan2_input.dev_attr.attr,
  1285. &sensor_dev_attr_fan2_div.dev_attr.attr,
  1286. &sensor_dev_attr_fan2_min.dev_attr.attr,
  1287. &sensor_dev_attr_fan2_alarm.dev_attr.attr,
  1288. &sensor_dev_attr_fan3_input.dev_attr.attr,
  1289. &sensor_dev_attr_fan3_div.dev_attr.attr,
  1290. &sensor_dev_attr_fan3_min.dev_attr.attr,
  1291. &sensor_dev_attr_fan3_alarm.dev_attr.attr,
  1292. &sensor_dev_attr_fan4_input.dev_attr.attr,
  1293. &sensor_dev_attr_fan4_div.dev_attr.attr,
  1294. &sensor_dev_attr_fan4_min.dev_attr.attr,
  1295. &sensor_dev_attr_fan4_alarm.dev_attr.attr,
  1296. &sensor_dev_attr_fan5_input.dev_attr.attr,
  1297. &sensor_dev_attr_fan5_div.dev_attr.attr,
  1298. &sensor_dev_attr_fan5_min.dev_attr.attr,
  1299. &sensor_dev_attr_fan5_alarm.dev_attr.attr,
  1300. &sensor_dev_attr_fan6_input.dev_attr.attr,
  1301. &sensor_dev_attr_fan6_div.dev_attr.attr,
  1302. &sensor_dev_attr_fan6_min.dev_attr.attr,
  1303. &sensor_dev_attr_fan6_alarm.dev_attr.attr,
  1304. &sensor_dev_attr_fan7_input.dev_attr.attr,
  1305. &sensor_dev_attr_fan7_div.dev_attr.attr,
  1306. &sensor_dev_attr_fan7_min.dev_attr.attr,
  1307. &sensor_dev_attr_fan7_alarm.dev_attr.attr,
  1308. &sensor_dev_attr_fan8_input.dev_attr.attr,
  1309. &sensor_dev_attr_fan8_div.dev_attr.attr,
  1310. &sensor_dev_attr_fan8_min.dev_attr.attr,
  1311. &sensor_dev_attr_fan8_alarm.dev_attr.attr,
  1312. &sensor_dev_attr_temp1_input.dev_attr.attr,
  1313. &sensor_dev_attr_temp1_max.dev_attr.attr,
  1314. &sensor_dev_attr_temp1_min.dev_attr.attr,
  1315. &sensor_dev_attr_temp1_alarm.dev_attr.attr,
  1316. &sensor_dev_attr_temp2_input.dev_attr.attr,
  1317. &sensor_dev_attr_temp2_max.dev_attr.attr,
  1318. &sensor_dev_attr_temp2_min.dev_attr.attr,
  1319. &sensor_dev_attr_temp2_alarm.dev_attr.attr,
  1320. &sensor_dev_attr_temp1_offset.dev_attr.attr,
  1321. &sensor_dev_attr_temp2_offset.dev_attr.attr,
  1322. &sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
  1323. &sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr,
  1324. &sensor_dev_attr_temp1_auto_point1_temp_hyst.dev_attr.attr,
  1325. &sensor_dev_attr_temp2_auto_point1_temp_hyst.dev_attr.attr,
  1326. &sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr,
  1327. &sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr,
  1328. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  1329. &sensor_dev_attr_temp2_crit.dev_attr.attr,
  1330. &dev_attr_temp1_crit_enable.attr,
  1331. &dev_attr_temp2_crit_enable.attr,
  1332. &dev_attr_cpu0_vid.attr,
  1333. &dev_attr_vrm.attr,
  1334. &dev_attr_alarms.attr,
  1335. &dev_attr_alarm_mask.attr,
  1336. &dev_attr_gpio.attr,
  1337. &dev_attr_gpio_mask.attr,
  1338. &dev_attr_pwm1.attr,
  1339. &dev_attr_pwm2.attr,
  1340. &dev_attr_pwm3.attr,
  1341. &dev_attr_pwm1_enable.attr,
  1342. &dev_attr_pwm2_enable.attr,
  1343. &dev_attr_pwm3_enable.attr,
  1344. &dev_attr_temp1_auto_point1_pwm.attr,
  1345. &dev_attr_temp2_auto_point1_pwm.attr,
  1346. &dev_attr_temp1_auto_point2_pwm.attr,
  1347. &dev_attr_temp2_auto_point2_pwm.attr,
  1348. &dev_attr_analog_out.attr,
  1349. NULL
  1350. };
  1351. static const struct attribute_group adm1026_group = {
  1352. .attrs = adm1026_attributes,
  1353. };
  1354. static struct attribute *adm1026_attributes_temp3[] = {
  1355. &sensor_dev_attr_temp3_input.dev_attr.attr,
  1356. &sensor_dev_attr_temp3_max.dev_attr.attr,
  1357. &sensor_dev_attr_temp3_min.dev_attr.attr,
  1358. &sensor_dev_attr_temp3_alarm.dev_attr.attr,
  1359. &sensor_dev_attr_temp3_offset.dev_attr.attr,
  1360. &sensor_dev_attr_temp3_auto_point1_temp.dev_attr.attr,
  1361. &sensor_dev_attr_temp3_auto_point1_temp_hyst.dev_attr.attr,
  1362. &sensor_dev_attr_temp3_auto_point2_temp.dev_attr.attr,
  1363. &sensor_dev_attr_temp3_crit.dev_attr.attr,
  1364. &dev_attr_temp3_crit_enable.attr,
  1365. &dev_attr_temp3_auto_point1_pwm.attr,
  1366. &dev_attr_temp3_auto_point2_pwm.attr,
  1367. NULL
  1368. };
  1369. static const struct attribute_group adm1026_group_temp3 = {
  1370. .attrs = adm1026_attributes_temp3,
  1371. };
  1372. static struct attribute *adm1026_attributes_in8_9[] = {
  1373. &sensor_dev_attr_in8_input.dev_attr.attr,
  1374. &sensor_dev_attr_in8_max.dev_attr.attr,
  1375. &sensor_dev_attr_in8_min.dev_attr.attr,
  1376. &sensor_dev_attr_in8_alarm.dev_attr.attr,
  1377. &sensor_dev_attr_in9_input.dev_attr.attr,
  1378. &sensor_dev_attr_in9_max.dev_attr.attr,
  1379. &sensor_dev_attr_in9_min.dev_attr.attr,
  1380. &sensor_dev_attr_in9_alarm.dev_attr.attr,
  1381. NULL
  1382. };
  1383. static const struct attribute_group adm1026_group_in8_9 = {
  1384. .attrs = adm1026_attributes_in8_9,
  1385. };
  1386. /* Return 0 if detection is successful, -ENODEV otherwise */
  1387. static int adm1026_detect(struct i2c_client *client,
  1388. struct i2c_board_info *info)
  1389. {
  1390. struct i2c_adapter *adapter = client->adapter;
  1391. int address = client->addr;
  1392. int company, verstep;
  1393. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
  1394. /* We need to be able to do byte I/O */
  1395. return -ENODEV;
  1396. }
  1397. /* Now, we do the remaining detection. */
  1398. company = adm1026_read_value(client, ADM1026_REG_COMPANY);
  1399. verstep = adm1026_read_value(client, ADM1026_REG_VERSTEP);
  1400. dev_dbg(&adapter->dev,
  1401. "Detecting device at %d,0x%02x with COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
  1402. i2c_adapter_id(client->adapter), client->addr,
  1403. company, verstep);
  1404. /* Determine the chip type. */
  1405. dev_dbg(&adapter->dev, "Autodetecting device at %d,0x%02x...\n",
  1406. i2c_adapter_id(adapter), address);
  1407. if (company == ADM1026_COMPANY_ANALOG_DEV
  1408. && verstep == ADM1026_VERSTEP_ADM1026) {
  1409. /* Analog Devices ADM1026 */
  1410. } else if (company == ADM1026_COMPANY_ANALOG_DEV
  1411. && (verstep & 0xf0) == ADM1026_VERSTEP_GENERIC) {
  1412. dev_err(&adapter->dev,
  1413. "Unrecognized stepping 0x%02x. Defaulting to ADM1026.\n",
  1414. verstep);
  1415. } else if ((verstep & 0xf0) == ADM1026_VERSTEP_GENERIC) {
  1416. dev_err(&adapter->dev,
  1417. "Found version/stepping 0x%02x. Assuming generic ADM1026.\n",
  1418. verstep);
  1419. } else {
  1420. dev_dbg(&adapter->dev, "Autodetection failed\n");
  1421. /* Not an ADM1026... */
  1422. return -ENODEV;
  1423. }
  1424. strlcpy(info->type, "adm1026", I2C_NAME_SIZE);
  1425. return 0;
  1426. }
  1427. static void adm1026_print_gpio(struct i2c_client *client)
  1428. {
  1429. struct adm1026_data *data = i2c_get_clientdata(client);
  1430. int i;
  1431. dev_dbg(&client->dev, "GPIO config is:\n");
  1432. for (i = 0; i <= 7; ++i) {
  1433. if (data->config2 & (1 << i)) {
  1434. dev_dbg(&client->dev, "\t%sGP%s%d\n",
  1435. data->gpio_config[i] & 0x02 ? "" : "!",
  1436. data->gpio_config[i] & 0x01 ? "OUT" : "IN",
  1437. i);
  1438. } else {
  1439. dev_dbg(&client->dev, "\tFAN%d\n", i);
  1440. }
  1441. }
  1442. for (i = 8; i <= 15; ++i) {
  1443. dev_dbg(&client->dev, "\t%sGP%s%d\n",
  1444. data->gpio_config[i] & 0x02 ? "" : "!",
  1445. data->gpio_config[i] & 0x01 ? "OUT" : "IN",
  1446. i);
  1447. }
  1448. if (data->config3 & CFG3_GPIO16_ENABLE) {
  1449. dev_dbg(&client->dev, "\t%sGP%s16\n",
  1450. data->gpio_config[16] & 0x02 ? "" : "!",
  1451. data->gpio_config[16] & 0x01 ? "OUT" : "IN");
  1452. } else {
  1453. /* GPIO16 is THERM */
  1454. dev_dbg(&client->dev, "\tTHERM\n");
  1455. }
  1456. }
  1457. static void adm1026_fixup_gpio(struct i2c_client *client)
  1458. {
  1459. struct adm1026_data *data = i2c_get_clientdata(client);
  1460. int i;
  1461. int value;
  1462. /* Make the changes requested. */
  1463. /*
  1464. * We may need to unlock/stop monitoring or soft-reset the
  1465. * chip before we can make changes. This hasn't been
  1466. * tested much. FIXME
  1467. */
  1468. /* Make outputs */
  1469. for (i = 0; i <= 16; ++i) {
  1470. if (gpio_output[i] >= 0 && gpio_output[i] <= 16)
  1471. data->gpio_config[gpio_output[i]] |= 0x01;
  1472. /* if GPIO0-7 is output, it isn't a FAN tach */
  1473. if (gpio_output[i] >= 0 && gpio_output[i] <= 7)
  1474. data->config2 |= 1 << gpio_output[i];
  1475. }
  1476. /* Input overrides output */
  1477. for (i = 0; i <= 16; ++i) {
  1478. if (gpio_input[i] >= 0 && gpio_input[i] <= 16)
  1479. data->gpio_config[gpio_input[i]] &= ~0x01;
  1480. /* if GPIO0-7 is input, it isn't a FAN tach */
  1481. if (gpio_input[i] >= 0 && gpio_input[i] <= 7)
  1482. data->config2 |= 1 << gpio_input[i];
  1483. }
  1484. /* Inverted */
  1485. for (i = 0; i <= 16; ++i) {
  1486. if (gpio_inverted[i] >= 0 && gpio_inverted[i] <= 16)
  1487. data->gpio_config[gpio_inverted[i]] &= ~0x02;
  1488. }
  1489. /* Normal overrides inverted */
  1490. for (i = 0; i <= 16; ++i) {
  1491. if (gpio_normal[i] >= 0 && gpio_normal[i] <= 16)
  1492. data->gpio_config[gpio_normal[i]] |= 0x02;
  1493. }
  1494. /* Fan overrides input and output */
  1495. for (i = 0; i <= 7; ++i) {
  1496. if (gpio_fan[i] >= 0 && gpio_fan[i] <= 7)
  1497. data->config2 &= ~(1 << gpio_fan[i]);
  1498. }
  1499. /* Write new configs to registers */
  1500. adm1026_write_value(client, ADM1026_REG_CONFIG2, data->config2);
  1501. data->config3 = (data->config3 & 0x3f)
  1502. | ((data->gpio_config[16] & 0x03) << 6);
  1503. adm1026_write_value(client, ADM1026_REG_CONFIG3, data->config3);
  1504. for (i = 15, value = 0; i >= 0; --i) {
  1505. value <<= 2;
  1506. value |= data->gpio_config[i] & 0x03;
  1507. if ((i & 0x03) == 0) {
  1508. adm1026_write_value(client,
  1509. ADM1026_REG_GPIO_CFG_0_3 + i/4,
  1510. value);
  1511. value = 0;
  1512. }
  1513. }
  1514. /* Print the new config */
  1515. adm1026_print_gpio(client);
  1516. }
  1517. static void adm1026_init_client(struct i2c_client *client)
  1518. {
  1519. int value, i;
  1520. struct adm1026_data *data = i2c_get_clientdata(client);
  1521. dev_dbg(&client->dev, "Initializing device\n");
  1522. /* Read chip config */
  1523. data->config1 = adm1026_read_value(client, ADM1026_REG_CONFIG1);
  1524. data->config2 = adm1026_read_value(client, ADM1026_REG_CONFIG2);
  1525. data->config3 = adm1026_read_value(client, ADM1026_REG_CONFIG3);
  1526. /* Inform user of chip config */
  1527. dev_dbg(&client->dev, "ADM1026_REG_CONFIG1 is: 0x%02x\n",
  1528. data->config1);
  1529. if ((data->config1 & CFG1_MONITOR) == 0) {
  1530. dev_dbg(&client->dev,
  1531. "Monitoring not currently enabled.\n");
  1532. }
  1533. if (data->config1 & CFG1_INT_ENABLE) {
  1534. dev_dbg(&client->dev,
  1535. "SMBALERT interrupts are enabled.\n");
  1536. }
  1537. if (data->config1 & CFG1_AIN8_9) {
  1538. dev_dbg(&client->dev,
  1539. "in8 and in9 enabled. temp3 disabled.\n");
  1540. } else {
  1541. dev_dbg(&client->dev,
  1542. "temp3 enabled. in8 and in9 disabled.\n");
  1543. }
  1544. if (data->config1 & CFG1_THERM_HOT) {
  1545. dev_dbg(&client->dev,
  1546. "Automatic THERM, PWM, and temp limits enabled.\n");
  1547. }
  1548. if (data->config3 & CFG3_GPIO16_ENABLE) {
  1549. dev_dbg(&client->dev,
  1550. "GPIO16 enabled. THERM pin disabled.\n");
  1551. } else {
  1552. dev_dbg(&client->dev,
  1553. "THERM pin enabled. GPIO16 disabled.\n");
  1554. }
  1555. if (data->config3 & CFG3_VREF_250)
  1556. dev_dbg(&client->dev, "Vref is 2.50 Volts.\n");
  1557. else
  1558. dev_dbg(&client->dev, "Vref is 1.82 Volts.\n");
  1559. /* Read and pick apart the existing GPIO configuration */
  1560. value = 0;
  1561. for (i = 0; i <= 15; ++i) {
  1562. if ((i & 0x03) == 0) {
  1563. value = adm1026_read_value(client,
  1564. ADM1026_REG_GPIO_CFG_0_3 + i / 4);
  1565. }
  1566. data->gpio_config[i] = value & 0x03;
  1567. value >>= 2;
  1568. }
  1569. data->gpio_config[16] = (data->config3 >> 6) & 0x03;
  1570. /* ... and then print it */
  1571. adm1026_print_gpio(client);
  1572. /*
  1573. * If the user asks us to reprogram the GPIO config, then
  1574. * do it now.
  1575. */
  1576. if (gpio_input[0] != -1 || gpio_output[0] != -1
  1577. || gpio_inverted[0] != -1 || gpio_normal[0] != -1
  1578. || gpio_fan[0] != -1) {
  1579. adm1026_fixup_gpio(client);
  1580. }
  1581. /*
  1582. * WE INTENTIONALLY make no changes to the limits,
  1583. * offsets, pwms, fans and zones. If they were
  1584. * configured, we don't want to mess with them.
  1585. * If they weren't, the default is 100% PWM, no
  1586. * control and will suffice until 'sensors -s'
  1587. * can be run by the user. We DO set the default
  1588. * value for pwm1.auto_pwm_min to its maximum
  1589. * so that enabling automatic pwm fan control
  1590. * without first setting a value for pwm1.auto_pwm_min
  1591. * will not result in potentially dangerous fan speed decrease.
  1592. */
  1593. data->pwm1.auto_pwm_min = 255;
  1594. /* Start monitoring */
  1595. value = adm1026_read_value(client, ADM1026_REG_CONFIG1);
  1596. /* Set MONITOR, clear interrupt acknowledge and s/w reset */
  1597. value = (value | CFG1_MONITOR) & (~CFG1_INT_CLEAR & ~CFG1_RESET);
  1598. dev_dbg(&client->dev, "Setting CONFIG to: 0x%02x\n", value);
  1599. data->config1 = value;
  1600. adm1026_write_value(client, ADM1026_REG_CONFIG1, value);
  1601. /* initialize fan_div[] to hardware defaults */
  1602. value = adm1026_read_value(client, ADM1026_REG_FAN_DIV_0_3) |
  1603. (adm1026_read_value(client, ADM1026_REG_FAN_DIV_4_7) << 8);
  1604. for (i = 0; i <= 7; ++i) {
  1605. data->fan_div[i] = DIV_FROM_REG(value & 0x03);
  1606. value >>= 2;
  1607. }
  1608. }
  1609. static int adm1026_probe(struct i2c_client *client,
  1610. const struct i2c_device_id *id)
  1611. {
  1612. struct device *dev = &client->dev;
  1613. struct device *hwmon_dev;
  1614. struct adm1026_data *data;
  1615. data = devm_kzalloc(dev, sizeof(struct adm1026_data), GFP_KERNEL);
  1616. if (!data)
  1617. return -ENOMEM;
  1618. i2c_set_clientdata(client, data);
  1619. data->client = client;
  1620. mutex_init(&data->update_lock);
  1621. /* Set the VRM version */
  1622. data->vrm = vid_which_vrm();
  1623. /* Initialize the ADM1026 chip */
  1624. adm1026_init_client(client);
  1625. /* sysfs hooks */
  1626. data->groups[0] = &adm1026_group;
  1627. if (data->config1 & CFG1_AIN8_9)
  1628. data->groups[1] = &adm1026_group_in8_9;
  1629. else
  1630. data->groups[1] = &adm1026_group_temp3;
  1631. hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
  1632. data, data->groups);
  1633. return PTR_ERR_OR_ZERO(hwmon_dev);
  1634. }
  1635. static const struct i2c_device_id adm1026_id[] = {
  1636. { "adm1026", 0 },
  1637. { }
  1638. };
  1639. MODULE_DEVICE_TABLE(i2c, adm1026_id);
  1640. static struct i2c_driver adm1026_driver = {
  1641. .class = I2C_CLASS_HWMON,
  1642. .driver = {
  1643. .name = "adm1026",
  1644. },
  1645. .probe = adm1026_probe,
  1646. .id_table = adm1026_id,
  1647. .detect = adm1026_detect,
  1648. .address_list = normal_i2c,
  1649. };
  1650. module_i2c_driver(adm1026_driver);
  1651. MODULE_LICENSE("GPL");
  1652. MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, "
  1653. "Justin Thiessen <jthiessen@penguincomputing.com>");
  1654. MODULE_DESCRIPTION("ADM1026 driver");