adm1026.c 63 KB

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