adm1026.c 59 KB

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