adm1026.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905
  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, "List of GPIO pins (0-16) to program as "
  50. "outputs");
  51. module_param_array(gpio_inverted, int, NULL, 0);
  52. MODULE_PARM_DESC(gpio_inverted, "List of GPIO pins (0-16) to program as "
  53. "inverted");
  54. module_param_array(gpio_normal, int, NULL, 0);
  55. MODULE_PARM_DESC(gpio_normal, "List of GPIO pins (0-16) to program as "
  56. "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) (SENSORS_LIMIT(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. SENSORS_LIMIT(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) (SENSORS_LIMIT(((val) + ((val) < 0 ? -500 : 500)) \
  198. / 1000, -127, 127))
  199. #define TEMP_FROM_REG(val) ((val) * 1000)
  200. #define OFFSET_TO_REG(val) (SENSORS_LIMIT(((val) + ((val) < 0 ? -500 : 500)) \
  201. / 1000, -127, 127))
  202. #define OFFSET_FROM_REG(val) ((val) * 1000)
  203. #define PWM_TO_REG(val) (SENSORS_LIMIT(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) (SENSORS_LIMIT(((((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 device *hwmon_dev;
  242. struct mutex update_lock;
  243. int valid; /* !=0 if following fields are valid */
  244. unsigned long last_reading; /* In jiffies */
  245. unsigned long last_config; /* In jiffies */
  246. u8 in[17]; /* Register value */
  247. u8 in_max[17]; /* Register value */
  248. u8 in_min[17]; /* Register value */
  249. s8 temp[3]; /* Register value */
  250. s8 temp_min[3]; /* Register value */
  251. s8 temp_max[3]; /* Register value */
  252. s8 temp_tmin[3]; /* Register value */
  253. s8 temp_crit[3]; /* Register value */
  254. s8 temp_offset[3]; /* Register value */
  255. u8 fan[8]; /* Register value */
  256. u8 fan_min[8]; /* Register value */
  257. u8 fan_div[8]; /* Decoded value */
  258. struct pwm_data pwm1; /* Pwm control values */
  259. u8 vrm; /* VRM version */
  260. u8 analog_out; /* Register value (DAC) */
  261. long alarms; /* Register encoding, combined */
  262. long alarm_mask; /* Register encoding, combined */
  263. long gpio; /* Register encoding, combined */
  264. long gpio_mask; /* Register encoding, combined */
  265. u8 gpio_config[17]; /* Decoded value */
  266. u8 config1; /* Register value */
  267. u8 config2; /* Register value */
  268. u8 config3; /* Register value */
  269. };
  270. static int adm1026_probe(struct i2c_client *client,
  271. const struct i2c_device_id *id);
  272. static int adm1026_detect(struct i2c_client *client,
  273. struct i2c_board_info *info);
  274. static int adm1026_remove(struct i2c_client *client);
  275. static int adm1026_read_value(struct i2c_client *client, u8 reg);
  276. static int adm1026_write_value(struct i2c_client *client, u8 reg, int value);
  277. static void adm1026_print_gpio(struct i2c_client *client);
  278. static void adm1026_fixup_gpio(struct i2c_client *client);
  279. static struct adm1026_data *adm1026_update_device(struct device *dev);
  280. static void adm1026_init_client(struct i2c_client *client);
  281. static const struct i2c_device_id adm1026_id[] = {
  282. { "adm1026", 0 },
  283. { }
  284. };
  285. MODULE_DEVICE_TABLE(i2c, adm1026_id);
  286. static struct i2c_driver adm1026_driver = {
  287. .class = I2C_CLASS_HWMON,
  288. .driver = {
  289. .name = "adm1026",
  290. },
  291. .probe = adm1026_probe,
  292. .remove = adm1026_remove,
  293. .id_table = adm1026_id,
  294. .detect = adm1026_detect,
  295. .address_list = normal_i2c,
  296. };
  297. static int adm1026_read_value(struct i2c_client *client, u8 reg)
  298. {
  299. int res;
  300. if (reg < 0x80) {
  301. /* "RAM" locations */
  302. res = i2c_smbus_read_byte_data(client, reg) & 0xff;
  303. } else {
  304. /* EEPROM, do nothing */
  305. res = 0;
  306. }
  307. return res;
  308. }
  309. static int adm1026_write_value(struct i2c_client *client, u8 reg, int value)
  310. {
  311. int res;
  312. if (reg < 0x80) {
  313. /* "RAM" locations */
  314. res = i2c_smbus_write_byte_data(client, reg, value);
  315. } else {
  316. /* EEPROM, do nothing */
  317. res = 0;
  318. }
  319. return res;
  320. }
  321. static void adm1026_init_client(struct i2c_client *client)
  322. {
  323. int value, i;
  324. struct adm1026_data *data = i2c_get_clientdata(client);
  325. dev_dbg(&client->dev, "Initializing device\n");
  326. /* Read chip config */
  327. data->config1 = adm1026_read_value(client, ADM1026_REG_CONFIG1);
  328. data->config2 = adm1026_read_value(client, ADM1026_REG_CONFIG2);
  329. data->config3 = adm1026_read_value(client, ADM1026_REG_CONFIG3);
  330. /* Inform user of chip config */
  331. dev_dbg(&client->dev, "ADM1026_REG_CONFIG1 is: 0x%02x\n",
  332. data->config1);
  333. if ((data->config1 & CFG1_MONITOR) == 0) {
  334. dev_dbg(&client->dev, "Monitoring not currently "
  335. "enabled.\n");
  336. }
  337. if (data->config1 & CFG1_INT_ENABLE) {
  338. dev_dbg(&client->dev, "SMBALERT interrupts are "
  339. "enabled.\n");
  340. }
  341. if (data->config1 & CFG1_AIN8_9) {
  342. dev_dbg(&client->dev, "in8 and in9 enabled. "
  343. "temp3 disabled.\n");
  344. } else {
  345. dev_dbg(&client->dev, "temp3 enabled. in8 and "
  346. "in9 disabled.\n");
  347. }
  348. if (data->config1 & CFG1_THERM_HOT) {
  349. dev_dbg(&client->dev, "Automatic THERM, PWM, "
  350. "and temp limits enabled.\n");
  351. }
  352. if (data->config3 & CFG3_GPIO16_ENABLE) {
  353. dev_dbg(&client->dev, "GPIO16 enabled. THERM "
  354. "pin disabled.\n");
  355. } else {
  356. dev_dbg(&client->dev, "THERM pin enabled. "
  357. "GPIO16 disabled.\n");
  358. }
  359. if (data->config3 & CFG3_VREF_250)
  360. dev_dbg(&client->dev, "Vref is 2.50 Volts.\n");
  361. else
  362. dev_dbg(&client->dev, "Vref is 1.82 Volts.\n");
  363. /* Read and pick apart the existing GPIO configuration */
  364. value = 0;
  365. for (i = 0; i <= 15; ++i) {
  366. if ((i & 0x03) == 0) {
  367. value = adm1026_read_value(client,
  368. ADM1026_REG_GPIO_CFG_0_3 + i / 4);
  369. }
  370. data->gpio_config[i] = value & 0x03;
  371. value >>= 2;
  372. }
  373. data->gpio_config[16] = (data->config3 >> 6) & 0x03;
  374. /* ... and then print it */
  375. adm1026_print_gpio(client);
  376. /*
  377. * If the user asks us to reprogram the GPIO config, then
  378. * do it now.
  379. */
  380. if (gpio_input[0] != -1 || gpio_output[0] != -1
  381. || gpio_inverted[0] != -1 || gpio_normal[0] != -1
  382. || gpio_fan[0] != -1) {
  383. adm1026_fixup_gpio(client);
  384. }
  385. /*
  386. * WE INTENTIONALLY make no changes to the limits,
  387. * offsets, pwms, fans and zones. If they were
  388. * configured, we don't want to mess with them.
  389. * If they weren't, the default is 100% PWM, no
  390. * control and will suffice until 'sensors -s'
  391. * can be run by the user. We DO set the default
  392. * value for pwm1.auto_pwm_min to its maximum
  393. * so that enabling automatic pwm fan control
  394. * without first setting a value for pwm1.auto_pwm_min
  395. * will not result in potentially dangerous fan speed decrease.
  396. */
  397. data->pwm1.auto_pwm_min = 255;
  398. /* Start monitoring */
  399. value = adm1026_read_value(client, ADM1026_REG_CONFIG1);
  400. /* Set MONITOR, clear interrupt acknowledge and s/w reset */
  401. value = (value | CFG1_MONITOR) & (~CFG1_INT_CLEAR & ~CFG1_RESET);
  402. dev_dbg(&client->dev, "Setting CONFIG to: 0x%02x\n", value);
  403. data->config1 = value;
  404. adm1026_write_value(client, ADM1026_REG_CONFIG1, value);
  405. /* initialize fan_div[] to hardware defaults */
  406. value = adm1026_read_value(client, ADM1026_REG_FAN_DIV_0_3) |
  407. (adm1026_read_value(client, ADM1026_REG_FAN_DIV_4_7) << 8);
  408. for (i = 0; i <= 7; ++i) {
  409. data->fan_div[i] = DIV_FROM_REG(value & 0x03);
  410. value >>= 2;
  411. }
  412. }
  413. static void adm1026_print_gpio(struct i2c_client *client)
  414. {
  415. struct adm1026_data *data = i2c_get_clientdata(client);
  416. int i;
  417. dev_dbg(&client->dev, "GPIO config is:\n");
  418. for (i = 0; i <= 7; ++i) {
  419. if (data->config2 & (1 << i)) {
  420. dev_dbg(&client->dev, "\t%sGP%s%d\n",
  421. data->gpio_config[i] & 0x02 ? "" : "!",
  422. data->gpio_config[i] & 0x01 ? "OUT" : "IN",
  423. i);
  424. } else {
  425. dev_dbg(&client->dev, "\tFAN%d\n", i);
  426. }
  427. }
  428. for (i = 8; i <= 15; ++i) {
  429. dev_dbg(&client->dev, "\t%sGP%s%d\n",
  430. data->gpio_config[i] & 0x02 ? "" : "!",
  431. data->gpio_config[i] & 0x01 ? "OUT" : "IN",
  432. i);
  433. }
  434. if (data->config3 & CFG3_GPIO16_ENABLE) {
  435. dev_dbg(&client->dev, "\t%sGP%s16\n",
  436. data->gpio_config[16] & 0x02 ? "" : "!",
  437. data->gpio_config[16] & 0x01 ? "OUT" : "IN");
  438. } else {
  439. /* GPIO16 is THERM */
  440. dev_dbg(&client->dev, "\tTHERM\n");
  441. }
  442. }
  443. static void adm1026_fixup_gpio(struct i2c_client *client)
  444. {
  445. struct adm1026_data *data = i2c_get_clientdata(client);
  446. int i;
  447. int value;
  448. /* Make the changes requested. */
  449. /*
  450. * We may need to unlock/stop monitoring or soft-reset the
  451. * chip before we can make changes. This hasn't been
  452. * tested much. FIXME
  453. */
  454. /* Make outputs */
  455. for (i = 0; i <= 16; ++i) {
  456. if (gpio_output[i] >= 0 && gpio_output[i] <= 16)
  457. data->gpio_config[gpio_output[i]] |= 0x01;
  458. /* if GPIO0-7 is output, it isn't a FAN tach */
  459. if (gpio_output[i] >= 0 && gpio_output[i] <= 7)
  460. data->config2 |= 1 << gpio_output[i];
  461. }
  462. /* Input overrides output */
  463. for (i = 0; i <= 16; ++i) {
  464. if (gpio_input[i] >= 0 && gpio_input[i] <= 16)
  465. data->gpio_config[gpio_input[i]] &= ~0x01;
  466. /* if GPIO0-7 is input, it isn't a FAN tach */
  467. if (gpio_input[i] >= 0 && gpio_input[i] <= 7)
  468. data->config2 |= 1 << gpio_input[i];
  469. }
  470. /* Inverted */
  471. for (i = 0; i <= 16; ++i) {
  472. if (gpio_inverted[i] >= 0 && gpio_inverted[i] <= 16)
  473. data->gpio_config[gpio_inverted[i]] &= ~0x02;
  474. }
  475. /* Normal overrides inverted */
  476. for (i = 0; i <= 16; ++i) {
  477. if (gpio_normal[i] >= 0 && gpio_normal[i] <= 16)
  478. data->gpio_config[gpio_normal[i]] |= 0x02;
  479. }
  480. /* Fan overrides input and output */
  481. for (i = 0; i <= 7; ++i) {
  482. if (gpio_fan[i] >= 0 && gpio_fan[i] <= 7)
  483. data->config2 &= ~(1 << gpio_fan[i]);
  484. }
  485. /* Write new configs to registers */
  486. adm1026_write_value(client, ADM1026_REG_CONFIG2, data->config2);
  487. data->config3 = (data->config3 & 0x3f)
  488. | ((data->gpio_config[16] & 0x03) << 6);
  489. adm1026_write_value(client, ADM1026_REG_CONFIG3, data->config3);
  490. for (i = 15, value = 0; i >= 0; --i) {
  491. value <<= 2;
  492. value |= data->gpio_config[i] & 0x03;
  493. if ((i & 0x03) == 0) {
  494. adm1026_write_value(client,
  495. ADM1026_REG_GPIO_CFG_0_3 + i/4,
  496. value);
  497. value = 0;
  498. }
  499. }
  500. /* Print the new config */
  501. adm1026_print_gpio(client);
  502. }
  503. static struct adm1026_data *adm1026_update_device(struct device *dev)
  504. {
  505. struct i2c_client *client = to_i2c_client(dev);
  506. struct adm1026_data *data = i2c_get_clientdata(client);
  507. int i;
  508. long value, alarms, gpio;
  509. mutex_lock(&data->update_lock);
  510. if (!data->valid
  511. || time_after(jiffies,
  512. data->last_reading + ADM1026_DATA_INTERVAL)) {
  513. /* Things that change quickly */
  514. dev_dbg(&client->dev, "Reading sensor values\n");
  515. for (i = 0; i <= 16; ++i) {
  516. data->in[i] =
  517. adm1026_read_value(client, ADM1026_REG_IN[i]);
  518. }
  519. for (i = 0; i <= 7; ++i) {
  520. data->fan[i] =
  521. adm1026_read_value(client, ADM1026_REG_FAN(i));
  522. }
  523. for (i = 0; i <= 2; ++i) {
  524. /*
  525. * NOTE: temp[] is s8 and we assume 2's complement
  526. * "conversion" in the assignment
  527. */
  528. data->temp[i] =
  529. adm1026_read_value(client, ADM1026_REG_TEMP[i]);
  530. }
  531. data->pwm1.pwm = adm1026_read_value(client,
  532. ADM1026_REG_PWM);
  533. data->analog_out = adm1026_read_value(client,
  534. ADM1026_REG_DAC);
  535. /* GPIO16 is MSbit of alarms, move it to gpio */
  536. alarms = adm1026_read_value(client, ADM1026_REG_STATUS4);
  537. gpio = alarms & 0x80 ? 0x0100 : 0; /* GPIO16 */
  538. alarms &= 0x7f;
  539. alarms <<= 8;
  540. alarms |= adm1026_read_value(client, ADM1026_REG_STATUS3);
  541. alarms <<= 8;
  542. alarms |= adm1026_read_value(client, ADM1026_REG_STATUS2);
  543. alarms <<= 8;
  544. alarms |= adm1026_read_value(client, ADM1026_REG_STATUS1);
  545. data->alarms = alarms;
  546. /* Read the GPIO values */
  547. gpio |= adm1026_read_value(client,
  548. ADM1026_REG_GPIO_STATUS_8_15);
  549. gpio <<= 8;
  550. gpio |= adm1026_read_value(client,
  551. ADM1026_REG_GPIO_STATUS_0_7);
  552. data->gpio = gpio;
  553. data->last_reading = jiffies;
  554. }; /* last_reading */
  555. if (!data->valid ||
  556. time_after(jiffies, data->last_config + ADM1026_CONFIG_INTERVAL)) {
  557. /* Things that don't change often */
  558. dev_dbg(&client->dev, "Reading config values\n");
  559. for (i = 0; i <= 16; ++i) {
  560. data->in_min[i] = adm1026_read_value(client,
  561. ADM1026_REG_IN_MIN[i]);
  562. data->in_max[i] = adm1026_read_value(client,
  563. ADM1026_REG_IN_MAX[i]);
  564. }
  565. value = adm1026_read_value(client, ADM1026_REG_FAN_DIV_0_3)
  566. | (adm1026_read_value(client, ADM1026_REG_FAN_DIV_4_7)
  567. << 8);
  568. for (i = 0; i <= 7; ++i) {
  569. data->fan_min[i] = adm1026_read_value(client,
  570. ADM1026_REG_FAN_MIN(i));
  571. data->fan_div[i] = DIV_FROM_REG(value & 0x03);
  572. value >>= 2;
  573. }
  574. for (i = 0; i <= 2; ++i) {
  575. /*
  576. * NOTE: temp_xxx[] are s8 and we assume 2's
  577. * complement "conversion" in the assignment
  578. */
  579. data->temp_min[i] = adm1026_read_value(client,
  580. ADM1026_REG_TEMP_MIN[i]);
  581. data->temp_max[i] = adm1026_read_value(client,
  582. ADM1026_REG_TEMP_MAX[i]);
  583. data->temp_tmin[i] = adm1026_read_value(client,
  584. ADM1026_REG_TEMP_TMIN[i]);
  585. data->temp_crit[i] = adm1026_read_value(client,
  586. ADM1026_REG_TEMP_THERM[i]);
  587. data->temp_offset[i] = adm1026_read_value(client,
  588. ADM1026_REG_TEMP_OFFSET[i]);
  589. }
  590. /* Read the STATUS/alarm masks */
  591. alarms = adm1026_read_value(client, ADM1026_REG_MASK4);
  592. gpio = alarms & 0x80 ? 0x0100 : 0; /* GPIO16 */
  593. alarms = (alarms & 0x7f) << 8;
  594. alarms |= adm1026_read_value(client, ADM1026_REG_MASK3);
  595. alarms <<= 8;
  596. alarms |= adm1026_read_value(client, ADM1026_REG_MASK2);
  597. alarms <<= 8;
  598. alarms |= adm1026_read_value(client, ADM1026_REG_MASK1);
  599. data->alarm_mask = alarms;
  600. /* Read the GPIO values */
  601. gpio |= adm1026_read_value(client,
  602. ADM1026_REG_GPIO_MASK_8_15);
  603. gpio <<= 8;
  604. gpio |= adm1026_read_value(client, ADM1026_REG_GPIO_MASK_0_7);
  605. data->gpio_mask = gpio;
  606. /* Read various values from CONFIG1 */
  607. data->config1 = adm1026_read_value(client,
  608. ADM1026_REG_CONFIG1);
  609. if (data->config1 & CFG1_PWM_AFC) {
  610. data->pwm1.enable = 2;
  611. data->pwm1.auto_pwm_min =
  612. PWM_MIN_FROM_REG(data->pwm1.pwm);
  613. }
  614. /* Read the GPIO config */
  615. data->config2 = adm1026_read_value(client,
  616. ADM1026_REG_CONFIG2);
  617. data->config3 = adm1026_read_value(client,
  618. ADM1026_REG_CONFIG3);
  619. data->gpio_config[16] = (data->config3 >> 6) & 0x03;
  620. value = 0;
  621. for (i = 0; i <= 15; ++i) {
  622. if ((i & 0x03) == 0) {
  623. value = adm1026_read_value(client,
  624. ADM1026_REG_GPIO_CFG_0_3 + i/4);
  625. }
  626. data->gpio_config[i] = value & 0x03;
  627. value >>= 2;
  628. }
  629. data->last_config = jiffies;
  630. }; /* last_config */
  631. data->valid = 1;
  632. mutex_unlock(&data->update_lock);
  633. return data;
  634. }
  635. static ssize_t show_in(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", INS_FROM_REG(nr, data->in[nr]));
  642. }
  643. static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
  644. char *buf)
  645. {
  646. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  647. int nr = sensor_attr->index;
  648. struct adm1026_data *data = adm1026_update_device(dev);
  649. return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_min[nr]));
  650. }
  651. static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
  652. const char *buf, size_t count)
  653. {
  654. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  655. int nr = sensor_attr->index;
  656. struct i2c_client *client = to_i2c_client(dev);
  657. struct adm1026_data *data = i2c_get_clientdata(client);
  658. long val;
  659. int err;
  660. err = kstrtol(buf, 10, &val);
  661. if (err)
  662. return err;
  663. mutex_lock(&data->update_lock);
  664. data->in_min[nr] = INS_TO_REG(nr, val);
  665. adm1026_write_value(client, ADM1026_REG_IN_MIN[nr], data->in_min[nr]);
  666. mutex_unlock(&data->update_lock);
  667. return count;
  668. }
  669. static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
  670. char *buf)
  671. {
  672. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  673. int nr = sensor_attr->index;
  674. struct adm1026_data *data = adm1026_update_device(dev);
  675. return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_max[nr]));
  676. }
  677. static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
  678. const char *buf, size_t count)
  679. {
  680. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  681. int nr = sensor_attr->index;
  682. struct i2c_client *client = to_i2c_client(dev);
  683. struct adm1026_data *data = i2c_get_clientdata(client);
  684. long val;
  685. int err;
  686. err = kstrtol(buf, 10, &val);
  687. if (err)
  688. return err;
  689. mutex_lock(&data->update_lock);
  690. data->in_max[nr] = INS_TO_REG(nr, val);
  691. adm1026_write_value(client, ADM1026_REG_IN_MAX[nr], data->in_max[nr]);
  692. mutex_unlock(&data->update_lock);
  693. return count;
  694. }
  695. #define in_reg(offset) \
  696. static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, show_in, \
  697. NULL, offset); \
  698. static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
  699. show_in_min, set_in_min, offset); \
  700. static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
  701. show_in_max, set_in_max, offset);
  702. in_reg(0);
  703. in_reg(1);
  704. in_reg(2);
  705. in_reg(3);
  706. in_reg(4);
  707. in_reg(5);
  708. in_reg(6);
  709. in_reg(7);
  710. in_reg(8);
  711. in_reg(9);
  712. in_reg(10);
  713. in_reg(11);
  714. in_reg(12);
  715. in_reg(13);
  716. in_reg(14);
  717. in_reg(15);
  718. static ssize_t show_in16(struct device *dev, struct device_attribute *attr,
  719. char *buf)
  720. {
  721. struct adm1026_data *data = adm1026_update_device(dev);
  722. return sprintf(buf, "%d\n", INS_FROM_REG(16, data->in[16]) -
  723. NEG12_OFFSET);
  724. }
  725. static ssize_t show_in16_min(struct device *dev, struct device_attribute *attr,
  726. char *buf)
  727. {
  728. struct adm1026_data *data = adm1026_update_device(dev);
  729. return sprintf(buf, "%d\n", INS_FROM_REG(16, data->in_min[16])
  730. - NEG12_OFFSET);
  731. }
  732. static ssize_t set_in16_min(struct device *dev, struct device_attribute *attr,
  733. const char *buf, size_t count)
  734. {
  735. struct i2c_client *client = to_i2c_client(dev);
  736. struct adm1026_data *data = i2c_get_clientdata(client);
  737. long val;
  738. int err;
  739. err = kstrtol(buf, 10, &val);
  740. if (err)
  741. return err;
  742. mutex_lock(&data->update_lock);
  743. data->in_min[16] = INS_TO_REG(16, val + NEG12_OFFSET);
  744. adm1026_write_value(client, ADM1026_REG_IN_MIN[16], data->in_min[16]);
  745. mutex_unlock(&data->update_lock);
  746. return count;
  747. }
  748. static ssize_t show_in16_max(struct device *dev, struct device_attribute *attr,
  749. char *buf)
  750. {
  751. struct adm1026_data *data = adm1026_update_device(dev);
  752. return sprintf(buf, "%d\n", INS_FROM_REG(16, data->in_max[16])
  753. - NEG12_OFFSET);
  754. }
  755. static ssize_t set_in16_max(struct device *dev, struct device_attribute *attr,
  756. const char *buf, size_t count)
  757. {
  758. struct i2c_client *client = to_i2c_client(dev);
  759. struct adm1026_data *data = i2c_get_clientdata(client);
  760. long val;
  761. int err;
  762. err = kstrtol(buf, 10, &val);
  763. if (err)
  764. return err;
  765. mutex_lock(&data->update_lock);
  766. data->in_max[16] = INS_TO_REG(16, val+NEG12_OFFSET);
  767. adm1026_write_value(client, ADM1026_REG_IN_MAX[16], data->in_max[16]);
  768. mutex_unlock(&data->update_lock);
  769. return count;
  770. }
  771. static SENSOR_DEVICE_ATTR(in16_input, S_IRUGO, show_in16, NULL, 16);
  772. static SENSOR_DEVICE_ATTR(in16_min, S_IRUGO | S_IWUSR, show_in16_min,
  773. set_in16_min, 16);
  774. static SENSOR_DEVICE_ATTR(in16_max, S_IRUGO | S_IWUSR, show_in16_max,
  775. set_in16_max, 16);
  776. /* Now add fan read/write functions */
  777. static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
  778. char *buf)
  779. {
  780. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  781. int nr = sensor_attr->index;
  782. struct adm1026_data *data = adm1026_update_device(dev);
  783. return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
  784. data->fan_div[nr]));
  785. }
  786. static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
  787. char *buf)
  788. {
  789. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  790. int nr = sensor_attr->index;
  791. struct adm1026_data *data = adm1026_update_device(dev);
  792. return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
  793. data->fan_div[nr]));
  794. }
  795. static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
  796. const char *buf, size_t count)
  797. {
  798. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  799. int nr = sensor_attr->index;
  800. struct i2c_client *client = to_i2c_client(dev);
  801. struct adm1026_data *data = i2c_get_clientdata(client);
  802. long val;
  803. int err;
  804. err = kstrtol(buf, 10, &val);
  805. if (err)
  806. return err;
  807. mutex_lock(&data->update_lock);
  808. data->fan_min[nr] = FAN_TO_REG(val, data->fan_div[nr]);
  809. adm1026_write_value(client, ADM1026_REG_FAN_MIN(nr),
  810. data->fan_min[nr]);
  811. mutex_unlock(&data->update_lock);
  812. return count;
  813. }
  814. #define fan_offset(offset) \
  815. static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan, NULL, \
  816. offset - 1); \
  817. static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
  818. show_fan_min, set_fan_min, offset - 1);
  819. fan_offset(1);
  820. fan_offset(2);
  821. fan_offset(3);
  822. fan_offset(4);
  823. fan_offset(5);
  824. fan_offset(6);
  825. fan_offset(7);
  826. fan_offset(8);
  827. /* Adjust fan_min to account for new fan divisor */
  828. static void fixup_fan_min(struct device *dev, int fan, int old_div)
  829. {
  830. struct i2c_client *client = to_i2c_client(dev);
  831. struct adm1026_data *data = i2c_get_clientdata(client);
  832. int new_min;
  833. int new_div = data->fan_div[fan];
  834. /* 0 and 0xff are special. Don't adjust them */
  835. if (data->fan_min[fan] == 0 || data->fan_min[fan] == 0xff)
  836. return;
  837. new_min = data->fan_min[fan] * old_div / new_div;
  838. new_min = SENSORS_LIMIT(new_min, 1, 254);
  839. data->fan_min[fan] = new_min;
  840. adm1026_write_value(client, ADM1026_REG_FAN_MIN(fan), new_min);
  841. }
  842. /* Now add fan_div read/write functions */
  843. static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
  844. char *buf)
  845. {
  846. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  847. int nr = sensor_attr->index;
  848. struct adm1026_data *data = adm1026_update_device(dev);
  849. return sprintf(buf, "%d\n", data->fan_div[nr]);
  850. }
  851. static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
  852. const char *buf, size_t count)
  853. {
  854. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  855. int nr = sensor_attr->index;
  856. struct i2c_client *client = to_i2c_client(dev);
  857. struct adm1026_data *data = i2c_get_clientdata(client);
  858. long val;
  859. int orig_div, new_div;
  860. int err;
  861. err = kstrtol(buf, 10, &val);
  862. if (err)
  863. return err;
  864. new_div = DIV_TO_REG(val);
  865. mutex_lock(&data->update_lock);
  866. orig_div = data->fan_div[nr];
  867. data->fan_div[nr] = DIV_FROM_REG(new_div);
  868. if (nr < 4) { /* 0 <= nr < 4 */
  869. adm1026_write_value(client, ADM1026_REG_FAN_DIV_0_3,
  870. (DIV_TO_REG(data->fan_div[0]) << 0) |
  871. (DIV_TO_REG(data->fan_div[1]) << 2) |
  872. (DIV_TO_REG(data->fan_div[2]) << 4) |
  873. (DIV_TO_REG(data->fan_div[3]) << 6));
  874. } else { /* 3 < nr < 8 */
  875. adm1026_write_value(client, ADM1026_REG_FAN_DIV_4_7,
  876. (DIV_TO_REG(data->fan_div[4]) << 0) |
  877. (DIV_TO_REG(data->fan_div[5]) << 2) |
  878. (DIV_TO_REG(data->fan_div[6]) << 4) |
  879. (DIV_TO_REG(data->fan_div[7]) << 6));
  880. }
  881. if (data->fan_div[nr] != orig_div)
  882. fixup_fan_min(dev, nr, orig_div);
  883. mutex_unlock(&data->update_lock);
  884. return count;
  885. }
  886. #define fan_offset_div(offset) \
  887. static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
  888. show_fan_div, set_fan_div, offset - 1);
  889. fan_offset_div(1);
  890. fan_offset_div(2);
  891. fan_offset_div(3);
  892. fan_offset_div(4);
  893. fan_offset_div(5);
  894. fan_offset_div(6);
  895. fan_offset_div(7);
  896. fan_offset_div(8);
  897. /* Temps */
  898. static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
  899. char *buf)
  900. {
  901. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  902. int nr = sensor_attr->index;
  903. struct adm1026_data *data = adm1026_update_device(dev);
  904. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
  905. }
  906. static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
  907. char *buf)
  908. {
  909. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  910. int nr = sensor_attr->index;
  911. struct adm1026_data *data = adm1026_update_device(dev);
  912. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
  913. }
  914. static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
  915. const char *buf, size_t count)
  916. {
  917. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  918. int nr = sensor_attr->index;
  919. struct i2c_client *client = to_i2c_client(dev);
  920. struct adm1026_data *data = i2c_get_clientdata(client);
  921. long val;
  922. int err;
  923. err = kstrtol(buf, 10, &val);
  924. if (err)
  925. return err;
  926. mutex_lock(&data->update_lock);
  927. data->temp_min[nr] = TEMP_TO_REG(val);
  928. adm1026_write_value(client, ADM1026_REG_TEMP_MIN[nr],
  929. data->temp_min[nr]);
  930. mutex_unlock(&data->update_lock);
  931. return count;
  932. }
  933. static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
  934. char *buf)
  935. {
  936. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  937. int nr = sensor_attr->index;
  938. struct adm1026_data *data = adm1026_update_device(dev);
  939. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
  940. }
  941. static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
  942. const char *buf, size_t count)
  943. {
  944. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  945. int nr = sensor_attr->index;
  946. struct i2c_client *client = to_i2c_client(dev);
  947. struct adm1026_data *data = i2c_get_clientdata(client);
  948. long val;
  949. int err;
  950. err = kstrtol(buf, 10, &val);
  951. if (err)
  952. return err;
  953. mutex_lock(&data->update_lock);
  954. data->temp_max[nr] = TEMP_TO_REG(val);
  955. adm1026_write_value(client, ADM1026_REG_TEMP_MAX[nr],
  956. data->temp_max[nr]);
  957. mutex_unlock(&data->update_lock);
  958. return count;
  959. }
  960. #define temp_reg(offset) \
  961. static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp, \
  962. NULL, offset - 1); \
  963. static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
  964. show_temp_min, set_temp_min, offset - 1); \
  965. static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
  966. show_temp_max, set_temp_max, offset - 1);
  967. temp_reg(1);
  968. temp_reg(2);
  969. temp_reg(3);
  970. static ssize_t show_temp_offset(struct device *dev,
  971. struct device_attribute *attr, char *buf)
  972. {
  973. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  974. int nr = sensor_attr->index;
  975. struct adm1026_data *data = adm1026_update_device(dev);
  976. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_offset[nr]));
  977. }
  978. static ssize_t set_temp_offset(struct device *dev,
  979. struct device_attribute *attr, const char *buf,
  980. size_t count)
  981. {
  982. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  983. int nr = sensor_attr->index;
  984. struct i2c_client *client = to_i2c_client(dev);
  985. struct adm1026_data *data = i2c_get_clientdata(client);
  986. long val;
  987. int err;
  988. err = kstrtol(buf, 10, &val);
  989. if (err)
  990. return err;
  991. mutex_lock(&data->update_lock);
  992. data->temp_offset[nr] = TEMP_TO_REG(val);
  993. adm1026_write_value(client, ADM1026_REG_TEMP_OFFSET[nr],
  994. data->temp_offset[nr]);
  995. mutex_unlock(&data->update_lock);
  996. return count;
  997. }
  998. #define temp_offset_reg(offset) \
  999. static SENSOR_DEVICE_ATTR(temp##offset##_offset, S_IRUGO | S_IWUSR, \
  1000. show_temp_offset, set_temp_offset, offset - 1);
  1001. temp_offset_reg(1);
  1002. temp_offset_reg(2);
  1003. temp_offset_reg(3);
  1004. static ssize_t show_temp_auto_point1_temp_hyst(struct device *dev,
  1005. struct device_attribute *attr, char *buf)
  1006. {
  1007. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  1008. int nr = sensor_attr->index;
  1009. struct adm1026_data *data = adm1026_update_device(dev);
  1010. return sprintf(buf, "%d\n", TEMP_FROM_REG(
  1011. ADM1026_FAN_ACTIVATION_TEMP_HYST + data->temp_tmin[nr]));
  1012. }
  1013. static ssize_t show_temp_auto_point2_temp(struct device *dev,
  1014. struct device_attribute *attr, char *buf)
  1015. {
  1016. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  1017. int nr = sensor_attr->index;
  1018. struct adm1026_data *data = adm1026_update_device(dev);
  1019. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_tmin[nr] +
  1020. ADM1026_FAN_CONTROL_TEMP_RANGE));
  1021. }
  1022. static ssize_t show_temp_auto_point1_temp(struct device *dev,
  1023. struct device_attribute *attr, char *buf)
  1024. {
  1025. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  1026. int nr = sensor_attr->index;
  1027. struct adm1026_data *data = adm1026_update_device(dev);
  1028. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_tmin[nr]));
  1029. }
  1030. static ssize_t set_temp_auto_point1_temp(struct device *dev,
  1031. struct device_attribute *attr, const char *buf, size_t count)
  1032. {
  1033. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  1034. int nr = sensor_attr->index;
  1035. struct i2c_client *client = to_i2c_client(dev);
  1036. struct adm1026_data *data = i2c_get_clientdata(client);
  1037. long val;
  1038. int err;
  1039. err = kstrtol(buf, 10, &val);
  1040. if (err)
  1041. return err;
  1042. mutex_lock(&data->update_lock);
  1043. data->temp_tmin[nr] = TEMP_TO_REG(val);
  1044. adm1026_write_value(client, ADM1026_REG_TEMP_TMIN[nr],
  1045. data->temp_tmin[nr]);
  1046. mutex_unlock(&data->update_lock);
  1047. return count;
  1048. }
  1049. #define temp_auto_point(offset) \
  1050. static SENSOR_DEVICE_ATTR(temp##offset##_auto_point1_temp, \
  1051. S_IRUGO | S_IWUSR, show_temp_auto_point1_temp, \
  1052. set_temp_auto_point1_temp, offset - 1); \
  1053. static SENSOR_DEVICE_ATTR(temp##offset##_auto_point1_temp_hyst, S_IRUGO,\
  1054. show_temp_auto_point1_temp_hyst, NULL, offset - 1); \
  1055. static SENSOR_DEVICE_ATTR(temp##offset##_auto_point2_temp, S_IRUGO, \
  1056. show_temp_auto_point2_temp, NULL, offset - 1);
  1057. temp_auto_point(1);
  1058. temp_auto_point(2);
  1059. temp_auto_point(3);
  1060. static ssize_t show_temp_crit_enable(struct device *dev,
  1061. struct device_attribute *attr, char *buf)
  1062. {
  1063. struct adm1026_data *data = adm1026_update_device(dev);
  1064. return sprintf(buf, "%d\n", (data->config1 & CFG1_THERM_HOT) >> 4);
  1065. }
  1066. static ssize_t set_temp_crit_enable(struct device *dev,
  1067. struct device_attribute *attr, const char *buf, size_t count)
  1068. {
  1069. struct i2c_client *client = to_i2c_client(dev);
  1070. struct adm1026_data *data = i2c_get_clientdata(client);
  1071. unsigned long val;
  1072. int err;
  1073. err = kstrtoul(buf, 10, &val);
  1074. if (err)
  1075. return err;
  1076. if (val > 1)
  1077. return -EINVAL;
  1078. mutex_lock(&data->update_lock);
  1079. data->config1 = (data->config1 & ~CFG1_THERM_HOT) | (val << 4);
  1080. adm1026_write_value(client, ADM1026_REG_CONFIG1, data->config1);
  1081. mutex_unlock(&data->update_lock);
  1082. return count;
  1083. }
  1084. #define temp_crit_enable(offset) \
  1085. static DEVICE_ATTR(temp##offset##_crit_enable, S_IRUGO | S_IWUSR, \
  1086. show_temp_crit_enable, set_temp_crit_enable);
  1087. temp_crit_enable(1);
  1088. temp_crit_enable(2);
  1089. temp_crit_enable(3);
  1090. static ssize_t show_temp_crit(struct device *dev,
  1091. struct device_attribute *attr, char *buf)
  1092. {
  1093. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  1094. int nr = sensor_attr->index;
  1095. struct adm1026_data *data = adm1026_update_device(dev);
  1096. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit[nr]));
  1097. }
  1098. static ssize_t set_temp_crit(struct device *dev, struct device_attribute *attr,
  1099. const char *buf, size_t count)
  1100. {
  1101. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  1102. int nr = sensor_attr->index;
  1103. struct i2c_client *client = to_i2c_client(dev);
  1104. struct adm1026_data *data = i2c_get_clientdata(client);
  1105. long val;
  1106. int err;
  1107. err = kstrtol(buf, 10, &val);
  1108. if (err)
  1109. return err;
  1110. mutex_lock(&data->update_lock);
  1111. data->temp_crit[nr] = TEMP_TO_REG(val);
  1112. adm1026_write_value(client, ADM1026_REG_TEMP_THERM[nr],
  1113. data->temp_crit[nr]);
  1114. mutex_unlock(&data->update_lock);
  1115. return count;
  1116. }
  1117. #define temp_crit_reg(offset) \
  1118. static SENSOR_DEVICE_ATTR(temp##offset##_crit, S_IRUGO | S_IWUSR, \
  1119. show_temp_crit, set_temp_crit, offset - 1);
  1120. temp_crit_reg(1);
  1121. temp_crit_reg(2);
  1122. temp_crit_reg(3);
  1123. static ssize_t show_analog_out_reg(struct device *dev,
  1124. struct device_attribute *attr, char *buf)
  1125. {
  1126. struct adm1026_data *data = adm1026_update_device(dev);
  1127. return sprintf(buf, "%d\n", DAC_FROM_REG(data->analog_out));
  1128. }
  1129. static ssize_t set_analog_out_reg(struct device *dev,
  1130. struct device_attribute *attr,
  1131. const char *buf, size_t count)
  1132. {
  1133. struct i2c_client *client = to_i2c_client(dev);
  1134. struct adm1026_data *data = i2c_get_clientdata(client);
  1135. long val;
  1136. int err;
  1137. err = kstrtol(buf, 10, &val);
  1138. if (err)
  1139. return err;
  1140. mutex_lock(&data->update_lock);
  1141. data->analog_out = DAC_TO_REG(val);
  1142. adm1026_write_value(client, ADM1026_REG_DAC, data->analog_out);
  1143. mutex_unlock(&data->update_lock);
  1144. return count;
  1145. }
  1146. static DEVICE_ATTR(analog_out, S_IRUGO | S_IWUSR, show_analog_out_reg,
  1147. set_analog_out_reg);
  1148. static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
  1149. char *buf)
  1150. {
  1151. struct adm1026_data *data = adm1026_update_device(dev);
  1152. int vid = (data->gpio >> 11) & 0x1f;
  1153. dev_dbg(dev, "Setting VID from GPIO11-15.\n");
  1154. return sprintf(buf, "%d\n", vid_from_reg(vid, data->vrm));
  1155. }
  1156. static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
  1157. static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
  1158. char *buf)
  1159. {
  1160. struct adm1026_data *data = dev_get_drvdata(dev);
  1161. return sprintf(buf, "%d\n", data->vrm);
  1162. }
  1163. static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
  1164. const char *buf, size_t count)
  1165. {
  1166. struct adm1026_data *data = dev_get_drvdata(dev);
  1167. unsigned long val;
  1168. int err;
  1169. err = kstrtoul(buf, 10, &val);
  1170. if (err)
  1171. return err;
  1172. data->vrm = val;
  1173. return count;
  1174. }
  1175. static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
  1176. static ssize_t show_alarms_reg(struct device *dev,
  1177. struct device_attribute *attr, char *buf)
  1178. {
  1179. struct adm1026_data *data = adm1026_update_device(dev);
  1180. return sprintf(buf, "%ld\n", data->alarms);
  1181. }
  1182. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
  1183. static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
  1184. char *buf)
  1185. {
  1186. struct adm1026_data *data = adm1026_update_device(dev);
  1187. int bitnr = to_sensor_dev_attr(attr)->index;
  1188. return sprintf(buf, "%ld\n", (data->alarms >> bitnr) & 1);
  1189. }
  1190. static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 0);
  1191. static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 1);
  1192. static SENSOR_DEVICE_ATTR(in9_alarm, S_IRUGO, show_alarm, NULL, 1);
  1193. static SENSOR_DEVICE_ATTR(in11_alarm, S_IRUGO, show_alarm, NULL, 2);
  1194. static SENSOR_DEVICE_ATTR(in12_alarm, S_IRUGO, show_alarm, NULL, 3);
  1195. static SENSOR_DEVICE_ATTR(in13_alarm, S_IRUGO, show_alarm, NULL, 4);
  1196. static SENSOR_DEVICE_ATTR(in14_alarm, S_IRUGO, show_alarm, NULL, 5);
  1197. static SENSOR_DEVICE_ATTR(in15_alarm, S_IRUGO, show_alarm, NULL, 6);
  1198. static SENSOR_DEVICE_ATTR(in16_alarm, S_IRUGO, show_alarm, NULL, 7);
  1199. static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
  1200. static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
  1201. static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
  1202. static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
  1203. static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
  1204. static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
  1205. static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
  1206. static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
  1207. static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 16);
  1208. static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 17);
  1209. static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 18);
  1210. static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 19);
  1211. static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 20);
  1212. static SENSOR_DEVICE_ATTR(fan6_alarm, S_IRUGO, show_alarm, NULL, 21);
  1213. static SENSOR_DEVICE_ATTR(fan7_alarm, S_IRUGO, show_alarm, NULL, 22);
  1214. static SENSOR_DEVICE_ATTR(fan8_alarm, S_IRUGO, show_alarm, NULL, 23);
  1215. static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 24);
  1216. static SENSOR_DEVICE_ATTR(in10_alarm, S_IRUGO, show_alarm, NULL, 25);
  1217. static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 26);
  1218. static ssize_t show_alarm_mask(struct device *dev,
  1219. struct device_attribute *attr, char *buf)
  1220. {
  1221. struct adm1026_data *data = adm1026_update_device(dev);
  1222. return sprintf(buf, "%ld\n", data->alarm_mask);
  1223. }
  1224. static ssize_t set_alarm_mask(struct device *dev, struct device_attribute *attr,
  1225. const char *buf, size_t count)
  1226. {
  1227. struct i2c_client *client = to_i2c_client(dev);
  1228. struct adm1026_data *data = i2c_get_clientdata(client);
  1229. unsigned long mask;
  1230. long val;
  1231. int err;
  1232. err = kstrtol(buf, 10, &val);
  1233. if (err)
  1234. return err;
  1235. mutex_lock(&data->update_lock);
  1236. data->alarm_mask = val & 0x7fffffff;
  1237. mask = data->alarm_mask
  1238. | (data->gpio_mask & 0x10000 ? 0x80000000 : 0);
  1239. adm1026_write_value(client, ADM1026_REG_MASK1,
  1240. mask & 0xff);
  1241. mask >>= 8;
  1242. adm1026_write_value(client, ADM1026_REG_MASK2,
  1243. mask & 0xff);
  1244. mask >>= 8;
  1245. adm1026_write_value(client, ADM1026_REG_MASK3,
  1246. mask & 0xff);
  1247. mask >>= 8;
  1248. adm1026_write_value(client, ADM1026_REG_MASK4,
  1249. mask & 0xff);
  1250. mutex_unlock(&data->update_lock);
  1251. return count;
  1252. }
  1253. static DEVICE_ATTR(alarm_mask, S_IRUGO | S_IWUSR, show_alarm_mask,
  1254. set_alarm_mask);
  1255. static ssize_t show_gpio(struct device *dev, struct device_attribute *attr,
  1256. char *buf)
  1257. {
  1258. struct adm1026_data *data = adm1026_update_device(dev);
  1259. return sprintf(buf, "%ld\n", data->gpio);
  1260. }
  1261. static ssize_t set_gpio(struct device *dev, struct device_attribute *attr,
  1262. const char *buf, size_t count)
  1263. {
  1264. struct i2c_client *client = to_i2c_client(dev);
  1265. struct adm1026_data *data = i2c_get_clientdata(client);
  1266. long gpio;
  1267. long val;
  1268. int err;
  1269. err = kstrtol(buf, 10, &val);
  1270. if (err)
  1271. return err;
  1272. mutex_lock(&data->update_lock);
  1273. data->gpio = val & 0x1ffff;
  1274. gpio = data->gpio;
  1275. adm1026_write_value(client, ADM1026_REG_GPIO_STATUS_0_7, gpio & 0xff);
  1276. gpio >>= 8;
  1277. adm1026_write_value(client, ADM1026_REG_GPIO_STATUS_8_15, gpio & 0xff);
  1278. gpio = ((gpio >> 1) & 0x80) | (data->alarms >> 24 & 0x7f);
  1279. adm1026_write_value(client, ADM1026_REG_STATUS4, gpio & 0xff);
  1280. mutex_unlock(&data->update_lock);
  1281. return count;
  1282. }
  1283. static DEVICE_ATTR(gpio, S_IRUGO | S_IWUSR, show_gpio, set_gpio);
  1284. static ssize_t show_gpio_mask(struct device *dev, struct device_attribute *attr,
  1285. char *buf)
  1286. {
  1287. struct adm1026_data *data = adm1026_update_device(dev);
  1288. return sprintf(buf, "%ld\n", data->gpio_mask);
  1289. }
  1290. static ssize_t set_gpio_mask(struct device *dev, struct device_attribute *attr,
  1291. const char *buf, size_t count)
  1292. {
  1293. struct i2c_client *client = to_i2c_client(dev);
  1294. struct adm1026_data *data = i2c_get_clientdata(client);
  1295. long mask;
  1296. long val;
  1297. int err;
  1298. err = kstrtol(buf, 10, &val);
  1299. if (err)
  1300. return err;
  1301. mutex_lock(&data->update_lock);
  1302. data->gpio_mask = val & 0x1ffff;
  1303. mask = data->gpio_mask;
  1304. adm1026_write_value(client, ADM1026_REG_GPIO_MASK_0_7, mask & 0xff);
  1305. mask >>= 8;
  1306. adm1026_write_value(client, ADM1026_REG_GPIO_MASK_8_15, mask & 0xff);
  1307. mask = ((mask >> 1) & 0x80) | (data->alarm_mask >> 24 & 0x7f);
  1308. adm1026_write_value(client, ADM1026_REG_MASK1, mask & 0xff);
  1309. mutex_unlock(&data->update_lock);
  1310. return count;
  1311. }
  1312. static DEVICE_ATTR(gpio_mask, S_IRUGO | S_IWUSR, show_gpio_mask, set_gpio_mask);
  1313. static ssize_t show_pwm_reg(struct device *dev, struct device_attribute *attr,
  1314. char *buf)
  1315. {
  1316. struct adm1026_data *data = adm1026_update_device(dev);
  1317. return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm1.pwm));
  1318. }
  1319. static ssize_t set_pwm_reg(struct device *dev, struct device_attribute *attr,
  1320. const char *buf, size_t count)
  1321. {
  1322. struct i2c_client *client = to_i2c_client(dev);
  1323. struct adm1026_data *data = i2c_get_clientdata(client);
  1324. if (data->pwm1.enable == 1) {
  1325. long val;
  1326. int err;
  1327. err = kstrtol(buf, 10, &val);
  1328. if (err)
  1329. return err;
  1330. mutex_lock(&data->update_lock);
  1331. data->pwm1.pwm = PWM_TO_REG(val);
  1332. adm1026_write_value(client, ADM1026_REG_PWM, data->pwm1.pwm);
  1333. mutex_unlock(&data->update_lock);
  1334. }
  1335. return count;
  1336. }
  1337. static ssize_t show_auto_pwm_min(struct device *dev,
  1338. struct device_attribute *attr, char *buf)
  1339. {
  1340. struct adm1026_data *data = adm1026_update_device(dev);
  1341. return sprintf(buf, "%d\n", data->pwm1.auto_pwm_min);
  1342. }
  1343. static ssize_t set_auto_pwm_min(struct device *dev,
  1344. struct device_attribute *attr, const char *buf,
  1345. size_t count)
  1346. {
  1347. struct i2c_client *client = to_i2c_client(dev);
  1348. struct adm1026_data *data = i2c_get_clientdata(client);
  1349. unsigned long val;
  1350. int err;
  1351. err = kstrtoul(buf, 10, &val);
  1352. if (err)
  1353. return err;
  1354. mutex_lock(&data->update_lock);
  1355. data->pwm1.auto_pwm_min = SENSORS_LIMIT(val, 0, 255);
  1356. if (data->pwm1.enable == 2) { /* apply immediately */
  1357. data->pwm1.pwm = PWM_TO_REG((data->pwm1.pwm & 0x0f) |
  1358. PWM_MIN_TO_REG(data->pwm1.auto_pwm_min));
  1359. adm1026_write_value(client, ADM1026_REG_PWM, data->pwm1.pwm);
  1360. }
  1361. mutex_unlock(&data->update_lock);
  1362. return count;
  1363. }
  1364. static ssize_t show_auto_pwm_max(struct device *dev,
  1365. struct device_attribute *attr, char *buf)
  1366. {
  1367. return sprintf(buf, "%d\n", ADM1026_PWM_MAX);
  1368. }
  1369. static ssize_t show_pwm_enable(struct device *dev,
  1370. struct device_attribute *attr, char *buf)
  1371. {
  1372. struct adm1026_data *data = adm1026_update_device(dev);
  1373. return sprintf(buf, "%d\n", data->pwm1.enable);
  1374. }
  1375. static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *attr,
  1376. const char *buf, size_t count)
  1377. {
  1378. struct i2c_client *client = to_i2c_client(dev);
  1379. struct adm1026_data *data = i2c_get_clientdata(client);
  1380. int old_enable;
  1381. unsigned long val;
  1382. int err;
  1383. err = kstrtoul(buf, 10, &val);
  1384. if (err)
  1385. return err;
  1386. if (val >= 3)
  1387. return -EINVAL;
  1388. mutex_lock(&data->update_lock);
  1389. old_enable = data->pwm1.enable;
  1390. data->pwm1.enable = val;
  1391. data->config1 = (data->config1 & ~CFG1_PWM_AFC)
  1392. | ((val == 2) ? CFG1_PWM_AFC : 0);
  1393. adm1026_write_value(client, ADM1026_REG_CONFIG1, data->config1);
  1394. if (val == 2) { /* apply pwm1_auto_pwm_min to pwm1 */
  1395. data->pwm1.pwm = PWM_TO_REG((data->pwm1.pwm & 0x0f) |
  1396. PWM_MIN_TO_REG(data->pwm1.auto_pwm_min));
  1397. adm1026_write_value(client, ADM1026_REG_PWM, data->pwm1.pwm);
  1398. } else if (!((old_enable == 1) && (val == 1))) {
  1399. /* set pwm to safe value */
  1400. data->pwm1.pwm = 255;
  1401. adm1026_write_value(client, ADM1026_REG_PWM, data->pwm1.pwm);
  1402. }
  1403. mutex_unlock(&data->update_lock);
  1404. return count;
  1405. }
  1406. /* enable PWM fan control */
  1407. static DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm_reg, set_pwm_reg);
  1408. static DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm_reg, set_pwm_reg);
  1409. static DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm_reg, set_pwm_reg);
  1410. static DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, show_pwm_enable,
  1411. set_pwm_enable);
  1412. static DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR, show_pwm_enable,
  1413. set_pwm_enable);
  1414. static DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR, show_pwm_enable,
  1415. set_pwm_enable);
  1416. static DEVICE_ATTR(temp1_auto_point1_pwm, S_IRUGO | S_IWUSR,
  1417. show_auto_pwm_min, set_auto_pwm_min);
  1418. static DEVICE_ATTR(temp2_auto_point1_pwm, S_IRUGO | S_IWUSR,
  1419. show_auto_pwm_min, set_auto_pwm_min);
  1420. static DEVICE_ATTR(temp3_auto_point1_pwm, S_IRUGO | S_IWUSR,
  1421. show_auto_pwm_min, set_auto_pwm_min);
  1422. static DEVICE_ATTR(temp1_auto_point2_pwm, S_IRUGO, show_auto_pwm_max, NULL);
  1423. static DEVICE_ATTR(temp2_auto_point2_pwm, S_IRUGO, show_auto_pwm_max, NULL);
  1424. static DEVICE_ATTR(temp3_auto_point2_pwm, S_IRUGO, show_auto_pwm_max, NULL);
  1425. static struct attribute *adm1026_attributes[] = {
  1426. &sensor_dev_attr_in0_input.dev_attr.attr,
  1427. &sensor_dev_attr_in0_max.dev_attr.attr,
  1428. &sensor_dev_attr_in0_min.dev_attr.attr,
  1429. &sensor_dev_attr_in0_alarm.dev_attr.attr,
  1430. &sensor_dev_attr_in1_input.dev_attr.attr,
  1431. &sensor_dev_attr_in1_max.dev_attr.attr,
  1432. &sensor_dev_attr_in1_min.dev_attr.attr,
  1433. &sensor_dev_attr_in1_alarm.dev_attr.attr,
  1434. &sensor_dev_attr_in2_input.dev_attr.attr,
  1435. &sensor_dev_attr_in2_max.dev_attr.attr,
  1436. &sensor_dev_attr_in2_min.dev_attr.attr,
  1437. &sensor_dev_attr_in2_alarm.dev_attr.attr,
  1438. &sensor_dev_attr_in3_input.dev_attr.attr,
  1439. &sensor_dev_attr_in3_max.dev_attr.attr,
  1440. &sensor_dev_attr_in3_min.dev_attr.attr,
  1441. &sensor_dev_attr_in3_alarm.dev_attr.attr,
  1442. &sensor_dev_attr_in4_input.dev_attr.attr,
  1443. &sensor_dev_attr_in4_max.dev_attr.attr,
  1444. &sensor_dev_attr_in4_min.dev_attr.attr,
  1445. &sensor_dev_attr_in4_alarm.dev_attr.attr,
  1446. &sensor_dev_attr_in5_input.dev_attr.attr,
  1447. &sensor_dev_attr_in5_max.dev_attr.attr,
  1448. &sensor_dev_attr_in5_min.dev_attr.attr,
  1449. &sensor_dev_attr_in5_alarm.dev_attr.attr,
  1450. &sensor_dev_attr_in6_input.dev_attr.attr,
  1451. &sensor_dev_attr_in6_max.dev_attr.attr,
  1452. &sensor_dev_attr_in6_min.dev_attr.attr,
  1453. &sensor_dev_attr_in6_alarm.dev_attr.attr,
  1454. &sensor_dev_attr_in7_input.dev_attr.attr,
  1455. &sensor_dev_attr_in7_max.dev_attr.attr,
  1456. &sensor_dev_attr_in7_min.dev_attr.attr,
  1457. &sensor_dev_attr_in7_alarm.dev_attr.attr,
  1458. &sensor_dev_attr_in10_input.dev_attr.attr,
  1459. &sensor_dev_attr_in10_max.dev_attr.attr,
  1460. &sensor_dev_attr_in10_min.dev_attr.attr,
  1461. &sensor_dev_attr_in10_alarm.dev_attr.attr,
  1462. &sensor_dev_attr_in11_input.dev_attr.attr,
  1463. &sensor_dev_attr_in11_max.dev_attr.attr,
  1464. &sensor_dev_attr_in11_min.dev_attr.attr,
  1465. &sensor_dev_attr_in11_alarm.dev_attr.attr,
  1466. &sensor_dev_attr_in12_input.dev_attr.attr,
  1467. &sensor_dev_attr_in12_max.dev_attr.attr,
  1468. &sensor_dev_attr_in12_min.dev_attr.attr,
  1469. &sensor_dev_attr_in12_alarm.dev_attr.attr,
  1470. &sensor_dev_attr_in13_input.dev_attr.attr,
  1471. &sensor_dev_attr_in13_max.dev_attr.attr,
  1472. &sensor_dev_attr_in13_min.dev_attr.attr,
  1473. &sensor_dev_attr_in13_alarm.dev_attr.attr,
  1474. &sensor_dev_attr_in14_input.dev_attr.attr,
  1475. &sensor_dev_attr_in14_max.dev_attr.attr,
  1476. &sensor_dev_attr_in14_min.dev_attr.attr,
  1477. &sensor_dev_attr_in14_alarm.dev_attr.attr,
  1478. &sensor_dev_attr_in15_input.dev_attr.attr,
  1479. &sensor_dev_attr_in15_max.dev_attr.attr,
  1480. &sensor_dev_attr_in15_min.dev_attr.attr,
  1481. &sensor_dev_attr_in15_alarm.dev_attr.attr,
  1482. &sensor_dev_attr_in16_input.dev_attr.attr,
  1483. &sensor_dev_attr_in16_max.dev_attr.attr,
  1484. &sensor_dev_attr_in16_min.dev_attr.attr,
  1485. &sensor_dev_attr_in16_alarm.dev_attr.attr,
  1486. &sensor_dev_attr_fan1_input.dev_attr.attr,
  1487. &sensor_dev_attr_fan1_div.dev_attr.attr,
  1488. &sensor_dev_attr_fan1_min.dev_attr.attr,
  1489. &sensor_dev_attr_fan1_alarm.dev_attr.attr,
  1490. &sensor_dev_attr_fan2_input.dev_attr.attr,
  1491. &sensor_dev_attr_fan2_div.dev_attr.attr,
  1492. &sensor_dev_attr_fan2_min.dev_attr.attr,
  1493. &sensor_dev_attr_fan2_alarm.dev_attr.attr,
  1494. &sensor_dev_attr_fan3_input.dev_attr.attr,
  1495. &sensor_dev_attr_fan3_div.dev_attr.attr,
  1496. &sensor_dev_attr_fan3_min.dev_attr.attr,
  1497. &sensor_dev_attr_fan3_alarm.dev_attr.attr,
  1498. &sensor_dev_attr_fan4_input.dev_attr.attr,
  1499. &sensor_dev_attr_fan4_div.dev_attr.attr,
  1500. &sensor_dev_attr_fan4_min.dev_attr.attr,
  1501. &sensor_dev_attr_fan4_alarm.dev_attr.attr,
  1502. &sensor_dev_attr_fan5_input.dev_attr.attr,
  1503. &sensor_dev_attr_fan5_div.dev_attr.attr,
  1504. &sensor_dev_attr_fan5_min.dev_attr.attr,
  1505. &sensor_dev_attr_fan5_alarm.dev_attr.attr,
  1506. &sensor_dev_attr_fan6_input.dev_attr.attr,
  1507. &sensor_dev_attr_fan6_div.dev_attr.attr,
  1508. &sensor_dev_attr_fan6_min.dev_attr.attr,
  1509. &sensor_dev_attr_fan6_alarm.dev_attr.attr,
  1510. &sensor_dev_attr_fan7_input.dev_attr.attr,
  1511. &sensor_dev_attr_fan7_div.dev_attr.attr,
  1512. &sensor_dev_attr_fan7_min.dev_attr.attr,
  1513. &sensor_dev_attr_fan7_alarm.dev_attr.attr,
  1514. &sensor_dev_attr_fan8_input.dev_attr.attr,
  1515. &sensor_dev_attr_fan8_div.dev_attr.attr,
  1516. &sensor_dev_attr_fan8_min.dev_attr.attr,
  1517. &sensor_dev_attr_fan8_alarm.dev_attr.attr,
  1518. &sensor_dev_attr_temp1_input.dev_attr.attr,
  1519. &sensor_dev_attr_temp1_max.dev_attr.attr,
  1520. &sensor_dev_attr_temp1_min.dev_attr.attr,
  1521. &sensor_dev_attr_temp1_alarm.dev_attr.attr,
  1522. &sensor_dev_attr_temp2_input.dev_attr.attr,
  1523. &sensor_dev_attr_temp2_max.dev_attr.attr,
  1524. &sensor_dev_attr_temp2_min.dev_attr.attr,
  1525. &sensor_dev_attr_temp2_alarm.dev_attr.attr,
  1526. &sensor_dev_attr_temp1_offset.dev_attr.attr,
  1527. &sensor_dev_attr_temp2_offset.dev_attr.attr,
  1528. &sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
  1529. &sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr,
  1530. &sensor_dev_attr_temp1_auto_point1_temp_hyst.dev_attr.attr,
  1531. &sensor_dev_attr_temp2_auto_point1_temp_hyst.dev_attr.attr,
  1532. &sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr,
  1533. &sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr,
  1534. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  1535. &sensor_dev_attr_temp2_crit.dev_attr.attr,
  1536. &dev_attr_temp1_crit_enable.attr,
  1537. &dev_attr_temp2_crit_enable.attr,
  1538. &dev_attr_cpu0_vid.attr,
  1539. &dev_attr_vrm.attr,
  1540. &dev_attr_alarms.attr,
  1541. &dev_attr_alarm_mask.attr,
  1542. &dev_attr_gpio.attr,
  1543. &dev_attr_gpio_mask.attr,
  1544. &dev_attr_pwm1.attr,
  1545. &dev_attr_pwm2.attr,
  1546. &dev_attr_pwm3.attr,
  1547. &dev_attr_pwm1_enable.attr,
  1548. &dev_attr_pwm2_enable.attr,
  1549. &dev_attr_pwm3_enable.attr,
  1550. &dev_attr_temp1_auto_point1_pwm.attr,
  1551. &dev_attr_temp2_auto_point1_pwm.attr,
  1552. &dev_attr_temp1_auto_point2_pwm.attr,
  1553. &dev_attr_temp2_auto_point2_pwm.attr,
  1554. &dev_attr_analog_out.attr,
  1555. NULL
  1556. };
  1557. static const struct attribute_group adm1026_group = {
  1558. .attrs = adm1026_attributes,
  1559. };
  1560. static struct attribute *adm1026_attributes_temp3[] = {
  1561. &sensor_dev_attr_temp3_input.dev_attr.attr,
  1562. &sensor_dev_attr_temp3_max.dev_attr.attr,
  1563. &sensor_dev_attr_temp3_min.dev_attr.attr,
  1564. &sensor_dev_attr_temp3_alarm.dev_attr.attr,
  1565. &sensor_dev_attr_temp3_offset.dev_attr.attr,
  1566. &sensor_dev_attr_temp3_auto_point1_temp.dev_attr.attr,
  1567. &sensor_dev_attr_temp3_auto_point1_temp_hyst.dev_attr.attr,
  1568. &sensor_dev_attr_temp3_auto_point2_temp.dev_attr.attr,
  1569. &sensor_dev_attr_temp3_crit.dev_attr.attr,
  1570. &dev_attr_temp3_crit_enable.attr,
  1571. &dev_attr_temp3_auto_point1_pwm.attr,
  1572. &dev_attr_temp3_auto_point2_pwm.attr,
  1573. NULL
  1574. };
  1575. static const struct attribute_group adm1026_group_temp3 = {
  1576. .attrs = adm1026_attributes_temp3,
  1577. };
  1578. static struct attribute *adm1026_attributes_in8_9[] = {
  1579. &sensor_dev_attr_in8_input.dev_attr.attr,
  1580. &sensor_dev_attr_in8_max.dev_attr.attr,
  1581. &sensor_dev_attr_in8_min.dev_attr.attr,
  1582. &sensor_dev_attr_in8_alarm.dev_attr.attr,
  1583. &sensor_dev_attr_in9_input.dev_attr.attr,
  1584. &sensor_dev_attr_in9_max.dev_attr.attr,
  1585. &sensor_dev_attr_in9_min.dev_attr.attr,
  1586. &sensor_dev_attr_in9_alarm.dev_attr.attr,
  1587. NULL
  1588. };
  1589. static const struct attribute_group adm1026_group_in8_9 = {
  1590. .attrs = adm1026_attributes_in8_9,
  1591. };
  1592. /* Return 0 if detection is successful, -ENODEV otherwise */
  1593. static int adm1026_detect(struct i2c_client *client,
  1594. struct i2c_board_info *info)
  1595. {
  1596. struct i2c_adapter *adapter = client->adapter;
  1597. int address = client->addr;
  1598. int company, verstep;
  1599. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
  1600. /* We need to be able to do byte I/O */
  1601. return -ENODEV;
  1602. };
  1603. /* Now, we do the remaining detection. */
  1604. company = adm1026_read_value(client, ADM1026_REG_COMPANY);
  1605. verstep = adm1026_read_value(client, ADM1026_REG_VERSTEP);
  1606. dev_dbg(&adapter->dev, "Detecting device at %d,0x%02x with"
  1607. " COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
  1608. i2c_adapter_id(client->adapter), client->addr,
  1609. company, verstep);
  1610. /* Determine the chip type. */
  1611. dev_dbg(&adapter->dev, "Autodetecting device at %d,0x%02x...\n",
  1612. i2c_adapter_id(adapter), address);
  1613. if (company == ADM1026_COMPANY_ANALOG_DEV
  1614. && verstep == ADM1026_VERSTEP_ADM1026) {
  1615. /* Analog Devices ADM1026 */
  1616. } else if (company == ADM1026_COMPANY_ANALOG_DEV
  1617. && (verstep & 0xf0) == ADM1026_VERSTEP_GENERIC) {
  1618. dev_err(&adapter->dev, "Unrecognized stepping "
  1619. "0x%02x. Defaulting to ADM1026.\n", verstep);
  1620. } else if ((verstep & 0xf0) == ADM1026_VERSTEP_GENERIC) {
  1621. dev_err(&adapter->dev, "Found version/stepping "
  1622. "0x%02x. Assuming generic ADM1026.\n",
  1623. verstep);
  1624. } else {
  1625. dev_dbg(&adapter->dev, "Autodetection failed\n");
  1626. /* Not an ADM1026... */
  1627. return -ENODEV;
  1628. }
  1629. strlcpy(info->type, "adm1026", I2C_NAME_SIZE);
  1630. return 0;
  1631. }
  1632. static int adm1026_probe(struct i2c_client *client,
  1633. const struct i2c_device_id *id)
  1634. {
  1635. struct adm1026_data *data;
  1636. int err;
  1637. data = kzalloc(sizeof(struct adm1026_data), GFP_KERNEL);
  1638. if (!data) {
  1639. err = -ENOMEM;
  1640. goto exit;
  1641. }
  1642. i2c_set_clientdata(client, data);
  1643. mutex_init(&data->update_lock);
  1644. /* Set the VRM version */
  1645. data->vrm = vid_which_vrm();
  1646. /* Initialize the ADM1026 chip */
  1647. adm1026_init_client(client);
  1648. /* Register sysfs hooks */
  1649. err = sysfs_create_group(&client->dev.kobj, &adm1026_group);
  1650. if (err)
  1651. goto exitfree;
  1652. if (data->config1 & CFG1_AIN8_9)
  1653. err = sysfs_create_group(&client->dev.kobj,
  1654. &adm1026_group_in8_9);
  1655. else
  1656. err = sysfs_create_group(&client->dev.kobj,
  1657. &adm1026_group_temp3);
  1658. if (err)
  1659. goto exitremove;
  1660. data->hwmon_dev = hwmon_device_register(&client->dev);
  1661. if (IS_ERR(data->hwmon_dev)) {
  1662. err = PTR_ERR(data->hwmon_dev);
  1663. goto exitremove;
  1664. }
  1665. return 0;
  1666. /* Error out and cleanup code */
  1667. exitremove:
  1668. sysfs_remove_group(&client->dev.kobj, &adm1026_group);
  1669. if (data->config1 & CFG1_AIN8_9)
  1670. sysfs_remove_group(&client->dev.kobj, &adm1026_group_in8_9);
  1671. else
  1672. sysfs_remove_group(&client->dev.kobj, &adm1026_group_temp3);
  1673. exitfree:
  1674. kfree(data);
  1675. exit:
  1676. return err;
  1677. }
  1678. static int adm1026_remove(struct i2c_client *client)
  1679. {
  1680. struct adm1026_data *data = i2c_get_clientdata(client);
  1681. hwmon_device_unregister(data->hwmon_dev);
  1682. sysfs_remove_group(&client->dev.kobj, &adm1026_group);
  1683. if (data->config1 & CFG1_AIN8_9)
  1684. sysfs_remove_group(&client->dev.kobj, &adm1026_group_in8_9);
  1685. else
  1686. sysfs_remove_group(&client->dev.kobj, &adm1026_group_temp3);
  1687. kfree(data);
  1688. return 0;
  1689. }
  1690. module_i2c_driver(adm1026_driver);
  1691. MODULE_LICENSE("GPL");
  1692. MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, "
  1693. "Justin Thiessen <jthiessen@penguincomputing.com>");
  1694. MODULE_DESCRIPTION("ADM1026 driver");