adm1026.c 58 KB

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