adm1026.c 59 KB

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