adt7475.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635
  1. /*
  2. * adt7475 - Thermal sensor driver for the ADT7475 chip and derivatives
  3. * Copyright (C) 2007-2008, Advanced Micro Devices, Inc.
  4. * Copyright (C) 2008 Jordan Crouse <jordan@cosmicpenguin.net>
  5. * Copyright (C) 2008 Hans de Goede <hdegoede@redhat.com>
  6. * Copyright (C) 2009 Jean Delvare <jdelvare@suse.de>
  7. *
  8. * Derived from the lm83 driver by Jean Delvare
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/slab.h>
  17. #include <linux/i2c.h>
  18. #include <linux/hwmon.h>
  19. #include <linux/hwmon-sysfs.h>
  20. #include <linux/hwmon-vid.h>
  21. #include <linux/err.h>
  22. #include <linux/jiffies.h>
  23. /* Indexes for the sysfs hooks */
  24. #define INPUT 0
  25. #define MIN 1
  26. #define MAX 2
  27. #define CONTROL 3
  28. #define OFFSET 3
  29. #define AUTOMIN 4
  30. #define THERM 5
  31. #define HYSTERSIS 6
  32. /*
  33. * These are unique identifiers for the sysfs functions - unlike the
  34. * numbers above, these are not also indexes into an array
  35. */
  36. #define ALARM 9
  37. #define FAULT 10
  38. /* 7475 Common Registers */
  39. #define REG_DEVREV2 0x12 /* ADT7490 only */
  40. #define REG_VTT 0x1E /* ADT7490 only */
  41. #define REG_EXTEND3 0x1F /* ADT7490 only */
  42. #define REG_VOLTAGE_BASE 0x20
  43. #define REG_TEMP_BASE 0x25
  44. #define REG_TACH_BASE 0x28
  45. #define REG_PWM_BASE 0x30
  46. #define REG_PWM_MAX_BASE 0x38
  47. #define REG_DEVID 0x3D
  48. #define REG_VENDID 0x3E
  49. #define REG_DEVID2 0x3F
  50. #define REG_STATUS1 0x41
  51. #define REG_STATUS2 0x42
  52. #define REG_VID 0x43 /* ADT7476 only */
  53. #define REG_VOLTAGE_MIN_BASE 0x44
  54. #define REG_VOLTAGE_MAX_BASE 0x45
  55. #define REG_TEMP_MIN_BASE 0x4E
  56. #define REG_TEMP_MAX_BASE 0x4F
  57. #define REG_TACH_MIN_BASE 0x54
  58. #define REG_PWM_CONFIG_BASE 0x5C
  59. #define REG_TEMP_TRANGE_BASE 0x5F
  60. #define REG_PWM_MIN_BASE 0x64
  61. #define REG_TEMP_TMIN_BASE 0x67
  62. #define REG_TEMP_THERM_BASE 0x6A
  63. #define REG_REMOTE1_HYSTERSIS 0x6D
  64. #define REG_REMOTE2_HYSTERSIS 0x6E
  65. #define REG_TEMP_OFFSET_BASE 0x70
  66. #define REG_CONFIG2 0x73
  67. #define REG_EXTEND1 0x76
  68. #define REG_EXTEND2 0x77
  69. #define REG_CONFIG3 0x78
  70. #define REG_CONFIG5 0x7C
  71. #define REG_CONFIG4 0x7D
  72. #define REG_STATUS4 0x81 /* ADT7490 only */
  73. #define REG_VTT_MIN 0x84 /* ADT7490 only */
  74. #define REG_VTT_MAX 0x86 /* ADT7490 only */
  75. #define VID_VIDSEL 0x80 /* ADT7476 only */
  76. #define CONFIG2_ATTN 0x20
  77. #define CONFIG3_SMBALERT 0x01
  78. #define CONFIG3_THERM 0x02
  79. #define CONFIG4_PINFUNC 0x03
  80. #define CONFIG4_MAXDUTY 0x08
  81. #define CONFIG4_ATTN_IN10 0x30
  82. #define CONFIG4_ATTN_IN43 0xC0
  83. #define CONFIG5_TWOSCOMP 0x01
  84. #define CONFIG5_TEMPOFFSET 0x02
  85. #define CONFIG5_VIDGPIO 0x10 /* ADT7476 only */
  86. /* ADT7475 Settings */
  87. #define ADT7475_VOLTAGE_COUNT 5 /* Not counting Vtt */
  88. #define ADT7475_TEMP_COUNT 3
  89. #define ADT7475_TACH_COUNT 4
  90. #define ADT7475_PWM_COUNT 3
  91. /* Macro to read the registers */
  92. #define adt7475_read(reg) i2c_smbus_read_byte_data(client, (reg))
  93. /* Macros to easily index the registers */
  94. #define TACH_REG(idx) (REG_TACH_BASE + ((idx) * 2))
  95. #define TACH_MIN_REG(idx) (REG_TACH_MIN_BASE + ((idx) * 2))
  96. #define PWM_REG(idx) (REG_PWM_BASE + (idx))
  97. #define PWM_MAX_REG(idx) (REG_PWM_MAX_BASE + (idx))
  98. #define PWM_MIN_REG(idx) (REG_PWM_MIN_BASE + (idx))
  99. #define PWM_CONFIG_REG(idx) (REG_PWM_CONFIG_BASE + (idx))
  100. #define VOLTAGE_REG(idx) (REG_VOLTAGE_BASE + (idx))
  101. #define VOLTAGE_MIN_REG(idx) (REG_VOLTAGE_MIN_BASE + ((idx) * 2))
  102. #define VOLTAGE_MAX_REG(idx) (REG_VOLTAGE_MAX_BASE + ((idx) * 2))
  103. #define TEMP_REG(idx) (REG_TEMP_BASE + (idx))
  104. #define TEMP_MIN_REG(idx) (REG_TEMP_MIN_BASE + ((idx) * 2))
  105. #define TEMP_MAX_REG(idx) (REG_TEMP_MAX_BASE + ((idx) * 2))
  106. #define TEMP_TMIN_REG(idx) (REG_TEMP_TMIN_BASE + (idx))
  107. #define TEMP_THERM_REG(idx) (REG_TEMP_THERM_BASE + (idx))
  108. #define TEMP_OFFSET_REG(idx) (REG_TEMP_OFFSET_BASE + (idx))
  109. #define TEMP_TRANGE_REG(idx) (REG_TEMP_TRANGE_BASE + (idx))
  110. static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
  111. enum chips { adt7473, adt7475, adt7476, adt7490 };
  112. static const struct i2c_device_id adt7475_id[] = {
  113. { "adt7473", adt7473 },
  114. { "adt7475", adt7475 },
  115. { "adt7476", adt7476 },
  116. { "adt7490", adt7490 },
  117. { }
  118. };
  119. MODULE_DEVICE_TABLE(i2c, adt7475_id);
  120. struct adt7475_data {
  121. struct device *hwmon_dev;
  122. struct mutex lock;
  123. unsigned long measure_updated;
  124. unsigned long limits_updated;
  125. char valid;
  126. u8 config4;
  127. u8 config5;
  128. u8 has_voltage;
  129. u8 bypass_attn; /* Bypass voltage attenuator */
  130. u8 has_pwm2:1;
  131. u8 has_fan4:1;
  132. u8 has_vid:1;
  133. u32 alarms;
  134. u16 voltage[3][6];
  135. u16 temp[7][3];
  136. u16 tach[2][4];
  137. u8 pwm[4][3];
  138. u8 range[3];
  139. u8 pwmctl[3];
  140. u8 pwmchan[3];
  141. u8 vid;
  142. u8 vrm;
  143. };
  144. static struct i2c_driver adt7475_driver;
  145. static struct adt7475_data *adt7475_update_device(struct device *dev);
  146. static void adt7475_read_hystersis(struct i2c_client *client);
  147. static void adt7475_read_pwm(struct i2c_client *client, int index);
  148. /* Given a temp value, convert it to register value */
  149. static inline u16 temp2reg(struct adt7475_data *data, long val)
  150. {
  151. u16 ret;
  152. if (!(data->config5 & CONFIG5_TWOSCOMP)) {
  153. val = clamp_val(val, -64000, 191000);
  154. ret = (val + 64500) / 1000;
  155. } else {
  156. val = clamp_val(val, -128000, 127000);
  157. if (val < -500)
  158. ret = (256500 + val) / 1000;
  159. else
  160. ret = (val + 500) / 1000;
  161. }
  162. return ret << 2;
  163. }
  164. /* Given a register value, convert it to a real temp value */
  165. static inline int reg2temp(struct adt7475_data *data, u16 reg)
  166. {
  167. if (data->config5 & CONFIG5_TWOSCOMP) {
  168. if (reg >= 512)
  169. return (reg - 1024) * 250;
  170. else
  171. return reg * 250;
  172. } else
  173. return (reg - 256) * 250;
  174. }
  175. static inline int tach2rpm(u16 tach)
  176. {
  177. if (tach == 0 || tach == 0xFFFF)
  178. return 0;
  179. return (90000 * 60) / tach;
  180. }
  181. static inline u16 rpm2tach(unsigned long rpm)
  182. {
  183. if (rpm == 0)
  184. return 0;
  185. return clamp_val((90000 * 60) / rpm, 1, 0xFFFF);
  186. }
  187. /* Scaling factors for voltage inputs, taken from the ADT7490 datasheet */
  188. static const int adt7473_in_scaling[ADT7475_VOLTAGE_COUNT + 1][2] = {
  189. { 45, 94 }, /* +2.5V */
  190. { 175, 525 }, /* Vccp */
  191. { 68, 71 }, /* Vcc */
  192. { 93, 47 }, /* +5V */
  193. { 120, 20 }, /* +12V */
  194. { 45, 45 }, /* Vtt */
  195. };
  196. static inline int reg2volt(int channel, u16 reg, u8 bypass_attn)
  197. {
  198. const int *r = adt7473_in_scaling[channel];
  199. if (bypass_attn & (1 << channel))
  200. return DIV_ROUND_CLOSEST(reg * 2250, 1024);
  201. return DIV_ROUND_CLOSEST(reg * (r[0] + r[1]) * 2250, r[1] * 1024);
  202. }
  203. static inline u16 volt2reg(int channel, long volt, u8 bypass_attn)
  204. {
  205. const int *r = adt7473_in_scaling[channel];
  206. long reg;
  207. if (bypass_attn & (1 << channel))
  208. reg = (volt * 1024) / 2250;
  209. else
  210. reg = (volt * r[1] * 1024) / ((r[0] + r[1]) * 2250);
  211. return clamp_val(reg, 0, 1023) & (0xff << 2);
  212. }
  213. static int adt7475_read_word(struct i2c_client *client, int reg)
  214. {
  215. int val1, val2;
  216. val1 = i2c_smbus_read_byte_data(client, reg);
  217. if (val1 < 0)
  218. return val1;
  219. val2 = i2c_smbus_read_byte_data(client, reg + 1);
  220. if (val2 < 0)
  221. return val2;
  222. return val1 | (val2 << 8);
  223. }
  224. static void adt7475_write_word(struct i2c_client *client, int reg, u16 val)
  225. {
  226. i2c_smbus_write_byte_data(client, reg + 1, val >> 8);
  227. i2c_smbus_write_byte_data(client, reg, val & 0xFF);
  228. }
  229. /*
  230. * Find the nearest value in a table - used for pwm frequency and
  231. * auto temp range
  232. */
  233. static int find_nearest(long val, const int *array, int size)
  234. {
  235. int i;
  236. if (val < array[0])
  237. return 0;
  238. if (val > array[size - 1])
  239. return size - 1;
  240. for (i = 0; i < size - 1; i++) {
  241. int a, b;
  242. if (val > array[i + 1])
  243. continue;
  244. a = val - array[i];
  245. b = array[i + 1] - val;
  246. return (a <= b) ? i : i + 1;
  247. }
  248. return 0;
  249. }
  250. static ssize_t show_voltage(struct device *dev, struct device_attribute *attr,
  251. char *buf)
  252. {
  253. struct adt7475_data *data = adt7475_update_device(dev);
  254. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  255. unsigned short val;
  256. switch (sattr->nr) {
  257. case ALARM:
  258. return sprintf(buf, "%d\n",
  259. (data->alarms >> sattr->index) & 1);
  260. default:
  261. val = data->voltage[sattr->nr][sattr->index];
  262. return sprintf(buf, "%d\n",
  263. reg2volt(sattr->index, val, data->bypass_attn));
  264. }
  265. }
  266. static ssize_t set_voltage(struct device *dev, struct device_attribute *attr,
  267. const char *buf, size_t count)
  268. {
  269. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  270. struct i2c_client *client = to_i2c_client(dev);
  271. struct adt7475_data *data = i2c_get_clientdata(client);
  272. unsigned char reg;
  273. long val;
  274. if (kstrtol(buf, 10, &val))
  275. return -EINVAL;
  276. mutex_lock(&data->lock);
  277. data->voltage[sattr->nr][sattr->index] =
  278. volt2reg(sattr->index, val, data->bypass_attn);
  279. if (sattr->index < ADT7475_VOLTAGE_COUNT) {
  280. if (sattr->nr == MIN)
  281. reg = VOLTAGE_MIN_REG(sattr->index);
  282. else
  283. reg = VOLTAGE_MAX_REG(sattr->index);
  284. } else {
  285. if (sattr->nr == MIN)
  286. reg = REG_VTT_MIN;
  287. else
  288. reg = REG_VTT_MAX;
  289. }
  290. i2c_smbus_write_byte_data(client, reg,
  291. data->voltage[sattr->nr][sattr->index] >> 2);
  292. mutex_unlock(&data->lock);
  293. return count;
  294. }
  295. static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
  296. char *buf)
  297. {
  298. struct adt7475_data *data = adt7475_update_device(dev);
  299. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  300. int out;
  301. switch (sattr->nr) {
  302. case HYSTERSIS:
  303. mutex_lock(&data->lock);
  304. out = data->temp[sattr->nr][sattr->index];
  305. if (sattr->index != 1)
  306. out = (out >> 4) & 0xF;
  307. else
  308. out = (out & 0xF);
  309. /*
  310. * Show the value as an absolute number tied to
  311. * THERM
  312. */
  313. out = reg2temp(data, data->temp[THERM][sattr->index]) -
  314. out * 1000;
  315. mutex_unlock(&data->lock);
  316. break;
  317. case OFFSET:
  318. /*
  319. * Offset is always 2's complement, regardless of the
  320. * setting in CONFIG5
  321. */
  322. mutex_lock(&data->lock);
  323. out = (s8)data->temp[sattr->nr][sattr->index];
  324. if (data->config5 & CONFIG5_TEMPOFFSET)
  325. out *= 1000;
  326. else
  327. out *= 500;
  328. mutex_unlock(&data->lock);
  329. break;
  330. case ALARM:
  331. out = (data->alarms >> (sattr->index + 4)) & 1;
  332. break;
  333. case FAULT:
  334. /* Note - only for remote1 and remote2 */
  335. out = !!(data->alarms & (sattr->index ? 0x8000 : 0x4000));
  336. break;
  337. default:
  338. /* All other temp values are in the configured format */
  339. out = reg2temp(data, data->temp[sattr->nr][sattr->index]);
  340. }
  341. return sprintf(buf, "%d\n", out);
  342. }
  343. static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
  344. const char *buf, size_t count)
  345. {
  346. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  347. struct i2c_client *client = to_i2c_client(dev);
  348. struct adt7475_data *data = i2c_get_clientdata(client);
  349. unsigned char reg = 0;
  350. u8 out;
  351. int temp;
  352. long val;
  353. if (kstrtol(buf, 10, &val))
  354. return -EINVAL;
  355. mutex_lock(&data->lock);
  356. /* We need the config register in all cases for temp <-> reg conv. */
  357. data->config5 = adt7475_read(REG_CONFIG5);
  358. switch (sattr->nr) {
  359. case OFFSET:
  360. if (data->config5 & CONFIG5_TEMPOFFSET) {
  361. val = clamp_val(val, -63000, 127000);
  362. out = data->temp[OFFSET][sattr->index] = val / 1000;
  363. } else {
  364. val = clamp_val(val, -63000, 64000);
  365. out = data->temp[OFFSET][sattr->index] = val / 500;
  366. }
  367. break;
  368. case HYSTERSIS:
  369. /*
  370. * The value will be given as an absolute value, turn it
  371. * into an offset based on THERM
  372. */
  373. /* Read fresh THERM and HYSTERSIS values from the chip */
  374. data->temp[THERM][sattr->index] =
  375. adt7475_read(TEMP_THERM_REG(sattr->index)) << 2;
  376. adt7475_read_hystersis(client);
  377. temp = reg2temp(data, data->temp[THERM][sattr->index]);
  378. val = clamp_val(val, temp - 15000, temp);
  379. val = (temp - val) / 1000;
  380. if (sattr->index != 1) {
  381. data->temp[HYSTERSIS][sattr->index] &= 0xF0;
  382. data->temp[HYSTERSIS][sattr->index] |= (val & 0xF) << 4;
  383. } else {
  384. data->temp[HYSTERSIS][sattr->index] &= 0x0F;
  385. data->temp[HYSTERSIS][sattr->index] |= (val & 0xF);
  386. }
  387. out = data->temp[HYSTERSIS][sattr->index];
  388. break;
  389. default:
  390. data->temp[sattr->nr][sattr->index] = temp2reg(data, val);
  391. /*
  392. * We maintain an extra 2 digits of precision for simplicity
  393. * - shift those back off before writing the value
  394. */
  395. out = (u8) (data->temp[sattr->nr][sattr->index] >> 2);
  396. }
  397. switch (sattr->nr) {
  398. case MIN:
  399. reg = TEMP_MIN_REG(sattr->index);
  400. break;
  401. case MAX:
  402. reg = TEMP_MAX_REG(sattr->index);
  403. break;
  404. case OFFSET:
  405. reg = TEMP_OFFSET_REG(sattr->index);
  406. break;
  407. case AUTOMIN:
  408. reg = TEMP_TMIN_REG(sattr->index);
  409. break;
  410. case THERM:
  411. reg = TEMP_THERM_REG(sattr->index);
  412. break;
  413. case HYSTERSIS:
  414. if (sattr->index != 2)
  415. reg = REG_REMOTE1_HYSTERSIS;
  416. else
  417. reg = REG_REMOTE2_HYSTERSIS;
  418. break;
  419. }
  420. i2c_smbus_write_byte_data(client, reg, out);
  421. mutex_unlock(&data->lock);
  422. return count;
  423. }
  424. /*
  425. * Table of autorange values - the user will write the value in millidegrees,
  426. * and we'll convert it
  427. */
  428. static const int autorange_table[] = {
  429. 2000, 2500, 3330, 4000, 5000, 6670, 8000,
  430. 10000, 13330, 16000, 20000, 26670, 32000, 40000,
  431. 53330, 80000
  432. };
  433. static ssize_t show_point2(struct device *dev, struct device_attribute *attr,
  434. char *buf)
  435. {
  436. struct adt7475_data *data = adt7475_update_device(dev);
  437. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  438. int out, val;
  439. mutex_lock(&data->lock);
  440. out = (data->range[sattr->index] >> 4) & 0x0F;
  441. val = reg2temp(data, data->temp[AUTOMIN][sattr->index]);
  442. mutex_unlock(&data->lock);
  443. return sprintf(buf, "%d\n", val + autorange_table[out]);
  444. }
  445. static ssize_t set_point2(struct device *dev, struct device_attribute *attr,
  446. const char *buf, size_t count)
  447. {
  448. struct i2c_client *client = to_i2c_client(dev);
  449. struct adt7475_data *data = i2c_get_clientdata(client);
  450. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  451. int temp;
  452. long val;
  453. if (kstrtol(buf, 10, &val))
  454. return -EINVAL;
  455. mutex_lock(&data->lock);
  456. /* Get a fresh copy of the needed registers */
  457. data->config5 = adt7475_read(REG_CONFIG5);
  458. data->temp[AUTOMIN][sattr->index] =
  459. adt7475_read(TEMP_TMIN_REG(sattr->index)) << 2;
  460. data->range[sattr->index] =
  461. adt7475_read(TEMP_TRANGE_REG(sattr->index));
  462. /*
  463. * The user will write an absolute value, so subtract the start point
  464. * to figure the range
  465. */
  466. temp = reg2temp(data, data->temp[AUTOMIN][sattr->index]);
  467. val = clamp_val(val, temp + autorange_table[0],
  468. temp + autorange_table[ARRAY_SIZE(autorange_table) - 1]);
  469. val -= temp;
  470. /* Find the nearest table entry to what the user wrote */
  471. val = find_nearest(val, autorange_table, ARRAY_SIZE(autorange_table));
  472. data->range[sattr->index] &= ~0xF0;
  473. data->range[sattr->index] |= val << 4;
  474. i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index),
  475. data->range[sattr->index]);
  476. mutex_unlock(&data->lock);
  477. return count;
  478. }
  479. static ssize_t show_tach(struct device *dev, struct device_attribute *attr,
  480. char *buf)
  481. {
  482. struct adt7475_data *data = adt7475_update_device(dev);
  483. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  484. int out;
  485. if (sattr->nr == ALARM)
  486. out = (data->alarms >> (sattr->index + 10)) & 1;
  487. else
  488. out = tach2rpm(data->tach[sattr->nr][sattr->index]);
  489. return sprintf(buf, "%d\n", out);
  490. }
  491. static ssize_t set_tach(struct device *dev, struct device_attribute *attr,
  492. const char *buf, size_t count)
  493. {
  494. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  495. struct i2c_client *client = to_i2c_client(dev);
  496. struct adt7475_data *data = i2c_get_clientdata(client);
  497. unsigned long val;
  498. if (kstrtoul(buf, 10, &val))
  499. return -EINVAL;
  500. mutex_lock(&data->lock);
  501. data->tach[MIN][sattr->index] = rpm2tach(val);
  502. adt7475_write_word(client, TACH_MIN_REG(sattr->index),
  503. data->tach[MIN][sattr->index]);
  504. mutex_unlock(&data->lock);
  505. return count;
  506. }
  507. static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
  508. char *buf)
  509. {
  510. struct adt7475_data *data = adt7475_update_device(dev);
  511. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  512. return sprintf(buf, "%d\n", data->pwm[sattr->nr][sattr->index]);
  513. }
  514. static ssize_t show_pwmchan(struct device *dev, struct device_attribute *attr,
  515. char *buf)
  516. {
  517. struct adt7475_data *data = adt7475_update_device(dev);
  518. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  519. return sprintf(buf, "%d\n", data->pwmchan[sattr->index]);
  520. }
  521. static ssize_t show_pwmctrl(struct device *dev, struct device_attribute *attr,
  522. char *buf)
  523. {
  524. struct adt7475_data *data = adt7475_update_device(dev);
  525. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  526. return sprintf(buf, "%d\n", data->pwmctl[sattr->index]);
  527. }
  528. static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
  529. const char *buf, size_t count)
  530. {
  531. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  532. struct i2c_client *client = to_i2c_client(dev);
  533. struct adt7475_data *data = i2c_get_clientdata(client);
  534. unsigned char reg = 0;
  535. long val;
  536. if (kstrtol(buf, 10, &val))
  537. return -EINVAL;
  538. mutex_lock(&data->lock);
  539. switch (sattr->nr) {
  540. case INPUT:
  541. /* Get a fresh value for CONTROL */
  542. data->pwm[CONTROL][sattr->index] =
  543. adt7475_read(PWM_CONFIG_REG(sattr->index));
  544. /*
  545. * If we are not in manual mode, then we shouldn't allow
  546. * the user to set the pwm speed
  547. */
  548. if (((data->pwm[CONTROL][sattr->index] >> 5) & 7) != 7) {
  549. mutex_unlock(&data->lock);
  550. return count;
  551. }
  552. reg = PWM_REG(sattr->index);
  553. break;
  554. case MIN:
  555. reg = PWM_MIN_REG(sattr->index);
  556. break;
  557. case MAX:
  558. reg = PWM_MAX_REG(sattr->index);
  559. break;
  560. }
  561. data->pwm[sattr->nr][sattr->index] = clamp_val(val, 0, 0xFF);
  562. i2c_smbus_write_byte_data(client, reg,
  563. data->pwm[sattr->nr][sattr->index]);
  564. mutex_unlock(&data->lock);
  565. return count;
  566. }
  567. /* Called by set_pwmctrl and set_pwmchan */
  568. static int hw_set_pwm(struct i2c_client *client, int index,
  569. unsigned int pwmctl, unsigned int pwmchan)
  570. {
  571. struct adt7475_data *data = i2c_get_clientdata(client);
  572. long val = 0;
  573. switch (pwmctl) {
  574. case 0:
  575. val = 0x03; /* Run at full speed */
  576. break;
  577. case 1:
  578. val = 0x07; /* Manual mode */
  579. break;
  580. case 2:
  581. switch (pwmchan) {
  582. case 1:
  583. /* Remote1 controls PWM */
  584. val = 0x00;
  585. break;
  586. case 2:
  587. /* local controls PWM */
  588. val = 0x01;
  589. break;
  590. case 4:
  591. /* remote2 controls PWM */
  592. val = 0x02;
  593. break;
  594. case 6:
  595. /* local/remote2 control PWM */
  596. val = 0x05;
  597. break;
  598. case 7:
  599. /* All three control PWM */
  600. val = 0x06;
  601. break;
  602. default:
  603. return -EINVAL;
  604. }
  605. break;
  606. default:
  607. return -EINVAL;
  608. }
  609. data->pwmctl[index] = pwmctl;
  610. data->pwmchan[index] = pwmchan;
  611. data->pwm[CONTROL][index] &= ~0xE0;
  612. data->pwm[CONTROL][index] |= (val & 7) << 5;
  613. i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
  614. data->pwm[CONTROL][index]);
  615. return 0;
  616. }
  617. static ssize_t set_pwmchan(struct device *dev, struct device_attribute *attr,
  618. const char *buf, size_t count)
  619. {
  620. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  621. struct i2c_client *client = to_i2c_client(dev);
  622. struct adt7475_data *data = i2c_get_clientdata(client);
  623. int r;
  624. long val;
  625. if (kstrtol(buf, 10, &val))
  626. return -EINVAL;
  627. mutex_lock(&data->lock);
  628. /* Read Modify Write PWM values */
  629. adt7475_read_pwm(client, sattr->index);
  630. r = hw_set_pwm(client, sattr->index, data->pwmctl[sattr->index], val);
  631. if (r)
  632. count = r;
  633. mutex_unlock(&data->lock);
  634. return count;
  635. }
  636. static ssize_t set_pwmctrl(struct device *dev, struct device_attribute *attr,
  637. const char *buf, size_t count)
  638. {
  639. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  640. struct i2c_client *client = to_i2c_client(dev);
  641. struct adt7475_data *data = i2c_get_clientdata(client);
  642. int r;
  643. long val;
  644. if (kstrtol(buf, 10, &val))
  645. return -EINVAL;
  646. mutex_lock(&data->lock);
  647. /* Read Modify Write PWM values */
  648. adt7475_read_pwm(client, sattr->index);
  649. r = hw_set_pwm(client, sattr->index, val, data->pwmchan[sattr->index]);
  650. if (r)
  651. count = r;
  652. mutex_unlock(&data->lock);
  653. return count;
  654. }
  655. /* List of frequencies for the PWM */
  656. static const int pwmfreq_table[] = {
  657. 11, 14, 22, 29, 35, 44, 58, 88
  658. };
  659. static ssize_t show_pwmfreq(struct device *dev, struct device_attribute *attr,
  660. char *buf)
  661. {
  662. struct adt7475_data *data = adt7475_update_device(dev);
  663. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  664. return sprintf(buf, "%d\n",
  665. pwmfreq_table[data->range[sattr->index] & 7]);
  666. }
  667. static ssize_t set_pwmfreq(struct device *dev, struct device_attribute *attr,
  668. const char *buf, size_t count)
  669. {
  670. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  671. struct i2c_client *client = to_i2c_client(dev);
  672. struct adt7475_data *data = i2c_get_clientdata(client);
  673. int out;
  674. long val;
  675. if (kstrtol(buf, 10, &val))
  676. return -EINVAL;
  677. out = find_nearest(val, pwmfreq_table, ARRAY_SIZE(pwmfreq_table));
  678. mutex_lock(&data->lock);
  679. data->range[sattr->index] =
  680. adt7475_read(TEMP_TRANGE_REG(sattr->index));
  681. data->range[sattr->index] &= ~7;
  682. data->range[sattr->index] |= out;
  683. i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index),
  684. data->range[sattr->index]);
  685. mutex_unlock(&data->lock);
  686. return count;
  687. }
  688. static ssize_t show_pwm_at_crit(struct device *dev,
  689. struct device_attribute *devattr, char *buf)
  690. {
  691. struct adt7475_data *data = adt7475_update_device(dev);
  692. return sprintf(buf, "%d\n", !!(data->config4 & CONFIG4_MAXDUTY));
  693. }
  694. static ssize_t set_pwm_at_crit(struct device *dev,
  695. struct device_attribute *devattr,
  696. const char *buf, size_t count)
  697. {
  698. struct i2c_client *client = to_i2c_client(dev);
  699. struct adt7475_data *data = i2c_get_clientdata(client);
  700. long val;
  701. if (kstrtol(buf, 10, &val))
  702. return -EINVAL;
  703. if (val != 0 && val != 1)
  704. return -EINVAL;
  705. mutex_lock(&data->lock);
  706. data->config4 = i2c_smbus_read_byte_data(client, REG_CONFIG4);
  707. if (val)
  708. data->config4 |= CONFIG4_MAXDUTY;
  709. else
  710. data->config4 &= ~CONFIG4_MAXDUTY;
  711. i2c_smbus_write_byte_data(client, REG_CONFIG4, data->config4);
  712. mutex_unlock(&data->lock);
  713. return count;
  714. }
  715. static ssize_t show_vrm(struct device *dev, struct device_attribute *devattr,
  716. char *buf)
  717. {
  718. struct adt7475_data *data = dev_get_drvdata(dev);
  719. return sprintf(buf, "%d\n", (int)data->vrm);
  720. }
  721. static ssize_t set_vrm(struct device *dev, struct device_attribute *devattr,
  722. const char *buf, size_t count)
  723. {
  724. struct adt7475_data *data = dev_get_drvdata(dev);
  725. long val;
  726. if (kstrtol(buf, 10, &val))
  727. return -EINVAL;
  728. if (val < 0 || val > 255)
  729. return -EINVAL;
  730. data->vrm = val;
  731. return count;
  732. }
  733. static ssize_t show_vid(struct device *dev, struct device_attribute *devattr,
  734. char *buf)
  735. {
  736. struct adt7475_data *data = adt7475_update_device(dev);
  737. return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
  738. }
  739. static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_voltage, NULL, INPUT, 0);
  740. static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_voltage,
  741. set_voltage, MAX, 0);
  742. static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_voltage,
  743. set_voltage, MIN, 0);
  744. static SENSOR_DEVICE_ATTR_2(in0_alarm, S_IRUGO, show_voltage, NULL, ALARM, 0);
  745. static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_voltage, NULL, INPUT, 1);
  746. static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_voltage,
  747. set_voltage, MAX, 1);
  748. static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_voltage,
  749. set_voltage, MIN, 1);
  750. static SENSOR_DEVICE_ATTR_2(in1_alarm, S_IRUGO, show_voltage, NULL, ALARM, 1);
  751. static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_voltage, NULL, INPUT, 2);
  752. static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_voltage,
  753. set_voltage, MAX, 2);
  754. static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_voltage,
  755. set_voltage, MIN, 2);
  756. static SENSOR_DEVICE_ATTR_2(in2_alarm, S_IRUGO, show_voltage, NULL, ALARM, 2);
  757. static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_voltage, NULL, INPUT, 3);
  758. static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_voltage,
  759. set_voltage, MAX, 3);
  760. static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_voltage,
  761. set_voltage, MIN, 3);
  762. static SENSOR_DEVICE_ATTR_2(in3_alarm, S_IRUGO, show_voltage, NULL, ALARM, 3);
  763. static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_voltage, NULL, INPUT, 4);
  764. static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_voltage,
  765. set_voltage, MAX, 4);
  766. static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_voltage,
  767. set_voltage, MIN, 4);
  768. static SENSOR_DEVICE_ATTR_2(in4_alarm, S_IRUGO, show_voltage, NULL, ALARM, 8);
  769. static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_voltage, NULL, INPUT, 5);
  770. static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGO | S_IWUSR, show_voltage,
  771. set_voltage, MAX, 5);
  772. static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGO | S_IWUSR, show_voltage,
  773. set_voltage, MIN, 5);
  774. static SENSOR_DEVICE_ATTR_2(in5_alarm, S_IRUGO, show_voltage, NULL, ALARM, 31);
  775. static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, INPUT, 0);
  776. static SENSOR_DEVICE_ATTR_2(temp1_alarm, S_IRUGO, show_temp, NULL, ALARM, 0);
  777. static SENSOR_DEVICE_ATTR_2(temp1_fault, S_IRUGO, show_temp, NULL, FAULT, 0);
  778. static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
  779. MAX, 0);
  780. static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
  781. MIN, 0);
  782. static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp,
  783. set_temp, OFFSET, 0);
  784. static SENSOR_DEVICE_ATTR_2(temp1_auto_point1_temp, S_IRUGO | S_IWUSR,
  785. show_temp, set_temp, AUTOMIN, 0);
  786. static SENSOR_DEVICE_ATTR_2(temp1_auto_point2_temp, S_IRUGO | S_IWUSR,
  787. show_point2, set_point2, 0, 0);
  788. static SENSOR_DEVICE_ATTR_2(temp1_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
  789. THERM, 0);
  790. static SENSOR_DEVICE_ATTR_2(temp1_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
  791. set_temp, HYSTERSIS, 0);
  792. static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, INPUT, 1);
  793. static SENSOR_DEVICE_ATTR_2(temp2_alarm, S_IRUGO, show_temp, NULL, ALARM, 1);
  794. static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
  795. MAX, 1);
  796. static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
  797. MIN, 1);
  798. static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp,
  799. set_temp, OFFSET, 1);
  800. static SENSOR_DEVICE_ATTR_2(temp2_auto_point1_temp, S_IRUGO | S_IWUSR,
  801. show_temp, set_temp, AUTOMIN, 1);
  802. static SENSOR_DEVICE_ATTR_2(temp2_auto_point2_temp, S_IRUGO | S_IWUSR,
  803. show_point2, set_point2, 0, 1);
  804. static SENSOR_DEVICE_ATTR_2(temp2_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
  805. THERM, 1);
  806. static SENSOR_DEVICE_ATTR_2(temp2_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
  807. set_temp, HYSTERSIS, 1);
  808. static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, INPUT, 2);
  809. static SENSOR_DEVICE_ATTR_2(temp3_alarm, S_IRUGO, show_temp, NULL, ALARM, 2);
  810. static SENSOR_DEVICE_ATTR_2(temp3_fault, S_IRUGO, show_temp, NULL, FAULT, 2);
  811. static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
  812. MAX, 2);
  813. static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
  814. MIN, 2);
  815. static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp,
  816. set_temp, OFFSET, 2);
  817. static SENSOR_DEVICE_ATTR_2(temp3_auto_point1_temp, S_IRUGO | S_IWUSR,
  818. show_temp, set_temp, AUTOMIN, 2);
  819. static SENSOR_DEVICE_ATTR_2(temp3_auto_point2_temp, S_IRUGO | S_IWUSR,
  820. show_point2, set_point2, 0, 2);
  821. static SENSOR_DEVICE_ATTR_2(temp3_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
  822. THERM, 2);
  823. static SENSOR_DEVICE_ATTR_2(temp3_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
  824. set_temp, HYSTERSIS, 2);
  825. static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_tach, NULL, INPUT, 0);
  826. static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
  827. MIN, 0);
  828. static SENSOR_DEVICE_ATTR_2(fan1_alarm, S_IRUGO, show_tach, NULL, ALARM, 0);
  829. static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_tach, NULL, INPUT, 1);
  830. static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
  831. MIN, 1);
  832. static SENSOR_DEVICE_ATTR_2(fan2_alarm, S_IRUGO, show_tach, NULL, ALARM, 1);
  833. static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_tach, NULL, INPUT, 2);
  834. static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
  835. MIN, 2);
  836. static SENSOR_DEVICE_ATTR_2(fan3_alarm, S_IRUGO, show_tach, NULL, ALARM, 2);
  837. static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_tach, NULL, INPUT, 3);
  838. static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
  839. MIN, 3);
  840. static SENSOR_DEVICE_ATTR_2(fan4_alarm, S_IRUGO, show_tach, NULL, ALARM, 3);
  841. static SENSOR_DEVICE_ATTR_2(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
  842. 0);
  843. static SENSOR_DEVICE_ATTR_2(pwm1_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
  844. set_pwmfreq, INPUT, 0);
  845. static SENSOR_DEVICE_ATTR_2(pwm1_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
  846. set_pwmctrl, INPUT, 0);
  847. static SENSOR_DEVICE_ATTR_2(pwm1_auto_channels_temp, S_IRUGO | S_IWUSR,
  848. show_pwmchan, set_pwmchan, INPUT, 0);
  849. static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
  850. set_pwm, MIN, 0);
  851. static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
  852. set_pwm, MAX, 0);
  853. static SENSOR_DEVICE_ATTR_2(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
  854. 1);
  855. static SENSOR_DEVICE_ATTR_2(pwm2_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
  856. set_pwmfreq, INPUT, 1);
  857. static SENSOR_DEVICE_ATTR_2(pwm2_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
  858. set_pwmctrl, INPUT, 1);
  859. static SENSOR_DEVICE_ATTR_2(pwm2_auto_channels_temp, S_IRUGO | S_IWUSR,
  860. show_pwmchan, set_pwmchan, INPUT, 1);
  861. static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
  862. set_pwm, MIN, 1);
  863. static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
  864. set_pwm, MAX, 1);
  865. static SENSOR_DEVICE_ATTR_2(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
  866. 2);
  867. static SENSOR_DEVICE_ATTR_2(pwm3_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
  868. set_pwmfreq, INPUT, 2);
  869. static SENSOR_DEVICE_ATTR_2(pwm3_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
  870. set_pwmctrl, INPUT, 2);
  871. static SENSOR_DEVICE_ATTR_2(pwm3_auto_channels_temp, S_IRUGO | S_IWUSR,
  872. show_pwmchan, set_pwmchan, INPUT, 2);
  873. static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
  874. set_pwm, MIN, 2);
  875. static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
  876. set_pwm, MAX, 2);
  877. /* Non-standard name, might need revisiting */
  878. static DEVICE_ATTR(pwm_use_point2_pwm_at_crit, S_IWUSR | S_IRUGO,
  879. show_pwm_at_crit, set_pwm_at_crit);
  880. static DEVICE_ATTR(vrm, S_IWUSR | S_IRUGO, show_vrm, set_vrm);
  881. static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
  882. static struct attribute *adt7475_attrs[] = {
  883. &sensor_dev_attr_in1_input.dev_attr.attr,
  884. &sensor_dev_attr_in1_max.dev_attr.attr,
  885. &sensor_dev_attr_in1_min.dev_attr.attr,
  886. &sensor_dev_attr_in1_alarm.dev_attr.attr,
  887. &sensor_dev_attr_in2_input.dev_attr.attr,
  888. &sensor_dev_attr_in2_max.dev_attr.attr,
  889. &sensor_dev_attr_in2_min.dev_attr.attr,
  890. &sensor_dev_attr_in2_alarm.dev_attr.attr,
  891. &sensor_dev_attr_temp1_input.dev_attr.attr,
  892. &sensor_dev_attr_temp1_alarm.dev_attr.attr,
  893. &sensor_dev_attr_temp1_fault.dev_attr.attr,
  894. &sensor_dev_attr_temp1_max.dev_attr.attr,
  895. &sensor_dev_attr_temp1_min.dev_attr.attr,
  896. &sensor_dev_attr_temp1_offset.dev_attr.attr,
  897. &sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
  898. &sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr,
  899. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  900. &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
  901. &sensor_dev_attr_temp2_input.dev_attr.attr,
  902. &sensor_dev_attr_temp2_alarm.dev_attr.attr,
  903. &sensor_dev_attr_temp2_max.dev_attr.attr,
  904. &sensor_dev_attr_temp2_min.dev_attr.attr,
  905. &sensor_dev_attr_temp2_offset.dev_attr.attr,
  906. &sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr,
  907. &sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr,
  908. &sensor_dev_attr_temp2_crit.dev_attr.attr,
  909. &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
  910. &sensor_dev_attr_temp3_input.dev_attr.attr,
  911. &sensor_dev_attr_temp3_fault.dev_attr.attr,
  912. &sensor_dev_attr_temp3_alarm.dev_attr.attr,
  913. &sensor_dev_attr_temp3_max.dev_attr.attr,
  914. &sensor_dev_attr_temp3_min.dev_attr.attr,
  915. &sensor_dev_attr_temp3_offset.dev_attr.attr,
  916. &sensor_dev_attr_temp3_auto_point1_temp.dev_attr.attr,
  917. &sensor_dev_attr_temp3_auto_point2_temp.dev_attr.attr,
  918. &sensor_dev_attr_temp3_crit.dev_attr.attr,
  919. &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
  920. &sensor_dev_attr_fan1_input.dev_attr.attr,
  921. &sensor_dev_attr_fan1_min.dev_attr.attr,
  922. &sensor_dev_attr_fan1_alarm.dev_attr.attr,
  923. &sensor_dev_attr_fan2_input.dev_attr.attr,
  924. &sensor_dev_attr_fan2_min.dev_attr.attr,
  925. &sensor_dev_attr_fan2_alarm.dev_attr.attr,
  926. &sensor_dev_attr_fan3_input.dev_attr.attr,
  927. &sensor_dev_attr_fan3_min.dev_attr.attr,
  928. &sensor_dev_attr_fan3_alarm.dev_attr.attr,
  929. &sensor_dev_attr_pwm1.dev_attr.attr,
  930. &sensor_dev_attr_pwm1_freq.dev_attr.attr,
  931. &sensor_dev_attr_pwm1_enable.dev_attr.attr,
  932. &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
  933. &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
  934. &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
  935. &sensor_dev_attr_pwm3.dev_attr.attr,
  936. &sensor_dev_attr_pwm3_freq.dev_attr.attr,
  937. &sensor_dev_attr_pwm3_enable.dev_attr.attr,
  938. &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
  939. &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
  940. &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
  941. &dev_attr_pwm_use_point2_pwm_at_crit.attr,
  942. NULL,
  943. };
  944. static struct attribute *fan4_attrs[] = {
  945. &sensor_dev_attr_fan4_input.dev_attr.attr,
  946. &sensor_dev_attr_fan4_min.dev_attr.attr,
  947. &sensor_dev_attr_fan4_alarm.dev_attr.attr,
  948. NULL
  949. };
  950. static struct attribute *pwm2_attrs[] = {
  951. &sensor_dev_attr_pwm2.dev_attr.attr,
  952. &sensor_dev_attr_pwm2_freq.dev_attr.attr,
  953. &sensor_dev_attr_pwm2_enable.dev_attr.attr,
  954. &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
  955. &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
  956. &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
  957. NULL
  958. };
  959. static struct attribute *in0_attrs[] = {
  960. &sensor_dev_attr_in0_input.dev_attr.attr,
  961. &sensor_dev_attr_in0_max.dev_attr.attr,
  962. &sensor_dev_attr_in0_min.dev_attr.attr,
  963. &sensor_dev_attr_in0_alarm.dev_attr.attr,
  964. NULL
  965. };
  966. static struct attribute *in3_attrs[] = {
  967. &sensor_dev_attr_in3_input.dev_attr.attr,
  968. &sensor_dev_attr_in3_max.dev_attr.attr,
  969. &sensor_dev_attr_in3_min.dev_attr.attr,
  970. &sensor_dev_attr_in3_alarm.dev_attr.attr,
  971. NULL
  972. };
  973. static struct attribute *in4_attrs[] = {
  974. &sensor_dev_attr_in4_input.dev_attr.attr,
  975. &sensor_dev_attr_in4_max.dev_attr.attr,
  976. &sensor_dev_attr_in4_min.dev_attr.attr,
  977. &sensor_dev_attr_in4_alarm.dev_attr.attr,
  978. NULL
  979. };
  980. static struct attribute *in5_attrs[] = {
  981. &sensor_dev_attr_in5_input.dev_attr.attr,
  982. &sensor_dev_attr_in5_max.dev_attr.attr,
  983. &sensor_dev_attr_in5_min.dev_attr.attr,
  984. &sensor_dev_attr_in5_alarm.dev_attr.attr,
  985. NULL
  986. };
  987. static struct attribute *vid_attrs[] = {
  988. &dev_attr_cpu0_vid.attr,
  989. &dev_attr_vrm.attr,
  990. NULL
  991. };
  992. static struct attribute_group adt7475_attr_group = { .attrs = adt7475_attrs };
  993. static struct attribute_group fan4_attr_group = { .attrs = fan4_attrs };
  994. static struct attribute_group pwm2_attr_group = { .attrs = pwm2_attrs };
  995. static struct attribute_group in0_attr_group = { .attrs = in0_attrs };
  996. static struct attribute_group in3_attr_group = { .attrs = in3_attrs };
  997. static struct attribute_group in4_attr_group = { .attrs = in4_attrs };
  998. static struct attribute_group in5_attr_group = { .attrs = in5_attrs };
  999. static struct attribute_group vid_attr_group = { .attrs = vid_attrs };
  1000. static int adt7475_detect(struct i2c_client *client,
  1001. struct i2c_board_info *info)
  1002. {
  1003. struct i2c_adapter *adapter = client->adapter;
  1004. int vendid, devid, devid2;
  1005. const char *name;
  1006. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  1007. return -ENODEV;
  1008. vendid = adt7475_read(REG_VENDID);
  1009. devid2 = adt7475_read(REG_DEVID2);
  1010. if (vendid != 0x41 || /* Analog Devices */
  1011. (devid2 & 0xf8) != 0x68)
  1012. return -ENODEV;
  1013. devid = adt7475_read(REG_DEVID);
  1014. if (devid == 0x73)
  1015. name = "adt7473";
  1016. else if (devid == 0x75 && client->addr == 0x2e)
  1017. name = "adt7475";
  1018. else if (devid == 0x76)
  1019. name = "adt7476";
  1020. else if ((devid2 & 0xfc) == 0x6c)
  1021. name = "adt7490";
  1022. else {
  1023. dev_dbg(&adapter->dev,
  1024. "Couldn't detect an ADT7473/75/76/90 part at "
  1025. "0x%02x\n", (unsigned int)client->addr);
  1026. return -ENODEV;
  1027. }
  1028. strlcpy(info->type, name, I2C_NAME_SIZE);
  1029. return 0;
  1030. }
  1031. static void adt7475_remove_files(struct i2c_client *client,
  1032. struct adt7475_data *data)
  1033. {
  1034. sysfs_remove_group(&client->dev.kobj, &adt7475_attr_group);
  1035. if (data->has_fan4)
  1036. sysfs_remove_group(&client->dev.kobj, &fan4_attr_group);
  1037. if (data->has_pwm2)
  1038. sysfs_remove_group(&client->dev.kobj, &pwm2_attr_group);
  1039. if (data->has_voltage & (1 << 0))
  1040. sysfs_remove_group(&client->dev.kobj, &in0_attr_group);
  1041. if (data->has_voltage & (1 << 3))
  1042. sysfs_remove_group(&client->dev.kobj, &in3_attr_group);
  1043. if (data->has_voltage & (1 << 4))
  1044. sysfs_remove_group(&client->dev.kobj, &in4_attr_group);
  1045. if (data->has_voltage & (1 << 5))
  1046. sysfs_remove_group(&client->dev.kobj, &in5_attr_group);
  1047. if (data->has_vid)
  1048. sysfs_remove_group(&client->dev.kobj, &vid_attr_group);
  1049. }
  1050. static int adt7475_probe(struct i2c_client *client,
  1051. const struct i2c_device_id *id)
  1052. {
  1053. static const char * const names[] = {
  1054. [adt7473] = "ADT7473",
  1055. [adt7475] = "ADT7475",
  1056. [adt7476] = "ADT7476",
  1057. [adt7490] = "ADT7490",
  1058. };
  1059. struct adt7475_data *data;
  1060. int i, ret = 0, revision;
  1061. u8 config2, config3;
  1062. data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
  1063. if (data == NULL)
  1064. return -ENOMEM;
  1065. mutex_init(&data->lock);
  1066. i2c_set_clientdata(client, data);
  1067. /* Initialize device-specific values */
  1068. switch (id->driver_data) {
  1069. case adt7476:
  1070. data->has_voltage = 0x0e; /* in1 to in3 */
  1071. revision = adt7475_read(REG_DEVID2) & 0x07;
  1072. break;
  1073. case adt7490:
  1074. data->has_voltage = 0x3e; /* in1 to in5 */
  1075. revision = adt7475_read(REG_DEVID2) & 0x03;
  1076. if (revision == 0x03)
  1077. revision += adt7475_read(REG_DEVREV2);
  1078. break;
  1079. default:
  1080. data->has_voltage = 0x06; /* in1, in2 */
  1081. revision = adt7475_read(REG_DEVID2) & 0x07;
  1082. }
  1083. config3 = adt7475_read(REG_CONFIG3);
  1084. /* Pin PWM2 may alternatively be used for ALERT output */
  1085. if (!(config3 & CONFIG3_SMBALERT))
  1086. data->has_pwm2 = 1;
  1087. /* Meaning of this bit is inverted for the ADT7473-1 */
  1088. if (id->driver_data == adt7473 && revision >= 1)
  1089. data->has_pwm2 = !data->has_pwm2;
  1090. data->config4 = adt7475_read(REG_CONFIG4);
  1091. /* Pin TACH4 may alternatively be used for THERM */
  1092. if ((data->config4 & CONFIG4_PINFUNC) == 0x0)
  1093. data->has_fan4 = 1;
  1094. /*
  1095. * THERM configuration is more complex on the ADT7476 and ADT7490,
  1096. * because 2 different pins (TACH4 and +2.5 Vin) can be used for
  1097. * this function
  1098. */
  1099. if (id->driver_data == adt7490) {
  1100. if ((data->config4 & CONFIG4_PINFUNC) == 0x1 &&
  1101. !(config3 & CONFIG3_THERM))
  1102. data->has_fan4 = 1;
  1103. }
  1104. if (id->driver_data == adt7476 || id->driver_data == adt7490) {
  1105. if (!(config3 & CONFIG3_THERM) ||
  1106. (data->config4 & CONFIG4_PINFUNC) == 0x1)
  1107. data->has_voltage |= (1 << 0); /* in0 */
  1108. }
  1109. /*
  1110. * On the ADT7476, the +12V input pin may instead be used as VID5,
  1111. * and VID pins may alternatively be used as GPIO
  1112. */
  1113. if (id->driver_data == adt7476) {
  1114. u8 vid = adt7475_read(REG_VID);
  1115. if (!(vid & VID_VIDSEL))
  1116. data->has_voltage |= (1 << 4); /* in4 */
  1117. data->has_vid = !(adt7475_read(REG_CONFIG5) & CONFIG5_VIDGPIO);
  1118. }
  1119. /* Voltage attenuators can be bypassed, globally or individually */
  1120. config2 = adt7475_read(REG_CONFIG2);
  1121. if (config2 & CONFIG2_ATTN) {
  1122. data->bypass_attn = (0x3 << 3) | 0x3;
  1123. } else {
  1124. data->bypass_attn = ((data->config4 & CONFIG4_ATTN_IN10) >> 4) |
  1125. ((data->config4 & CONFIG4_ATTN_IN43) >> 3);
  1126. }
  1127. data->bypass_attn &= data->has_voltage;
  1128. /*
  1129. * Call adt7475_read_pwm for all pwm's as this will reprogram any
  1130. * pwm's which are disabled to manual mode with 0% duty cycle
  1131. */
  1132. for (i = 0; i < ADT7475_PWM_COUNT; i++)
  1133. adt7475_read_pwm(client, i);
  1134. ret = sysfs_create_group(&client->dev.kobj, &adt7475_attr_group);
  1135. if (ret)
  1136. return ret;
  1137. /* Features that can be disabled individually */
  1138. if (data->has_fan4) {
  1139. ret = sysfs_create_group(&client->dev.kobj, &fan4_attr_group);
  1140. if (ret)
  1141. goto eremove;
  1142. }
  1143. if (data->has_pwm2) {
  1144. ret = sysfs_create_group(&client->dev.kobj, &pwm2_attr_group);
  1145. if (ret)
  1146. goto eremove;
  1147. }
  1148. if (data->has_voltage & (1 << 0)) {
  1149. ret = sysfs_create_group(&client->dev.kobj, &in0_attr_group);
  1150. if (ret)
  1151. goto eremove;
  1152. }
  1153. if (data->has_voltage & (1 << 3)) {
  1154. ret = sysfs_create_group(&client->dev.kobj, &in3_attr_group);
  1155. if (ret)
  1156. goto eremove;
  1157. }
  1158. if (data->has_voltage & (1 << 4)) {
  1159. ret = sysfs_create_group(&client->dev.kobj, &in4_attr_group);
  1160. if (ret)
  1161. goto eremove;
  1162. }
  1163. if (data->has_voltage & (1 << 5)) {
  1164. ret = sysfs_create_group(&client->dev.kobj, &in5_attr_group);
  1165. if (ret)
  1166. goto eremove;
  1167. }
  1168. if (data->has_vid) {
  1169. data->vrm = vid_which_vrm();
  1170. ret = sysfs_create_group(&client->dev.kobj, &vid_attr_group);
  1171. if (ret)
  1172. goto eremove;
  1173. }
  1174. data->hwmon_dev = hwmon_device_register(&client->dev);
  1175. if (IS_ERR(data->hwmon_dev)) {
  1176. ret = PTR_ERR(data->hwmon_dev);
  1177. goto eremove;
  1178. }
  1179. dev_info(&client->dev, "%s device, revision %d\n",
  1180. names[id->driver_data], revision);
  1181. if ((data->has_voltage & 0x11) || data->has_fan4 || data->has_pwm2)
  1182. dev_info(&client->dev, "Optional features:%s%s%s%s%s\n",
  1183. (data->has_voltage & (1 << 0)) ? " in0" : "",
  1184. (data->has_voltage & (1 << 4)) ? " in4" : "",
  1185. data->has_fan4 ? " fan4" : "",
  1186. data->has_pwm2 ? " pwm2" : "",
  1187. data->has_vid ? " vid" : "");
  1188. if (data->bypass_attn)
  1189. dev_info(&client->dev, "Bypassing attenuators on:%s%s%s%s\n",
  1190. (data->bypass_attn & (1 << 0)) ? " in0" : "",
  1191. (data->bypass_attn & (1 << 1)) ? " in1" : "",
  1192. (data->bypass_attn & (1 << 3)) ? " in3" : "",
  1193. (data->bypass_attn & (1 << 4)) ? " in4" : "");
  1194. return 0;
  1195. eremove:
  1196. adt7475_remove_files(client, data);
  1197. return ret;
  1198. }
  1199. static int adt7475_remove(struct i2c_client *client)
  1200. {
  1201. struct adt7475_data *data = i2c_get_clientdata(client);
  1202. hwmon_device_unregister(data->hwmon_dev);
  1203. adt7475_remove_files(client, data);
  1204. return 0;
  1205. }
  1206. static struct i2c_driver adt7475_driver = {
  1207. .class = I2C_CLASS_HWMON,
  1208. .driver = {
  1209. .name = "adt7475",
  1210. },
  1211. .probe = adt7475_probe,
  1212. .remove = adt7475_remove,
  1213. .id_table = adt7475_id,
  1214. .detect = adt7475_detect,
  1215. .address_list = normal_i2c,
  1216. };
  1217. static void adt7475_read_hystersis(struct i2c_client *client)
  1218. {
  1219. struct adt7475_data *data = i2c_get_clientdata(client);
  1220. data->temp[HYSTERSIS][0] = (u16) adt7475_read(REG_REMOTE1_HYSTERSIS);
  1221. data->temp[HYSTERSIS][1] = data->temp[HYSTERSIS][0];
  1222. data->temp[HYSTERSIS][2] = (u16) adt7475_read(REG_REMOTE2_HYSTERSIS);
  1223. }
  1224. static void adt7475_read_pwm(struct i2c_client *client, int index)
  1225. {
  1226. struct adt7475_data *data = i2c_get_clientdata(client);
  1227. unsigned int v;
  1228. data->pwm[CONTROL][index] = adt7475_read(PWM_CONFIG_REG(index));
  1229. /*
  1230. * Figure out the internal value for pwmctrl and pwmchan
  1231. * based on the current settings
  1232. */
  1233. v = (data->pwm[CONTROL][index] >> 5) & 7;
  1234. if (v == 3)
  1235. data->pwmctl[index] = 0;
  1236. else if (v == 7)
  1237. data->pwmctl[index] = 1;
  1238. else if (v == 4) {
  1239. /*
  1240. * The fan is disabled - we don't want to
  1241. * support that, so change to manual mode and
  1242. * set the duty cycle to 0 instead
  1243. */
  1244. data->pwm[INPUT][index] = 0;
  1245. data->pwm[CONTROL][index] &= ~0xE0;
  1246. data->pwm[CONTROL][index] |= (7 << 5);
  1247. i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
  1248. data->pwm[INPUT][index]);
  1249. i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
  1250. data->pwm[CONTROL][index]);
  1251. data->pwmctl[index] = 1;
  1252. } else {
  1253. data->pwmctl[index] = 2;
  1254. switch (v) {
  1255. case 0:
  1256. data->pwmchan[index] = 1;
  1257. break;
  1258. case 1:
  1259. data->pwmchan[index] = 2;
  1260. break;
  1261. case 2:
  1262. data->pwmchan[index] = 4;
  1263. break;
  1264. case 5:
  1265. data->pwmchan[index] = 6;
  1266. break;
  1267. case 6:
  1268. data->pwmchan[index] = 7;
  1269. break;
  1270. }
  1271. }
  1272. }
  1273. static struct adt7475_data *adt7475_update_device(struct device *dev)
  1274. {
  1275. struct i2c_client *client = to_i2c_client(dev);
  1276. struct adt7475_data *data = i2c_get_clientdata(client);
  1277. u16 ext;
  1278. int i;
  1279. mutex_lock(&data->lock);
  1280. /* Measurement values update every 2 seconds */
  1281. if (time_after(jiffies, data->measure_updated + HZ * 2) ||
  1282. !data->valid) {
  1283. data->alarms = adt7475_read(REG_STATUS2) << 8;
  1284. data->alarms |= adt7475_read(REG_STATUS1);
  1285. ext = (adt7475_read(REG_EXTEND2) << 8) |
  1286. adt7475_read(REG_EXTEND1);
  1287. for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) {
  1288. if (!(data->has_voltage & (1 << i)))
  1289. continue;
  1290. data->voltage[INPUT][i] =
  1291. (adt7475_read(VOLTAGE_REG(i)) << 2) |
  1292. ((ext >> (i * 2)) & 3);
  1293. }
  1294. for (i = 0; i < ADT7475_TEMP_COUNT; i++)
  1295. data->temp[INPUT][i] =
  1296. (adt7475_read(TEMP_REG(i)) << 2) |
  1297. ((ext >> ((i + 5) * 2)) & 3);
  1298. if (data->has_voltage & (1 << 5)) {
  1299. data->alarms |= adt7475_read(REG_STATUS4) << 24;
  1300. ext = adt7475_read(REG_EXTEND3);
  1301. data->voltage[INPUT][5] = adt7475_read(REG_VTT) << 2 |
  1302. ((ext >> 4) & 3);
  1303. }
  1304. for (i = 0; i < ADT7475_TACH_COUNT; i++) {
  1305. if (i == 3 && !data->has_fan4)
  1306. continue;
  1307. data->tach[INPUT][i] =
  1308. adt7475_read_word(client, TACH_REG(i));
  1309. }
  1310. /* Updated by hw when in auto mode */
  1311. for (i = 0; i < ADT7475_PWM_COUNT; i++) {
  1312. if (i == 1 && !data->has_pwm2)
  1313. continue;
  1314. data->pwm[INPUT][i] = adt7475_read(PWM_REG(i));
  1315. }
  1316. if (data->has_vid)
  1317. data->vid = adt7475_read(REG_VID) & 0x3f;
  1318. data->measure_updated = jiffies;
  1319. }
  1320. /* Limits and settings, should never change update every 60 seconds */
  1321. if (time_after(jiffies, data->limits_updated + HZ * 60) ||
  1322. !data->valid) {
  1323. data->config4 = adt7475_read(REG_CONFIG4);
  1324. data->config5 = adt7475_read(REG_CONFIG5);
  1325. for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) {
  1326. if (!(data->has_voltage & (1 << i)))
  1327. continue;
  1328. /* Adjust values so they match the input precision */
  1329. data->voltage[MIN][i] =
  1330. adt7475_read(VOLTAGE_MIN_REG(i)) << 2;
  1331. data->voltage[MAX][i] =
  1332. adt7475_read(VOLTAGE_MAX_REG(i)) << 2;
  1333. }
  1334. if (data->has_voltage & (1 << 5)) {
  1335. data->voltage[MIN][5] = adt7475_read(REG_VTT_MIN) << 2;
  1336. data->voltage[MAX][5] = adt7475_read(REG_VTT_MAX) << 2;
  1337. }
  1338. for (i = 0; i < ADT7475_TEMP_COUNT; i++) {
  1339. /* Adjust values so they match the input precision */
  1340. data->temp[MIN][i] =
  1341. adt7475_read(TEMP_MIN_REG(i)) << 2;
  1342. data->temp[MAX][i] =
  1343. adt7475_read(TEMP_MAX_REG(i)) << 2;
  1344. data->temp[AUTOMIN][i] =
  1345. adt7475_read(TEMP_TMIN_REG(i)) << 2;
  1346. data->temp[THERM][i] =
  1347. adt7475_read(TEMP_THERM_REG(i)) << 2;
  1348. data->temp[OFFSET][i] =
  1349. adt7475_read(TEMP_OFFSET_REG(i));
  1350. }
  1351. adt7475_read_hystersis(client);
  1352. for (i = 0; i < ADT7475_TACH_COUNT; i++) {
  1353. if (i == 3 && !data->has_fan4)
  1354. continue;
  1355. data->tach[MIN][i] =
  1356. adt7475_read_word(client, TACH_MIN_REG(i));
  1357. }
  1358. for (i = 0; i < ADT7475_PWM_COUNT; i++) {
  1359. if (i == 1 && !data->has_pwm2)
  1360. continue;
  1361. data->pwm[MAX][i] = adt7475_read(PWM_MAX_REG(i));
  1362. data->pwm[MIN][i] = adt7475_read(PWM_MIN_REG(i));
  1363. /* Set the channel and control information */
  1364. adt7475_read_pwm(client, i);
  1365. }
  1366. data->range[0] = adt7475_read(TEMP_TRANGE_REG(0));
  1367. data->range[1] = adt7475_read(TEMP_TRANGE_REG(1));
  1368. data->range[2] = adt7475_read(TEMP_TRANGE_REG(2));
  1369. data->limits_updated = jiffies;
  1370. data->valid = 1;
  1371. }
  1372. mutex_unlock(&data->lock);
  1373. return data;
  1374. }
  1375. module_i2c_driver(adt7475_driver);
  1376. MODULE_AUTHOR("Advanced Micro Devices, Inc");
  1377. MODULE_DESCRIPTION("adt7475 driver");
  1378. MODULE_LICENSE("GPL");