adm1026.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720
  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 register);
  268. static int adm1026_write_value(struct i2c_client *client, u8 register,
  269. int value);
  270. static void adm1026_print_gpio(struct i2c_client *client);
  271. static void adm1026_fixup_gpio(struct i2c_client *client);
  272. static struct adm1026_data *adm1026_update_device(struct device *dev);
  273. static void adm1026_init_client(struct i2c_client *client);
  274. static struct i2c_driver adm1026_driver = {
  275. .driver = {
  276. .name = "adm1026",
  277. },
  278. .attach_adapter = adm1026_attach_adapter,
  279. .detach_client = adm1026_detach_client,
  280. };
  281. static int adm1026_attach_adapter(struct i2c_adapter *adapter)
  282. {
  283. if (!(adapter->class & I2C_CLASS_HWMON)) {
  284. return 0;
  285. }
  286. return i2c_probe(adapter, &addr_data, adm1026_detect);
  287. }
  288. static int adm1026_detach_client(struct i2c_client *client)
  289. {
  290. struct adm1026_data *data = i2c_get_clientdata(client);
  291. hwmon_device_unregister(data->class_dev);
  292. i2c_detach_client(client);
  293. kfree(data);
  294. return 0;
  295. }
  296. static int adm1026_read_value(struct i2c_client *client, u8 reg)
  297. {
  298. int res;
  299. if (reg < 0x80) {
  300. /* "RAM" locations */
  301. res = i2c_smbus_read_byte_data(client, reg) & 0xff;
  302. } else {
  303. /* EEPROM, do nothing */
  304. res = 0;
  305. }
  306. return res;
  307. }
  308. static int adm1026_write_value(struct i2c_client *client, u8 reg, int value)
  309. {
  310. int res;
  311. if (reg < 0x80) {
  312. /* "RAM" locations */
  313. res = i2c_smbus_write_byte_data(client, reg, value);
  314. } else {
  315. /* EEPROM, do nothing */
  316. res = 0;
  317. }
  318. return res;
  319. }
  320. static void adm1026_init_client(struct i2c_client *client)
  321. {
  322. int value, i;
  323. struct adm1026_data *data = i2c_get_clientdata(client);
  324. dev_dbg(&client->dev, "Initializing device\n");
  325. /* Read chip config */
  326. data->config1 = adm1026_read_value(client, ADM1026_REG_CONFIG1);
  327. data->config2 = adm1026_read_value(client, ADM1026_REG_CONFIG2);
  328. data->config3 = adm1026_read_value(client, ADM1026_REG_CONFIG3);
  329. /* Inform user of chip config */
  330. dev_dbg(&client->dev, "ADM1026_REG_CONFIG1 is: 0x%02x\n",
  331. data->config1);
  332. if ((data->config1 & CFG1_MONITOR) == 0) {
  333. dev_dbg(&client->dev, "Monitoring not currently "
  334. "enabled.\n");
  335. }
  336. if (data->config1 & CFG1_INT_ENABLE) {
  337. dev_dbg(&client->dev, "SMBALERT interrupts are "
  338. "enabled.\n");
  339. }
  340. if (data->config1 & CFG1_AIN8_9) {
  341. dev_dbg(&client->dev, "in8 and in9 enabled. "
  342. "temp3 disabled.\n");
  343. } else {
  344. dev_dbg(&client->dev, "temp3 enabled. in8 and "
  345. "in9 disabled.\n");
  346. }
  347. if (data->config1 & CFG1_THERM_HOT) {
  348. dev_dbg(&client->dev, "Automatic THERM, PWM, "
  349. "and temp limits enabled.\n");
  350. }
  351. value = data->config3;
  352. if (data->config3 & CFG3_GPIO16_ENABLE) {
  353. dev_dbg(&client->dev, "GPIO16 enabled. THERM "
  354. "pin disabled.\n");
  355. } else {
  356. dev_dbg(&client->dev, "THERM pin enabled. "
  357. "GPIO16 disabled.\n");
  358. }
  359. if (data->config3 & CFG3_VREF_250) {
  360. dev_dbg(&client->dev, "Vref is 2.50 Volts.\n");
  361. } else {
  362. dev_dbg(&client->dev, "Vref is 1.82 Volts.\n");
  363. }
  364. /* Read and pick apart the existing GPIO configuration */
  365. value = 0;
  366. for (i = 0;i <= 15;++i) {
  367. if ((i & 0x03) == 0) {
  368. value = adm1026_read_value(client,
  369. ADM1026_REG_GPIO_CFG_0_3 + i/4);
  370. }
  371. data->gpio_config[i] = value & 0x03;
  372. value >>= 2;
  373. }
  374. data->gpio_config[16] = (data->config3 >> 6) & 0x03;
  375. /* ... and then print it */
  376. adm1026_print_gpio(client);
  377. /* If the user asks us to reprogram the GPIO config, then
  378. * do it now.
  379. */
  380. if (gpio_input[0] != -1 || gpio_output[0] != -1
  381. || gpio_inverted[0] != -1 || gpio_normal[0] != -1
  382. || gpio_fan[0] != -1) {
  383. adm1026_fixup_gpio(client);
  384. }
  385. /* WE INTENTIONALLY make no changes to the limits,
  386. * offsets, pwms, fans and zones. If they were
  387. * configured, we don't want to mess with them.
  388. * If they weren't, the default is 100% PWM, no
  389. * control and will suffice until 'sensors -s'
  390. * can be run by the user. We DO set the default
  391. * value for pwm1.auto_pwm_min to its maximum
  392. * so that enabling automatic pwm fan control
  393. * without first setting a value for pwm1.auto_pwm_min
  394. * will not result in potentially dangerous fan speed decrease.
  395. */
  396. data->pwm1.auto_pwm_min=255;
  397. /* Start monitoring */
  398. value = adm1026_read_value(client, ADM1026_REG_CONFIG1);
  399. /* Set MONITOR, clear interrupt acknowledge and s/w reset */
  400. value = (value | CFG1_MONITOR) & (~CFG1_INT_CLEAR & ~CFG1_RESET);
  401. dev_dbg(&client->dev, "Setting CONFIG to: 0x%02x\n", value);
  402. data->config1 = value;
  403. adm1026_write_value(client, ADM1026_REG_CONFIG1, value);
  404. /* initialize fan_div[] to hardware defaults */
  405. value = adm1026_read_value(client, ADM1026_REG_FAN_DIV_0_3) |
  406. (adm1026_read_value(client, ADM1026_REG_FAN_DIV_4_7) << 8);
  407. for (i = 0;i <= 7;++i) {
  408. data->fan_div[i] = DIV_FROM_REG(value & 0x03);
  409. value >>= 2;
  410. }
  411. }
  412. static void adm1026_print_gpio(struct i2c_client *client)
  413. {
  414. struct adm1026_data *data = i2c_get_clientdata(client);
  415. int i;
  416. dev_dbg(&client->dev, "GPIO config is:");
  417. for (i = 0;i <= 7;++i) {
  418. if (data->config2 & (1 << i)) {
  419. dev_dbg(&client->dev, "\t%sGP%s%d\n",
  420. data->gpio_config[i] & 0x02 ? "" : "!",
  421. data->gpio_config[i] & 0x01 ? "OUT" : "IN",
  422. i);
  423. } else {
  424. dev_dbg(&client->dev, "\tFAN%d\n", i);
  425. }
  426. }
  427. for (i = 8;i <= 15;++i) {
  428. dev_dbg(&client->dev, "\t%sGP%s%d\n",
  429. data->gpio_config[i] & 0x02 ? "" : "!",
  430. data->gpio_config[i] & 0x01 ? "OUT" : "IN",
  431. i);
  432. }
  433. if (data->config3 & CFG3_GPIO16_ENABLE) {
  434. dev_dbg(&client->dev, "\t%sGP%s16\n",
  435. data->gpio_config[16] & 0x02 ? "" : "!",
  436. data->gpio_config[16] & 0x01 ? "OUT" : "IN");
  437. } else {
  438. /* GPIO16 is THERM */
  439. dev_dbg(&client->dev, "\tTHERM\n");
  440. }
  441. }
  442. static void adm1026_fixup_gpio(struct i2c_client *client)
  443. {
  444. struct adm1026_data *data = i2c_get_clientdata(client);
  445. int i;
  446. int value;
  447. /* Make the changes requested. */
  448. /* We may need to unlock/stop monitoring or soft-reset the
  449. * chip before we can make changes. This hasn't been
  450. * tested much. FIXME
  451. */
  452. /* Make outputs */
  453. for (i = 0;i <= 16;++i) {
  454. if (gpio_output[i] >= 0 && gpio_output[i] <= 16) {
  455. data->gpio_config[gpio_output[i]] |= 0x01;
  456. }
  457. /* if GPIO0-7 is output, it isn't a FAN tach */
  458. if (gpio_output[i] >= 0 && gpio_output[i] <= 7) {
  459. data->config2 |= 1 << gpio_output[i];
  460. }
  461. }
  462. /* Input overrides output */
  463. for (i = 0;i <= 16;++i) {
  464. if (gpio_input[i] >= 0 && gpio_input[i] <= 16) {
  465. data->gpio_config[gpio_input[i]] &= ~ 0x01;
  466. }
  467. /* if GPIO0-7 is input, it isn't a FAN tach */
  468. if (gpio_input[i] >= 0 && gpio_input[i] <= 7) {
  469. data->config2 |= 1 << gpio_input[i];
  470. }
  471. }
  472. /* Inverted */
  473. for (i = 0;i <= 16;++i) {
  474. if (gpio_inverted[i] >= 0 && gpio_inverted[i] <= 16) {
  475. data->gpio_config[gpio_inverted[i]] &= ~ 0x02;
  476. }
  477. }
  478. /* Normal overrides inverted */
  479. for (i = 0;i <= 16;++i) {
  480. if (gpio_normal[i] >= 0 && gpio_normal[i] <= 16) {
  481. data->gpio_config[gpio_normal[i]] |= 0x02;
  482. }
  483. }
  484. /* Fan overrides input and output */
  485. for (i = 0;i <= 7;++i) {
  486. if (gpio_fan[i] >= 0 && gpio_fan[i] <= 7) {
  487. data->config2 &= ~(1 << gpio_fan[i]);
  488. }
  489. }
  490. /* Write new configs to registers */
  491. adm1026_write_value(client, ADM1026_REG_CONFIG2, data->config2);
  492. data->config3 = (data->config3 & 0x3f)
  493. | ((data->gpio_config[16] & 0x03) << 6);
  494. adm1026_write_value(client, ADM1026_REG_CONFIG3, data->config3);
  495. for (i = 15, value = 0;i >= 0;--i) {
  496. value <<= 2;
  497. value |= data->gpio_config[i] & 0x03;
  498. if ((i & 0x03) == 0) {
  499. adm1026_write_value(client,
  500. ADM1026_REG_GPIO_CFG_0_3 + i/4,
  501. value);
  502. value = 0;
  503. }
  504. }
  505. /* Print the new config */
  506. adm1026_print_gpio(client);
  507. }
  508. static struct adm1026_data *adm1026_update_device(struct device *dev)
  509. {
  510. struct i2c_client *client = to_i2c_client(dev);
  511. struct adm1026_data *data = i2c_get_clientdata(client);
  512. int i;
  513. long value, alarms, gpio;
  514. mutex_lock(&data->update_lock);
  515. if (!data->valid
  516. || time_after(jiffies, data->last_reading + ADM1026_DATA_INTERVAL)) {
  517. /* Things that change quickly */
  518. dev_dbg(&client->dev,"Reading sensor values\n");
  519. for (i = 0;i <= 16;++i) {
  520. data->in[i] =
  521. adm1026_read_value(client, ADM1026_REG_IN[i]);
  522. }
  523. for (i = 0;i <= 7;++i) {
  524. data->fan[i] =
  525. adm1026_read_value(client, ADM1026_REG_FAN(i));
  526. }
  527. for (i = 0;i <= 2;++i) {
  528. /* NOTE: temp[] is s8 and we assume 2's complement
  529. * "conversion" in the assignment */
  530. data->temp[i] =
  531. adm1026_read_value(client, ADM1026_REG_TEMP[i]);
  532. }
  533. data->pwm1.pwm = adm1026_read_value(client,
  534. ADM1026_REG_PWM);
  535. data->analog_out = adm1026_read_value(client,
  536. ADM1026_REG_DAC);
  537. /* GPIO16 is MSbit of alarms, move it to gpio */
  538. alarms = adm1026_read_value(client, ADM1026_REG_STATUS4);
  539. gpio = alarms & 0x80 ? 0x0100 : 0; /* GPIO16 */
  540. alarms &= 0x7f;
  541. alarms <<= 8;
  542. alarms |= adm1026_read_value(client, ADM1026_REG_STATUS3);
  543. alarms <<= 8;
  544. alarms |= adm1026_read_value(client, ADM1026_REG_STATUS2);
  545. alarms <<= 8;
  546. alarms |= adm1026_read_value(client, ADM1026_REG_STATUS1);
  547. data->alarms = alarms;
  548. /* Read the GPIO values */
  549. gpio |= adm1026_read_value(client,
  550. ADM1026_REG_GPIO_STATUS_8_15);
  551. gpio <<= 8;
  552. gpio |= adm1026_read_value(client,
  553. ADM1026_REG_GPIO_STATUS_0_7);
  554. data->gpio = gpio;
  555. data->last_reading = jiffies;
  556. }; /* last_reading */
  557. if (!data->valid ||
  558. time_after(jiffies, data->last_config + ADM1026_CONFIG_INTERVAL)) {
  559. /* Things that don't change often */
  560. dev_dbg(&client->dev, "Reading config values\n");
  561. for (i = 0;i <= 16;++i) {
  562. data->in_min[i] = adm1026_read_value(client,
  563. ADM1026_REG_IN_MIN[i]);
  564. data->in_max[i] = adm1026_read_value(client,
  565. ADM1026_REG_IN_MAX[i]);
  566. }
  567. value = adm1026_read_value(client, ADM1026_REG_FAN_DIV_0_3)
  568. | (adm1026_read_value(client, ADM1026_REG_FAN_DIV_4_7)
  569. << 8);
  570. for (i = 0;i <= 7;++i) {
  571. data->fan_min[i] = adm1026_read_value(client,
  572. ADM1026_REG_FAN_MIN(i));
  573. data->fan_div[i] = DIV_FROM_REG(value & 0x03);
  574. value >>= 2;
  575. }
  576. for (i = 0; i <= 2; ++i) {
  577. /* NOTE: temp_xxx[] are s8 and we assume 2's
  578. * complement "conversion" in the assignment
  579. */
  580. data->temp_min[i] = adm1026_read_value(client,
  581. ADM1026_REG_TEMP_MIN[i]);
  582. data->temp_max[i] = adm1026_read_value(client,
  583. ADM1026_REG_TEMP_MAX[i]);
  584. data->temp_tmin[i] = adm1026_read_value(client,
  585. ADM1026_REG_TEMP_TMIN[i]);
  586. data->temp_crit[i] = adm1026_read_value(client,
  587. ADM1026_REG_TEMP_THERM[i]);
  588. data->temp_offset[i] = adm1026_read_value(client,
  589. ADM1026_REG_TEMP_OFFSET[i]);
  590. }
  591. /* Read the STATUS/alarm masks */
  592. alarms = adm1026_read_value(client, ADM1026_REG_MASK4);
  593. gpio = alarms & 0x80 ? 0x0100 : 0; /* GPIO16 */
  594. alarms = (alarms & 0x7f) << 8;
  595. alarms |= adm1026_read_value(client, ADM1026_REG_MASK3);
  596. alarms <<= 8;
  597. alarms |= adm1026_read_value(client, ADM1026_REG_MASK2);
  598. alarms <<= 8;
  599. alarms |= adm1026_read_value(client, ADM1026_REG_MASK1);
  600. data->alarm_mask = alarms;
  601. /* Read the GPIO values */
  602. gpio |= adm1026_read_value(client,
  603. ADM1026_REG_GPIO_MASK_8_15);
  604. gpio <<= 8;
  605. gpio |= adm1026_read_value(client, ADM1026_REG_GPIO_MASK_0_7);
  606. data->gpio_mask = gpio;
  607. /* Read various values from CONFIG1 */
  608. data->config1 = adm1026_read_value(client,
  609. ADM1026_REG_CONFIG1);
  610. if (data->config1 & CFG1_PWM_AFC) {
  611. data->pwm1.enable = 2;
  612. data->pwm1.auto_pwm_min =
  613. PWM_MIN_FROM_REG(data->pwm1.pwm);
  614. }
  615. /* Read the GPIO config */
  616. data->config2 = adm1026_read_value(client,
  617. ADM1026_REG_CONFIG2);
  618. data->config3 = adm1026_read_value(client,
  619. ADM1026_REG_CONFIG3);
  620. data->gpio_config[16] = (data->config3 >> 6) & 0x03;
  621. value = 0;
  622. for (i = 0;i <= 15;++i) {
  623. if ((i & 0x03) == 0) {
  624. value = adm1026_read_value(client,
  625. ADM1026_REG_GPIO_CFG_0_3 + i/4);
  626. }
  627. data->gpio_config[i] = value & 0x03;
  628. value >>= 2;
  629. }
  630. data->last_config = jiffies;
  631. }; /* last_config */
  632. dev_dbg(&client->dev, "Setting VID from GPIO11-15.\n");
  633. data->vid = (data->gpio >> 11) & 0x1f;
  634. data->valid = 1;
  635. mutex_unlock(&data->update_lock);
  636. return data;
  637. }
  638. static ssize_t show_in(struct device *dev, struct device_attribute *attr,
  639. char *buf)
  640. {
  641. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  642. int nr = sensor_attr->index;
  643. struct adm1026_data *data = adm1026_update_device(dev);
  644. return sprintf(buf,"%d\n", INS_FROM_REG(nr, data->in[nr]));
  645. }
  646. static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
  647. char *buf)
  648. {
  649. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  650. int nr = sensor_attr->index;
  651. struct adm1026_data *data = adm1026_update_device(dev);
  652. return sprintf(buf,"%d\n", INS_FROM_REG(nr, data->in_min[nr]));
  653. }
  654. static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
  655. const char *buf, size_t count)
  656. {
  657. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  658. int nr = sensor_attr->index;
  659. struct i2c_client *client = to_i2c_client(dev);
  660. struct adm1026_data *data = i2c_get_clientdata(client);
  661. int val = simple_strtol(buf, NULL, 10);
  662. mutex_lock(&data->update_lock);
  663. data->in_min[nr] = INS_TO_REG(nr, val);
  664. adm1026_write_value(client, ADM1026_REG_IN_MIN[nr], data->in_min[nr]);
  665. mutex_unlock(&data->update_lock);
  666. return count;
  667. }
  668. static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
  669. char *buf)
  670. {
  671. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  672. int nr = sensor_attr->index;
  673. struct adm1026_data *data = adm1026_update_device(dev);
  674. return sprintf(buf,"%d\n", INS_FROM_REG(nr, data->in_max[nr]));
  675. }
  676. static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
  677. const char *buf, size_t count)
  678. {
  679. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  680. int nr = sensor_attr->index;
  681. struct i2c_client *client = to_i2c_client(dev);
  682. struct adm1026_data *data = i2c_get_clientdata(client);
  683. int val = simple_strtol(buf, NULL, 10);
  684. mutex_lock(&data->update_lock);
  685. data->in_max[nr] = INS_TO_REG(nr, val);
  686. adm1026_write_value(client, ADM1026_REG_IN_MAX[nr], data->in_max[nr]);
  687. mutex_unlock(&data->update_lock);
  688. return count;
  689. }
  690. #define in_reg(offset) \
  691. static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, show_in, \
  692. NULL, offset); \
  693. static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
  694. show_in_min, set_in_min, offset); \
  695. static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
  696. show_in_max, set_in_max, offset);
  697. in_reg(0);
  698. in_reg(1);
  699. in_reg(2);
  700. in_reg(3);
  701. in_reg(4);
  702. in_reg(5);
  703. in_reg(6);
  704. in_reg(7);
  705. in_reg(8);
  706. in_reg(9);
  707. in_reg(10);
  708. in_reg(11);
  709. in_reg(12);
  710. in_reg(13);
  711. in_reg(14);
  712. in_reg(15);
  713. static ssize_t show_in16(struct device *dev, struct device_attribute *attr, char *buf)
  714. {
  715. struct adm1026_data *data = adm1026_update_device(dev);
  716. return sprintf(buf,"%d\n", INS_FROM_REG(16, data->in[16]) -
  717. NEG12_OFFSET);
  718. }
  719. static ssize_t show_in16_min(struct device *dev, struct device_attribute *attr, char *buf)
  720. {
  721. struct adm1026_data *data = adm1026_update_device(dev);
  722. return sprintf(buf,"%d\n", INS_FROM_REG(16, data->in_min[16])
  723. - NEG12_OFFSET);
  724. }
  725. static ssize_t set_in16_min(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  726. {
  727. struct i2c_client *client = to_i2c_client(dev);
  728. struct adm1026_data *data = i2c_get_clientdata(client);
  729. int val = simple_strtol(buf, NULL, 10);
  730. mutex_lock(&data->update_lock);
  731. data->in_min[16] = INS_TO_REG(16, val + NEG12_OFFSET);
  732. adm1026_write_value(client, ADM1026_REG_IN_MIN[16], data->in_min[16]);
  733. mutex_unlock(&data->update_lock);
  734. return count;
  735. }
  736. static ssize_t show_in16_max(struct device *dev, struct device_attribute *attr, char *buf)
  737. {
  738. struct adm1026_data *data = adm1026_update_device(dev);
  739. return sprintf(buf,"%d\n", INS_FROM_REG(16, data->in_max[16])
  740. - NEG12_OFFSET);
  741. }
  742. static ssize_t set_in16_max(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  743. {
  744. struct i2c_client *client = to_i2c_client(dev);
  745. struct adm1026_data *data = i2c_get_clientdata(client);
  746. int val = simple_strtol(buf, NULL, 10);
  747. mutex_lock(&data->update_lock);
  748. data->in_max[16] = INS_TO_REG(16, val+NEG12_OFFSET);
  749. adm1026_write_value(client, ADM1026_REG_IN_MAX[16], data->in_max[16]);
  750. mutex_unlock(&data->update_lock);
  751. return count;
  752. }
  753. static SENSOR_DEVICE_ATTR(in16_input, S_IRUGO, show_in16, NULL, 16);
  754. static SENSOR_DEVICE_ATTR(in16_min, S_IRUGO | S_IWUSR, show_in16_min, set_in16_min, 16);
  755. static SENSOR_DEVICE_ATTR(in16_max, S_IRUGO | S_IWUSR, show_in16_max, set_in16_max, 16);
  756. /* Now add fan read/write functions */
  757. static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
  758. char *buf)
  759. {
  760. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  761. int nr = sensor_attr->index;
  762. struct adm1026_data *data = adm1026_update_device(dev);
  763. return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr],
  764. data->fan_div[nr]));
  765. }
  766. static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
  767. char *buf)
  768. {
  769. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  770. int nr = sensor_attr->index;
  771. struct adm1026_data *data = adm1026_update_device(dev);
  772. return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan_min[nr],
  773. data->fan_div[nr]));
  774. }
  775. static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
  776. const char *buf, size_t count)
  777. {
  778. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  779. int nr = sensor_attr->index;
  780. struct i2c_client *client = to_i2c_client(dev);
  781. struct adm1026_data *data = i2c_get_clientdata(client);
  782. int val = simple_strtol(buf, NULL, 10);
  783. mutex_lock(&data->update_lock);
  784. data->fan_min[nr] = FAN_TO_REG(val, data->fan_div[nr]);
  785. adm1026_write_value(client, ADM1026_REG_FAN_MIN(nr),
  786. data->fan_min[nr]);
  787. mutex_unlock(&data->update_lock);
  788. return count;
  789. }
  790. #define fan_offset(offset) \
  791. static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan, NULL, \
  792. offset - 1); \
  793. static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
  794. show_fan_min, set_fan_min, offset - 1);
  795. fan_offset(1);
  796. fan_offset(2);
  797. fan_offset(3);
  798. fan_offset(4);
  799. fan_offset(5);
  800. fan_offset(6);
  801. fan_offset(7);
  802. fan_offset(8);
  803. /* Adjust fan_min to account for new fan divisor */
  804. static void fixup_fan_min(struct device *dev, int fan, int old_div)
  805. {
  806. struct i2c_client *client = to_i2c_client(dev);
  807. struct adm1026_data *data = i2c_get_clientdata(client);
  808. int new_min;
  809. int new_div = data->fan_div[fan];
  810. /* 0 and 0xff are special. Don't adjust them */
  811. if (data->fan_min[fan] == 0 || data->fan_min[fan] == 0xff) {
  812. return;
  813. }
  814. new_min = data->fan_min[fan] * old_div / new_div;
  815. new_min = SENSORS_LIMIT(new_min, 1, 254);
  816. data->fan_min[fan] = new_min;
  817. adm1026_write_value(client, ADM1026_REG_FAN_MIN(fan), new_min);
  818. }
  819. /* Now add fan_div read/write functions */
  820. static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
  821. char *buf)
  822. {
  823. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  824. int nr = sensor_attr->index;
  825. struct adm1026_data *data = adm1026_update_device(dev);
  826. return sprintf(buf,"%d\n", data->fan_div[nr]);
  827. }
  828. static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
  829. const char *buf, size_t count)
  830. {
  831. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  832. int nr = sensor_attr->index;
  833. struct i2c_client *client = to_i2c_client(dev);
  834. struct adm1026_data *data = i2c_get_clientdata(client);
  835. int val,orig_div,new_div,shift;
  836. val = simple_strtol(buf, NULL, 10);
  837. new_div = DIV_TO_REG(val);
  838. if (new_div == 0) {
  839. return -EINVAL;
  840. }
  841. mutex_lock(&data->update_lock);
  842. orig_div = data->fan_div[nr];
  843. data->fan_div[nr] = DIV_FROM_REG(new_div);
  844. if (nr < 4) { /* 0 <= nr < 4 */
  845. shift = 2 * nr;
  846. adm1026_write_value(client, ADM1026_REG_FAN_DIV_0_3,
  847. ((DIV_TO_REG(orig_div) & (~(0x03 << shift))) |
  848. (new_div << shift)));
  849. } else { /* 3 < nr < 8 */
  850. shift = 2 * (nr - 4);
  851. adm1026_write_value(client, ADM1026_REG_FAN_DIV_4_7,
  852. ((DIV_TO_REG(orig_div) & (~(0x03 << (2 * shift)))) |
  853. (new_div << shift)));
  854. }
  855. if (data->fan_div[nr] != orig_div) {
  856. fixup_fan_min(dev,nr,orig_div);
  857. }
  858. mutex_unlock(&data->update_lock);
  859. return count;
  860. }
  861. #define fan_offset_div(offset) \
  862. static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
  863. show_fan_div, set_fan_div, offset - 1);
  864. fan_offset_div(1);
  865. fan_offset_div(2);
  866. fan_offset_div(3);
  867. fan_offset_div(4);
  868. fan_offset_div(5);
  869. fan_offset_div(6);
  870. fan_offset_div(7);
  871. fan_offset_div(8);
  872. /* Temps */
  873. static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
  874. char *buf)
  875. {
  876. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  877. int nr = sensor_attr->index;
  878. struct adm1026_data *data = adm1026_update_device(dev);
  879. return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp[nr]));
  880. }
  881. static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
  882. char *buf)
  883. {
  884. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  885. int nr = sensor_attr->index;
  886. struct adm1026_data *data = adm1026_update_device(dev);
  887. return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_min[nr]));
  888. }
  889. static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
  890. const char *buf, size_t count)
  891. {
  892. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  893. int nr = sensor_attr->index;
  894. struct i2c_client *client = to_i2c_client(dev);
  895. struct adm1026_data *data = i2c_get_clientdata(client);
  896. int val = simple_strtol(buf, NULL, 10);
  897. mutex_lock(&data->update_lock);
  898. data->temp_min[nr] = TEMP_TO_REG(val);
  899. adm1026_write_value(client, ADM1026_REG_TEMP_MIN[nr],
  900. data->temp_min[nr]);
  901. mutex_unlock(&data->update_lock);
  902. return count;
  903. }
  904. static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
  905. char *buf)
  906. {
  907. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  908. int nr = sensor_attr->index;
  909. struct adm1026_data *data = adm1026_update_device(dev);
  910. return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_max[nr]));
  911. }
  912. static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
  913. const char *buf, size_t count)
  914. {
  915. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  916. int nr = sensor_attr->index;
  917. struct i2c_client *client = to_i2c_client(dev);
  918. struct adm1026_data *data = i2c_get_clientdata(client);
  919. int val = simple_strtol(buf, NULL, 10);
  920. mutex_lock(&data->update_lock);
  921. data->temp_max[nr] = TEMP_TO_REG(val);
  922. adm1026_write_value(client, ADM1026_REG_TEMP_MAX[nr],
  923. data->temp_max[nr]);
  924. mutex_unlock(&data->update_lock);
  925. return count;
  926. }
  927. #define temp_reg(offset) \
  928. static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp, \
  929. NULL, offset - 1); \
  930. static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
  931. show_temp_min, set_temp_min, offset - 1); \
  932. static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
  933. show_temp_max, set_temp_max, offset - 1);
  934. temp_reg(1);
  935. temp_reg(2);
  936. temp_reg(3);
  937. static ssize_t show_temp_offset(struct device *dev,
  938. struct device_attribute *attr, char *buf)
  939. {
  940. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  941. int nr = sensor_attr->index;
  942. struct adm1026_data *data = adm1026_update_device(dev);
  943. return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_offset[nr]));
  944. }
  945. static ssize_t set_temp_offset(struct device *dev,
  946. struct device_attribute *attr, const char *buf,
  947. size_t count)
  948. {
  949. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  950. int nr = sensor_attr->index;
  951. struct i2c_client *client = to_i2c_client(dev);
  952. struct adm1026_data *data = i2c_get_clientdata(client);
  953. int val = simple_strtol(buf, NULL, 10);
  954. mutex_lock(&data->update_lock);
  955. data->temp_offset[nr] = TEMP_TO_REG(val);
  956. adm1026_write_value(client, ADM1026_REG_TEMP_OFFSET[nr],
  957. data->temp_offset[nr]);
  958. mutex_unlock(&data->update_lock);
  959. return count;
  960. }
  961. #define temp_offset_reg(offset) \
  962. static SENSOR_DEVICE_ATTR(temp##offset##_offset, S_IRUGO | S_IWUSR, \
  963. show_temp_offset, set_temp_offset, offset - 1);
  964. temp_offset_reg(1);
  965. temp_offset_reg(2);
  966. temp_offset_reg(3);
  967. static ssize_t show_temp_auto_point1_temp_hyst(struct device *dev,
  968. struct device_attribute *attr, char *buf)
  969. {
  970. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  971. int nr = sensor_attr->index;
  972. struct adm1026_data *data = adm1026_update_device(dev);
  973. return sprintf(buf,"%d\n", TEMP_FROM_REG(
  974. ADM1026_FAN_ACTIVATION_TEMP_HYST + data->temp_tmin[nr]));
  975. }
  976. static ssize_t show_temp_auto_point2_temp(struct device *dev,
  977. struct device_attribute *attr, char *buf)
  978. {
  979. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  980. int nr = sensor_attr->index;
  981. struct adm1026_data *data = adm1026_update_device(dev);
  982. return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_tmin[nr] +
  983. ADM1026_FAN_CONTROL_TEMP_RANGE));
  984. }
  985. static ssize_t show_temp_auto_point1_temp(struct device *dev,
  986. struct device_attribute *attr, char *buf)
  987. {
  988. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  989. int nr = sensor_attr->index;
  990. struct adm1026_data *data = adm1026_update_device(dev);
  991. return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_tmin[nr]));
  992. }
  993. static ssize_t set_temp_auto_point1_temp(struct device *dev,
  994. struct device_attribute *attr, const char *buf, size_t count)
  995. {
  996. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  997. int nr = sensor_attr->index;
  998. struct i2c_client *client = to_i2c_client(dev);
  999. struct adm1026_data *data = i2c_get_clientdata(client);
  1000. int val = simple_strtol(buf, NULL, 10);
  1001. mutex_lock(&data->update_lock);
  1002. data->temp_tmin[nr] = TEMP_TO_REG(val);
  1003. adm1026_write_value(client, ADM1026_REG_TEMP_TMIN[nr],
  1004. data->temp_tmin[nr]);
  1005. mutex_unlock(&data->update_lock);
  1006. return count;
  1007. }
  1008. #define temp_auto_point(offset) \
  1009. static SENSOR_DEVICE_ATTR(temp##offset##_auto_point1_temp, S_IRUGO | S_IWUSR, \
  1010. show_temp_auto_point1_temp, set_temp_auto_point1_temp, \
  1011. offset - 1); \
  1012. static SENSOR_DEVICE_ATTR(temp##offset##_auto_point1_temp_hyst, S_IRUGO, \
  1013. show_temp_auto_point1_temp_hyst, NULL, offset - 1); \
  1014. static SENSOR_DEVICE_ATTR(temp##offset##_auto_point2_temp, S_IRUGO, \
  1015. show_temp_auto_point2_temp, NULL, offset - 1);
  1016. temp_auto_point(1);
  1017. temp_auto_point(2);
  1018. temp_auto_point(3);
  1019. static ssize_t show_temp_crit_enable(struct device *dev,
  1020. struct device_attribute *attr, char *buf)
  1021. {
  1022. struct adm1026_data *data = adm1026_update_device(dev);
  1023. return sprintf(buf,"%d\n", (data->config1 & CFG1_THERM_HOT) >> 4);
  1024. }
  1025. static ssize_t set_temp_crit_enable(struct device *dev,
  1026. struct device_attribute *attr, const char *buf, size_t count)
  1027. {
  1028. struct i2c_client *client = to_i2c_client(dev);
  1029. struct adm1026_data *data = i2c_get_clientdata(client);
  1030. int val = simple_strtol(buf, NULL, 10);
  1031. if ((val == 1) || (val==0)) {
  1032. mutex_lock(&data->update_lock);
  1033. data->config1 = (data->config1 & ~CFG1_THERM_HOT) | (val << 4);
  1034. adm1026_write_value(client, ADM1026_REG_CONFIG1,
  1035. data->config1);
  1036. mutex_unlock(&data->update_lock);
  1037. }
  1038. return count;
  1039. }
  1040. #define temp_crit_enable(offset) \
  1041. static DEVICE_ATTR(temp##offset##_crit_enable, S_IRUGO | S_IWUSR, \
  1042. show_temp_crit_enable, set_temp_crit_enable);
  1043. temp_crit_enable(1);
  1044. temp_crit_enable(2);
  1045. temp_crit_enable(3);
  1046. static ssize_t show_temp_crit(struct device *dev,
  1047. struct device_attribute *attr, char *buf)
  1048. {
  1049. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  1050. int nr = sensor_attr->index;
  1051. struct adm1026_data *data = adm1026_update_device(dev);
  1052. return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_crit[nr]));
  1053. }
  1054. static ssize_t set_temp_crit(struct device *dev, struct device_attribute *attr,
  1055. const char *buf, size_t count)
  1056. {
  1057. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  1058. int nr = sensor_attr->index;
  1059. struct i2c_client *client = to_i2c_client(dev);
  1060. struct adm1026_data *data = i2c_get_clientdata(client);
  1061. int val = simple_strtol(buf, NULL, 10);
  1062. mutex_lock(&data->update_lock);
  1063. data->temp_crit[nr] = TEMP_TO_REG(val);
  1064. adm1026_write_value(client, ADM1026_REG_TEMP_THERM[nr],
  1065. data->temp_crit[nr]);
  1066. mutex_unlock(&data->update_lock);
  1067. return count;
  1068. }
  1069. #define temp_crit_reg(offset) \
  1070. static SENSOR_DEVICE_ATTR(temp##offset##_crit, S_IRUGO | S_IWUSR, \
  1071. show_temp_crit, set_temp_crit, offset - 1);
  1072. temp_crit_reg(1);
  1073. temp_crit_reg(2);
  1074. temp_crit_reg(3);
  1075. static ssize_t show_analog_out_reg(struct device *dev, struct device_attribute *attr, char *buf)
  1076. {
  1077. struct adm1026_data *data = adm1026_update_device(dev);
  1078. return sprintf(buf,"%d\n", DAC_FROM_REG(data->analog_out));
  1079. }
  1080. static ssize_t set_analog_out_reg(struct device *dev, struct device_attribute *attr, const char *buf,
  1081. size_t count)
  1082. {
  1083. struct i2c_client *client = to_i2c_client(dev);
  1084. struct adm1026_data *data = i2c_get_clientdata(client);
  1085. int val = simple_strtol(buf, NULL, 10);
  1086. mutex_lock(&data->update_lock);
  1087. data->analog_out = DAC_TO_REG(val);
  1088. adm1026_write_value(client, ADM1026_REG_DAC, data->analog_out);
  1089. mutex_unlock(&data->update_lock);
  1090. return count;
  1091. }
  1092. static DEVICE_ATTR(analog_out, S_IRUGO | S_IWUSR, show_analog_out_reg,
  1093. set_analog_out_reg);
  1094. static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
  1095. {
  1096. struct adm1026_data *data = adm1026_update_device(dev);
  1097. return sprintf(buf,"%d\n", vid_from_reg(data->vid & 0x3f, data->vrm));
  1098. }
  1099. static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
  1100. static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
  1101. {
  1102. struct adm1026_data *data = adm1026_update_device(dev);
  1103. return sprintf(buf,"%d\n", data->vrm);
  1104. }
  1105. static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf,
  1106. size_t count)
  1107. {
  1108. struct i2c_client *client = to_i2c_client(dev);
  1109. struct adm1026_data *data = i2c_get_clientdata(client);
  1110. data->vrm = simple_strtol(buf, NULL, 10);
  1111. return count;
  1112. }
  1113. static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
  1114. static ssize_t show_alarms_reg(struct device *dev, struct device_attribute *attr, char *buf)
  1115. {
  1116. struct adm1026_data *data = adm1026_update_device(dev);
  1117. return sprintf(buf, "%ld\n", (long) (data->alarms));
  1118. }
  1119. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
  1120. static ssize_t show_alarm_mask(struct device *dev, struct device_attribute *attr, char *buf)
  1121. {
  1122. struct adm1026_data *data = adm1026_update_device(dev);
  1123. return sprintf(buf,"%ld\n", data->alarm_mask);
  1124. }
  1125. static ssize_t set_alarm_mask(struct device *dev, struct device_attribute *attr, const char *buf,
  1126. size_t count)
  1127. {
  1128. struct i2c_client *client = to_i2c_client(dev);
  1129. struct adm1026_data *data = i2c_get_clientdata(client);
  1130. int val = simple_strtol(buf, NULL, 10);
  1131. unsigned long mask;
  1132. mutex_lock(&data->update_lock);
  1133. data->alarm_mask = val & 0x7fffffff;
  1134. mask = data->alarm_mask
  1135. | (data->gpio_mask & 0x10000 ? 0x80000000 : 0);
  1136. adm1026_write_value(client, ADM1026_REG_MASK1,
  1137. mask & 0xff);
  1138. mask >>= 8;
  1139. adm1026_write_value(client, ADM1026_REG_MASK2,
  1140. mask & 0xff);
  1141. mask >>= 8;
  1142. adm1026_write_value(client, ADM1026_REG_MASK3,
  1143. mask & 0xff);
  1144. mask >>= 8;
  1145. adm1026_write_value(client, ADM1026_REG_MASK4,
  1146. mask & 0xff);
  1147. mutex_unlock(&data->update_lock);
  1148. return count;
  1149. }
  1150. static DEVICE_ATTR(alarm_mask, S_IRUGO | S_IWUSR, show_alarm_mask,
  1151. set_alarm_mask);
  1152. static ssize_t show_gpio(struct device *dev, struct device_attribute *attr, char *buf)
  1153. {
  1154. struct adm1026_data *data = adm1026_update_device(dev);
  1155. return sprintf(buf,"%ld\n", data->gpio);
  1156. }
  1157. static ssize_t set_gpio(struct device *dev, struct device_attribute *attr, const char *buf,
  1158. size_t count)
  1159. {
  1160. struct i2c_client *client = to_i2c_client(dev);
  1161. struct adm1026_data *data = i2c_get_clientdata(client);
  1162. int val = simple_strtol(buf, NULL, 10);
  1163. long gpio;
  1164. mutex_lock(&data->update_lock);
  1165. data->gpio = val & 0x1ffff;
  1166. gpio = data->gpio;
  1167. adm1026_write_value(client, ADM1026_REG_GPIO_STATUS_0_7,gpio & 0xff);
  1168. gpio >>= 8;
  1169. adm1026_write_value(client, ADM1026_REG_GPIO_STATUS_8_15,gpio & 0xff);
  1170. gpio = ((gpio >> 1) & 0x80) | (data->alarms >> 24 & 0x7f);
  1171. adm1026_write_value(client, ADM1026_REG_STATUS4,gpio & 0xff);
  1172. mutex_unlock(&data->update_lock);
  1173. return count;
  1174. }
  1175. static DEVICE_ATTR(gpio, S_IRUGO | S_IWUSR, show_gpio, set_gpio);
  1176. static ssize_t show_gpio_mask(struct device *dev, struct device_attribute *attr, char *buf)
  1177. {
  1178. struct adm1026_data *data = adm1026_update_device(dev);
  1179. return sprintf(buf,"%ld\n", data->gpio_mask);
  1180. }
  1181. static ssize_t set_gpio_mask(struct device *dev, struct device_attribute *attr, const char *buf,
  1182. size_t count)
  1183. {
  1184. struct i2c_client *client = to_i2c_client(dev);
  1185. struct adm1026_data *data = i2c_get_clientdata(client);
  1186. int val = simple_strtol(buf, NULL, 10);
  1187. long mask;
  1188. mutex_lock(&data->update_lock);
  1189. data->gpio_mask = val & 0x1ffff;
  1190. mask = data->gpio_mask;
  1191. adm1026_write_value(client, ADM1026_REG_GPIO_MASK_0_7,mask & 0xff);
  1192. mask >>= 8;
  1193. adm1026_write_value(client, ADM1026_REG_GPIO_MASK_8_15,mask & 0xff);
  1194. mask = ((mask >> 1) & 0x80) | (data->alarm_mask >> 24 & 0x7f);
  1195. adm1026_write_value(client, ADM1026_REG_MASK1,mask & 0xff);
  1196. mutex_unlock(&data->update_lock);
  1197. return count;
  1198. }
  1199. static DEVICE_ATTR(gpio_mask, S_IRUGO | S_IWUSR, show_gpio_mask, set_gpio_mask);
  1200. static ssize_t show_pwm_reg(struct device *dev, struct device_attribute *attr, char *buf)
  1201. {
  1202. struct adm1026_data *data = adm1026_update_device(dev);
  1203. return sprintf(buf,"%d\n", PWM_FROM_REG(data->pwm1.pwm));
  1204. }
  1205. static ssize_t set_pwm_reg(struct device *dev, struct device_attribute *attr, const char *buf,
  1206. size_t count)
  1207. {
  1208. struct i2c_client *client = to_i2c_client(dev);
  1209. struct adm1026_data *data = i2c_get_clientdata(client);
  1210. if (data->pwm1.enable == 1) {
  1211. int val = simple_strtol(buf, NULL, 10);
  1212. mutex_lock(&data->update_lock);
  1213. data->pwm1.pwm = PWM_TO_REG(val);
  1214. adm1026_write_value(client, ADM1026_REG_PWM, data->pwm1.pwm);
  1215. mutex_unlock(&data->update_lock);
  1216. }
  1217. return count;
  1218. }
  1219. static ssize_t show_auto_pwm_min(struct device *dev, struct device_attribute *attr, char *buf)
  1220. {
  1221. struct adm1026_data *data = adm1026_update_device(dev);
  1222. return sprintf(buf,"%d\n", data->pwm1.auto_pwm_min);
  1223. }
  1224. static ssize_t set_auto_pwm_min(struct device *dev, struct device_attribute *attr, const char *buf,
  1225. size_t count)
  1226. {
  1227. struct i2c_client *client = to_i2c_client(dev);
  1228. struct adm1026_data *data = i2c_get_clientdata(client);
  1229. int val = simple_strtol(buf, NULL, 10);
  1230. mutex_lock(&data->update_lock);
  1231. data->pwm1.auto_pwm_min = SENSORS_LIMIT(val,0,255);
  1232. if (data->pwm1.enable == 2) { /* apply immediately */
  1233. data->pwm1.pwm = PWM_TO_REG((data->pwm1.pwm & 0x0f) |
  1234. PWM_MIN_TO_REG(data->pwm1.auto_pwm_min));
  1235. adm1026_write_value(client, ADM1026_REG_PWM, data->pwm1.pwm);
  1236. }
  1237. mutex_unlock(&data->update_lock);
  1238. return count;
  1239. }
  1240. static ssize_t show_auto_pwm_max(struct device *dev, struct device_attribute *attr, char *buf)
  1241. {
  1242. return sprintf(buf,"%d\n", ADM1026_PWM_MAX);
  1243. }
  1244. static ssize_t show_pwm_enable(struct device *dev, struct device_attribute *attr, char *buf)
  1245. {
  1246. struct adm1026_data *data = adm1026_update_device(dev);
  1247. return sprintf(buf,"%d\n", data->pwm1.enable);
  1248. }
  1249. static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *attr, const char *buf,
  1250. size_t count)
  1251. {
  1252. struct i2c_client *client = to_i2c_client(dev);
  1253. struct adm1026_data *data = i2c_get_clientdata(client);
  1254. int val = simple_strtol(buf, NULL, 10);
  1255. int old_enable;
  1256. if ((val >= 0) && (val < 3)) {
  1257. mutex_lock(&data->update_lock);
  1258. old_enable = data->pwm1.enable;
  1259. data->pwm1.enable = val;
  1260. data->config1 = (data->config1 & ~CFG1_PWM_AFC)
  1261. | ((val == 2) ? CFG1_PWM_AFC : 0);
  1262. adm1026_write_value(client, ADM1026_REG_CONFIG1,
  1263. data->config1);
  1264. if (val == 2) { /* apply pwm1_auto_pwm_min to pwm1 */
  1265. data->pwm1.pwm = PWM_TO_REG((data->pwm1.pwm & 0x0f) |
  1266. PWM_MIN_TO_REG(data->pwm1.auto_pwm_min));
  1267. adm1026_write_value(client, ADM1026_REG_PWM,
  1268. data->pwm1.pwm);
  1269. } else if (!((old_enable == 1) && (val == 1))) {
  1270. /* set pwm to safe value */
  1271. data->pwm1.pwm = 255;
  1272. adm1026_write_value(client, ADM1026_REG_PWM,
  1273. data->pwm1.pwm);
  1274. }
  1275. mutex_unlock(&data->update_lock);
  1276. }
  1277. return count;
  1278. }
  1279. /* enable PWM fan control */
  1280. static DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm_reg, set_pwm_reg);
  1281. static DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm_reg, set_pwm_reg);
  1282. static DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm_reg, set_pwm_reg);
  1283. static DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, show_pwm_enable,
  1284. set_pwm_enable);
  1285. static DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR, show_pwm_enable,
  1286. set_pwm_enable);
  1287. static DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR, show_pwm_enable,
  1288. set_pwm_enable);
  1289. static DEVICE_ATTR(temp1_auto_point1_pwm, S_IRUGO | S_IWUSR,
  1290. show_auto_pwm_min, set_auto_pwm_min);
  1291. static DEVICE_ATTR(temp2_auto_point1_pwm, S_IRUGO | S_IWUSR,
  1292. show_auto_pwm_min, set_auto_pwm_min);
  1293. static DEVICE_ATTR(temp3_auto_point1_pwm, S_IRUGO | S_IWUSR,
  1294. show_auto_pwm_min, set_auto_pwm_min);
  1295. static DEVICE_ATTR(temp1_auto_point2_pwm, S_IRUGO, show_auto_pwm_max, NULL);
  1296. static DEVICE_ATTR(temp2_auto_point2_pwm, S_IRUGO, show_auto_pwm_max, NULL);
  1297. static DEVICE_ATTR(temp3_auto_point2_pwm, S_IRUGO, show_auto_pwm_max, NULL);
  1298. static int adm1026_detect(struct i2c_adapter *adapter, int address,
  1299. int kind)
  1300. {
  1301. int company, verstep;
  1302. struct i2c_client *new_client;
  1303. struct adm1026_data *data;
  1304. int err = 0;
  1305. const char *type_name = "";
  1306. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
  1307. /* We need to be able to do byte I/O */
  1308. goto exit;
  1309. };
  1310. /* OK. For now, we presume we have a valid client. We now create the
  1311. client structure, even though we cannot fill it completely yet.
  1312. But it allows us to access adm1026_{read,write}_value. */
  1313. if (!(data = kzalloc(sizeof(struct adm1026_data), GFP_KERNEL))) {
  1314. err = -ENOMEM;
  1315. goto exit;
  1316. }
  1317. new_client = &data->client;
  1318. i2c_set_clientdata(new_client, data);
  1319. new_client->addr = address;
  1320. new_client->adapter = adapter;
  1321. new_client->driver = &adm1026_driver;
  1322. new_client->flags = 0;
  1323. /* Now, we do the remaining detection. */
  1324. company = adm1026_read_value(new_client, ADM1026_REG_COMPANY);
  1325. verstep = adm1026_read_value(new_client, ADM1026_REG_VERSTEP);
  1326. dev_dbg(&new_client->dev, "Detecting device at %d,0x%02x with"
  1327. " COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
  1328. i2c_adapter_id(new_client->adapter), new_client->addr,
  1329. company, verstep);
  1330. /* If auto-detecting, Determine the chip type. */
  1331. if (kind <= 0) {
  1332. dev_dbg(&new_client->dev, "Autodetecting device at %d,0x%02x "
  1333. "...\n", i2c_adapter_id(adapter), address);
  1334. if (company == ADM1026_COMPANY_ANALOG_DEV
  1335. && verstep == ADM1026_VERSTEP_ADM1026) {
  1336. kind = adm1026;
  1337. } else if (company == ADM1026_COMPANY_ANALOG_DEV
  1338. && (verstep & 0xf0) == ADM1026_VERSTEP_GENERIC) {
  1339. dev_err(&adapter->dev, ": Unrecognized stepping "
  1340. "0x%02x. Defaulting to ADM1026.\n", verstep);
  1341. kind = adm1026;
  1342. } else if ((verstep & 0xf0) == ADM1026_VERSTEP_GENERIC) {
  1343. dev_err(&adapter->dev, ": Found version/stepping "
  1344. "0x%02x. Assuming generic ADM1026.\n",
  1345. verstep);
  1346. kind = any_chip;
  1347. } else {
  1348. dev_dbg(&new_client->dev, ": Autodetection "
  1349. "failed\n");
  1350. /* Not an ADM1026 ... */
  1351. if (kind == 0) { /* User used force=x,y */
  1352. dev_err(&adapter->dev, "Generic ADM1026 not "
  1353. "found at %d,0x%02x. Try "
  1354. "force_adm1026.\n",
  1355. i2c_adapter_id(adapter), address);
  1356. }
  1357. err = 0;
  1358. goto exitfree;
  1359. }
  1360. }
  1361. /* Fill in the chip specific driver values */
  1362. switch (kind) {
  1363. case any_chip :
  1364. type_name = "adm1026";
  1365. break;
  1366. case adm1026 :
  1367. type_name = "adm1026";
  1368. break;
  1369. default :
  1370. dev_err(&adapter->dev, ": Internal error, invalid "
  1371. "kind (%d)!", kind);
  1372. err = -EFAULT;
  1373. goto exitfree;
  1374. }
  1375. strlcpy(new_client->name, type_name, I2C_NAME_SIZE);
  1376. /* Fill in the remaining client fields */
  1377. data->type = kind;
  1378. data->valid = 0;
  1379. mutex_init(&data->update_lock);
  1380. /* Tell the I2C layer a new client has arrived */
  1381. if ((err = i2c_attach_client(new_client)))
  1382. goto exitfree;
  1383. /* Set the VRM version */
  1384. data->vrm = vid_which_vrm();
  1385. /* Initialize the ADM1026 chip */
  1386. adm1026_init_client(new_client);
  1387. /* Register sysfs hooks */
  1388. data->class_dev = hwmon_device_register(&new_client->dev);
  1389. if (IS_ERR(data->class_dev)) {
  1390. err = PTR_ERR(data->class_dev);
  1391. goto exitdetach;
  1392. }
  1393. device_create_file(&new_client->dev, &sensor_dev_attr_in0_input.dev_attr);
  1394. device_create_file(&new_client->dev, &sensor_dev_attr_in0_max.dev_attr);
  1395. device_create_file(&new_client->dev, &sensor_dev_attr_in0_min.dev_attr);
  1396. device_create_file(&new_client->dev, &sensor_dev_attr_in1_input.dev_attr);
  1397. device_create_file(&new_client->dev, &sensor_dev_attr_in1_max.dev_attr);
  1398. device_create_file(&new_client->dev, &sensor_dev_attr_in1_min.dev_attr);
  1399. device_create_file(&new_client->dev, &sensor_dev_attr_in2_input.dev_attr);
  1400. device_create_file(&new_client->dev, &sensor_dev_attr_in2_max.dev_attr);
  1401. device_create_file(&new_client->dev, &sensor_dev_attr_in2_min.dev_attr);
  1402. device_create_file(&new_client->dev, &sensor_dev_attr_in3_input.dev_attr);
  1403. device_create_file(&new_client->dev, &sensor_dev_attr_in3_max.dev_attr);
  1404. device_create_file(&new_client->dev, &sensor_dev_attr_in3_min.dev_attr);
  1405. device_create_file(&new_client->dev, &sensor_dev_attr_in4_input.dev_attr);
  1406. device_create_file(&new_client->dev, &sensor_dev_attr_in4_max.dev_attr);
  1407. device_create_file(&new_client->dev, &sensor_dev_attr_in4_min.dev_attr);
  1408. device_create_file(&new_client->dev, &sensor_dev_attr_in5_input.dev_attr);
  1409. device_create_file(&new_client->dev, &sensor_dev_attr_in5_max.dev_attr);
  1410. device_create_file(&new_client->dev, &sensor_dev_attr_in5_min.dev_attr);
  1411. device_create_file(&new_client->dev, &sensor_dev_attr_in6_input.dev_attr);
  1412. device_create_file(&new_client->dev, &sensor_dev_attr_in6_max.dev_attr);
  1413. device_create_file(&new_client->dev, &sensor_dev_attr_in6_min.dev_attr);
  1414. device_create_file(&new_client->dev, &sensor_dev_attr_in7_input.dev_attr);
  1415. device_create_file(&new_client->dev, &sensor_dev_attr_in7_max.dev_attr);
  1416. device_create_file(&new_client->dev, &sensor_dev_attr_in7_min.dev_attr);
  1417. device_create_file(&new_client->dev, &sensor_dev_attr_in8_input.dev_attr);
  1418. device_create_file(&new_client->dev, &sensor_dev_attr_in8_max.dev_attr);
  1419. device_create_file(&new_client->dev, &sensor_dev_attr_in8_min.dev_attr);
  1420. device_create_file(&new_client->dev, &sensor_dev_attr_in9_input.dev_attr);
  1421. device_create_file(&new_client->dev, &sensor_dev_attr_in9_max.dev_attr);
  1422. device_create_file(&new_client->dev, &sensor_dev_attr_in9_min.dev_attr);
  1423. device_create_file(&new_client->dev, &sensor_dev_attr_in10_input.dev_attr);
  1424. device_create_file(&new_client->dev, &sensor_dev_attr_in10_max.dev_attr);
  1425. device_create_file(&new_client->dev, &sensor_dev_attr_in10_min.dev_attr);
  1426. device_create_file(&new_client->dev, &sensor_dev_attr_in11_input.dev_attr);
  1427. device_create_file(&new_client->dev, &sensor_dev_attr_in11_max.dev_attr);
  1428. device_create_file(&new_client->dev, &sensor_dev_attr_in11_min.dev_attr);
  1429. device_create_file(&new_client->dev, &sensor_dev_attr_in12_input.dev_attr);
  1430. device_create_file(&new_client->dev, &sensor_dev_attr_in12_max.dev_attr);
  1431. device_create_file(&new_client->dev, &sensor_dev_attr_in12_min.dev_attr);
  1432. device_create_file(&new_client->dev, &sensor_dev_attr_in13_input.dev_attr);
  1433. device_create_file(&new_client->dev, &sensor_dev_attr_in13_max.dev_attr);
  1434. device_create_file(&new_client->dev, &sensor_dev_attr_in13_min.dev_attr);
  1435. device_create_file(&new_client->dev, &sensor_dev_attr_in14_input.dev_attr);
  1436. device_create_file(&new_client->dev, &sensor_dev_attr_in14_max.dev_attr);
  1437. device_create_file(&new_client->dev, &sensor_dev_attr_in14_min.dev_attr);
  1438. device_create_file(&new_client->dev, &sensor_dev_attr_in15_input.dev_attr);
  1439. device_create_file(&new_client->dev, &sensor_dev_attr_in15_max.dev_attr);
  1440. device_create_file(&new_client->dev, &sensor_dev_attr_in15_min.dev_attr);
  1441. device_create_file(&new_client->dev, &sensor_dev_attr_in16_input.dev_attr);
  1442. device_create_file(&new_client->dev, &sensor_dev_attr_in16_max.dev_attr);
  1443. device_create_file(&new_client->dev, &sensor_dev_attr_in16_min.dev_attr);
  1444. device_create_file(&new_client->dev, &sensor_dev_attr_fan1_input.dev_attr);
  1445. device_create_file(&new_client->dev, &sensor_dev_attr_fan1_div.dev_attr);
  1446. device_create_file(&new_client->dev, &sensor_dev_attr_fan1_min.dev_attr);
  1447. device_create_file(&new_client->dev, &sensor_dev_attr_fan2_input.dev_attr);
  1448. device_create_file(&new_client->dev, &sensor_dev_attr_fan2_div.dev_attr);
  1449. device_create_file(&new_client->dev, &sensor_dev_attr_fan2_min.dev_attr);
  1450. device_create_file(&new_client->dev, &sensor_dev_attr_fan3_input.dev_attr);
  1451. device_create_file(&new_client->dev, &sensor_dev_attr_fan3_div.dev_attr);
  1452. device_create_file(&new_client->dev, &sensor_dev_attr_fan3_min.dev_attr);
  1453. device_create_file(&new_client->dev, &sensor_dev_attr_fan4_input.dev_attr);
  1454. device_create_file(&new_client->dev, &sensor_dev_attr_fan4_div.dev_attr);
  1455. device_create_file(&new_client->dev, &sensor_dev_attr_fan4_min.dev_attr);
  1456. device_create_file(&new_client->dev, &sensor_dev_attr_fan5_input.dev_attr);
  1457. device_create_file(&new_client->dev, &sensor_dev_attr_fan5_div.dev_attr);
  1458. device_create_file(&new_client->dev, &sensor_dev_attr_fan5_min.dev_attr);
  1459. device_create_file(&new_client->dev, &sensor_dev_attr_fan6_input.dev_attr);
  1460. device_create_file(&new_client->dev, &sensor_dev_attr_fan6_div.dev_attr);
  1461. device_create_file(&new_client->dev, &sensor_dev_attr_fan6_min.dev_attr);
  1462. device_create_file(&new_client->dev, &sensor_dev_attr_fan7_input.dev_attr);
  1463. device_create_file(&new_client->dev, &sensor_dev_attr_fan7_div.dev_attr);
  1464. device_create_file(&new_client->dev, &sensor_dev_attr_fan7_min.dev_attr);
  1465. device_create_file(&new_client->dev, &sensor_dev_attr_fan8_input.dev_attr);
  1466. device_create_file(&new_client->dev, &sensor_dev_attr_fan8_div.dev_attr);
  1467. device_create_file(&new_client->dev, &sensor_dev_attr_fan8_min.dev_attr);
  1468. device_create_file(&new_client->dev, &sensor_dev_attr_temp1_input.dev_attr);
  1469. device_create_file(&new_client->dev, &sensor_dev_attr_temp1_max.dev_attr);
  1470. device_create_file(&new_client->dev, &sensor_dev_attr_temp1_min.dev_attr);
  1471. device_create_file(&new_client->dev, &sensor_dev_attr_temp2_input.dev_attr);
  1472. device_create_file(&new_client->dev, &sensor_dev_attr_temp2_max.dev_attr);
  1473. device_create_file(&new_client->dev, &sensor_dev_attr_temp2_min.dev_attr);
  1474. device_create_file(&new_client->dev, &sensor_dev_attr_temp3_input.dev_attr);
  1475. device_create_file(&new_client->dev, &sensor_dev_attr_temp3_max.dev_attr);
  1476. device_create_file(&new_client->dev, &sensor_dev_attr_temp3_min.dev_attr);
  1477. device_create_file(&new_client->dev, &sensor_dev_attr_temp1_offset.dev_attr);
  1478. device_create_file(&new_client->dev, &sensor_dev_attr_temp2_offset.dev_attr);
  1479. device_create_file(&new_client->dev, &sensor_dev_attr_temp3_offset.dev_attr);
  1480. device_create_file(&new_client->dev,
  1481. &sensor_dev_attr_temp1_auto_point1_temp.dev_attr);
  1482. device_create_file(&new_client->dev,
  1483. &sensor_dev_attr_temp2_auto_point1_temp.dev_attr);
  1484. device_create_file(&new_client->dev,
  1485. &sensor_dev_attr_temp3_auto_point1_temp.dev_attr);
  1486. device_create_file(&new_client->dev,
  1487. &sensor_dev_attr_temp1_auto_point1_temp_hyst.dev_attr);
  1488. device_create_file(&new_client->dev,
  1489. &sensor_dev_attr_temp2_auto_point1_temp_hyst.dev_attr);
  1490. device_create_file(&new_client->dev,
  1491. &sensor_dev_attr_temp3_auto_point1_temp_hyst.dev_attr);
  1492. device_create_file(&new_client->dev,
  1493. &sensor_dev_attr_temp1_auto_point2_temp.dev_attr);
  1494. device_create_file(&new_client->dev,
  1495. &sensor_dev_attr_temp2_auto_point2_temp.dev_attr);
  1496. device_create_file(&new_client->dev,
  1497. &sensor_dev_attr_temp3_auto_point2_temp.dev_attr);
  1498. device_create_file(&new_client->dev, &sensor_dev_attr_temp1_crit.dev_attr);
  1499. device_create_file(&new_client->dev, &sensor_dev_attr_temp2_crit.dev_attr);
  1500. device_create_file(&new_client->dev, &sensor_dev_attr_temp3_crit.dev_attr);
  1501. device_create_file(&new_client->dev, &dev_attr_temp1_crit_enable);
  1502. device_create_file(&new_client->dev, &dev_attr_temp2_crit_enable);
  1503. device_create_file(&new_client->dev, &dev_attr_temp3_crit_enable);
  1504. device_create_file(&new_client->dev, &dev_attr_cpu0_vid);
  1505. device_create_file(&new_client->dev, &dev_attr_vrm);
  1506. device_create_file(&new_client->dev, &dev_attr_alarms);
  1507. device_create_file(&new_client->dev, &dev_attr_alarm_mask);
  1508. device_create_file(&new_client->dev, &dev_attr_gpio);
  1509. device_create_file(&new_client->dev, &dev_attr_gpio_mask);
  1510. device_create_file(&new_client->dev, &dev_attr_pwm1);
  1511. device_create_file(&new_client->dev, &dev_attr_pwm2);
  1512. device_create_file(&new_client->dev, &dev_attr_pwm3);
  1513. device_create_file(&new_client->dev, &dev_attr_pwm1_enable);
  1514. device_create_file(&new_client->dev, &dev_attr_pwm2_enable);
  1515. device_create_file(&new_client->dev, &dev_attr_pwm3_enable);
  1516. device_create_file(&new_client->dev, &dev_attr_temp1_auto_point1_pwm);
  1517. device_create_file(&new_client->dev, &dev_attr_temp2_auto_point1_pwm);
  1518. device_create_file(&new_client->dev, &dev_attr_temp3_auto_point1_pwm);
  1519. device_create_file(&new_client->dev, &dev_attr_temp1_auto_point2_pwm);
  1520. device_create_file(&new_client->dev, &dev_attr_temp2_auto_point2_pwm);
  1521. device_create_file(&new_client->dev, &dev_attr_temp3_auto_point2_pwm);
  1522. device_create_file(&new_client->dev, &dev_attr_analog_out);
  1523. return 0;
  1524. /* Error out and cleanup code */
  1525. exitdetach:
  1526. i2c_detach_client(new_client);
  1527. exitfree:
  1528. kfree(data);
  1529. exit:
  1530. return err;
  1531. }
  1532. static int __init sm_adm1026_init(void)
  1533. {
  1534. return i2c_add_driver(&adm1026_driver);
  1535. }
  1536. static void __exit sm_adm1026_exit(void)
  1537. {
  1538. i2c_del_driver(&adm1026_driver);
  1539. }
  1540. MODULE_LICENSE("GPL");
  1541. MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, "
  1542. "Justin Thiessen <jthiessen@penguincomputing.com>");
  1543. MODULE_DESCRIPTION("ADM1026 driver");
  1544. module_init(sm_adm1026_init);
  1545. module_exit(sm_adm1026_exit);