adm1026.c 59 KB

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