adt7470.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396
  1. /*
  2. * A hwmon driver for the Analog Devices ADT7470
  3. * Copyright (C) 2007 IBM
  4. *
  5. * Author: Darrick J. Wong <[email protected]>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  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. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  22. #include <linux/module.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/err.h>
  28. #include <linux/mutex.h>
  29. #include <linux/delay.h>
  30. #include <linux/log2.h>
  31. #include <linux/kthread.h>
  32. #include <linux/slab.h>
  33. #include <linux/util_macros.h>
  34. /* Addresses to scan */
  35. static const unsigned short normal_i2c[] = { 0x2C, 0x2E, 0x2F, I2C_CLIENT_END };
  36. /* ADT7470 registers */
  37. #define ADT7470_REG_BASE_ADDR 0x20
  38. #define ADT7470_REG_TEMP_BASE_ADDR 0x20
  39. #define ADT7470_REG_TEMP_MAX_ADDR 0x29
  40. #define ADT7470_REG_FAN_BASE_ADDR 0x2A
  41. #define ADT7470_REG_FAN_MAX_ADDR 0x31
  42. #define ADT7470_REG_PWM_BASE_ADDR 0x32
  43. #define ADT7470_REG_PWM_MAX_ADDR 0x35
  44. #define ADT7470_REG_PWM_MAX_BASE_ADDR 0x38
  45. #define ADT7470_REG_PWM_MAX_MAX_ADDR 0x3B
  46. #define ADT7470_REG_CFG 0x40
  47. #define ADT7470_FSPD_MASK 0x04
  48. #define ADT7470_REG_ALARM1 0x41
  49. #define ADT7470_R1T_ALARM 0x01
  50. #define ADT7470_R2T_ALARM 0x02
  51. #define ADT7470_R3T_ALARM 0x04
  52. #define ADT7470_R4T_ALARM 0x08
  53. #define ADT7470_R5T_ALARM 0x10
  54. #define ADT7470_R6T_ALARM 0x20
  55. #define ADT7470_R7T_ALARM 0x40
  56. #define ADT7470_OOL_ALARM 0x80
  57. #define ADT7470_REG_ALARM2 0x42
  58. #define ADT7470_R8T_ALARM 0x01
  59. #define ADT7470_R9T_ALARM 0x02
  60. #define ADT7470_R10T_ALARM 0x04
  61. #define ADT7470_FAN1_ALARM 0x10
  62. #define ADT7470_FAN2_ALARM 0x20
  63. #define ADT7470_FAN3_ALARM 0x40
  64. #define ADT7470_FAN4_ALARM 0x80
  65. #define ADT7470_REG_TEMP_LIMITS_BASE_ADDR 0x44
  66. #define ADT7470_REG_TEMP_LIMITS_MAX_ADDR 0x57
  67. #define ADT7470_REG_FAN_MIN_BASE_ADDR 0x58
  68. #define ADT7470_REG_FAN_MIN_MAX_ADDR 0x5F
  69. #define ADT7470_REG_FAN_MAX_BASE_ADDR 0x60
  70. #define ADT7470_REG_FAN_MAX_MAX_ADDR 0x67
  71. #define ADT7470_REG_PWM_CFG_BASE_ADDR 0x68
  72. #define ADT7470_REG_PWM12_CFG 0x68
  73. #define ADT7470_PWM2_AUTO_MASK 0x40
  74. #define ADT7470_PWM1_AUTO_MASK 0x80
  75. #define ADT7470_PWM_AUTO_MASK 0xC0
  76. #define ADT7470_REG_PWM34_CFG 0x69
  77. #define ADT7470_PWM3_AUTO_MASK 0x40
  78. #define ADT7470_PWM4_AUTO_MASK 0x80
  79. #define ADT7470_REG_PWM_MIN_BASE_ADDR 0x6A
  80. #define ADT7470_REG_PWM_MIN_MAX_ADDR 0x6D
  81. #define ADT7470_REG_PWM_TEMP_MIN_BASE_ADDR 0x6E
  82. #define ADT7470_REG_PWM_TEMP_MIN_MAX_ADDR 0x71
  83. #define ADT7470_REG_CFG_2 0x74
  84. #define ADT7470_REG_ACOUSTICS12 0x75
  85. #define ADT7470_REG_ACOUSTICS34 0x76
  86. #define ADT7470_REG_DEVICE 0x3D
  87. #define ADT7470_REG_VENDOR 0x3E
  88. #define ADT7470_REG_REVISION 0x3F
  89. #define ADT7470_REG_ALARM1_MASK 0x72
  90. #define ADT7470_REG_ALARM2_MASK 0x73
  91. #define ADT7470_REG_PWM_AUTO_TEMP_BASE_ADDR 0x7C
  92. #define ADT7470_REG_PWM_AUTO_TEMP_MAX_ADDR 0x7D
  93. #define ADT7470_REG_MAX_ADDR 0x81
  94. #define ADT7470_TEMP_COUNT 10
  95. #define ADT7470_TEMP_REG(x) (ADT7470_REG_TEMP_BASE_ADDR + (x))
  96. #define ADT7470_TEMP_MIN_REG(x) (ADT7470_REG_TEMP_LIMITS_BASE_ADDR + ((x) * 2))
  97. #define ADT7470_TEMP_MAX_REG(x) (ADT7470_REG_TEMP_LIMITS_BASE_ADDR + \
  98. ((x) * 2) + 1)
  99. #define ADT7470_FAN_COUNT 4
  100. #define ADT7470_REG_FAN(x) (ADT7470_REG_FAN_BASE_ADDR + ((x) * 2))
  101. #define ADT7470_REG_FAN_MIN(x) (ADT7470_REG_FAN_MIN_BASE_ADDR + ((x) * 2))
  102. #define ADT7470_REG_FAN_MAX(x) (ADT7470_REG_FAN_MAX_BASE_ADDR + ((x) * 2))
  103. #define ADT7470_PWM_COUNT 4
  104. #define ADT7470_REG_PWM(x) (ADT7470_REG_PWM_BASE_ADDR + (x))
  105. #define ADT7470_REG_PWM_MAX(x) (ADT7470_REG_PWM_MAX_BASE_ADDR + (x))
  106. #define ADT7470_REG_PWM_MIN(x) (ADT7470_REG_PWM_MIN_BASE_ADDR + (x))
  107. #define ADT7470_REG_PWM_TMIN(x) (ADT7470_REG_PWM_TEMP_MIN_BASE_ADDR + (x))
  108. #define ADT7470_REG_PWM_CFG(x) (ADT7470_REG_PWM_CFG_BASE_ADDR + ((x) / 2))
  109. #define ADT7470_REG_PWM_AUTO_TEMP(x) (ADT7470_REG_PWM_AUTO_TEMP_BASE_ADDR + \
  110. ((x) / 2))
  111. #define ALARM2(x) ((x) << 8)
  112. #define ADT7470_VENDOR 0x41
  113. #define ADT7470_DEVICE 0x70
  114. /* datasheet only mentions a revision 2 */
  115. #define ADT7470_REVISION 0x02
  116. /* "all temps" according to hwmon sysfs interface spec */
  117. #define ADT7470_PWM_ALL_TEMPS 0x3FF
  118. /* How often do we reread sensors values? (In jiffies) */
  119. #define SENSOR_REFRESH_INTERVAL (5 * HZ)
  120. /* How often do we reread sensor limit values? (In jiffies) */
  121. #define LIMIT_REFRESH_INTERVAL (60 * HZ)
  122. /* Wait at least 200ms per sensor for 10 sensors */
  123. #define TEMP_COLLECTION_TIME 2000
  124. /* auto update thing won't fire more than every 2s */
  125. #define AUTO_UPDATE_INTERVAL 2000
  126. /* datasheet says to divide this number by the fan reading to get fan rpm */
  127. #define FAN_PERIOD_TO_RPM(x) ((90000 * 60) / (x))
  128. #define FAN_RPM_TO_PERIOD FAN_PERIOD_TO_RPM
  129. #define FAN_PERIOD_INVALID 65535
  130. #define FAN_DATA_VALID(x) ((x) && (x) != FAN_PERIOD_INVALID)
  131. /* Config registers 1 and 2 include fields for selecting the PWM frequency */
  132. #define ADT7470_CFG_LF 0x40
  133. #define ADT7470_FREQ_MASK 0x70
  134. #define ADT7470_FREQ_SHIFT 4
  135. struct adt7470_data {
  136. struct i2c_client *client;
  137. struct mutex lock;
  138. char sensors_valid;
  139. char limits_valid;
  140. unsigned long sensors_last_updated; /* In jiffies */
  141. unsigned long limits_last_updated; /* In jiffies */
  142. int num_temp_sensors; /* -1 = probe */
  143. int temperatures_probed;
  144. s8 temp[ADT7470_TEMP_COUNT];
  145. s8 temp_min[ADT7470_TEMP_COUNT];
  146. s8 temp_max[ADT7470_TEMP_COUNT];
  147. u16 fan[ADT7470_FAN_COUNT];
  148. u16 fan_min[ADT7470_FAN_COUNT];
  149. u16 fan_max[ADT7470_FAN_COUNT];
  150. u16 alarm;
  151. u16 alarms_mask;
  152. u8 force_pwm_max;
  153. u8 pwm[ADT7470_PWM_COUNT];
  154. u8 pwm_max[ADT7470_PWM_COUNT];
  155. u8 pwm_automatic[ADT7470_PWM_COUNT];
  156. u8 pwm_min[ADT7470_PWM_COUNT];
  157. s8 pwm_tmin[ADT7470_PWM_COUNT];
  158. u8 pwm_auto_temp[ADT7470_PWM_COUNT];
  159. struct task_struct *auto_update;
  160. unsigned int auto_update_interval;
  161. };
  162. /*
  163. * 16-bit registers on the ADT7470 are low-byte first. The data sheet says
  164. * that the low byte must be read before the high byte.
  165. */
  166. static inline int adt7470_read_word_data(struct i2c_client *client, u8 reg)
  167. {
  168. u16 foo;
  169. foo = i2c_smbus_read_byte_data(client, reg);
  170. foo |= ((u16)i2c_smbus_read_byte_data(client, reg + 1) << 8);
  171. return foo;
  172. }
  173. static inline int adt7470_write_word_data(struct i2c_client *client, u8 reg,
  174. u16 value)
  175. {
  176. return i2c_smbus_write_byte_data(client, reg, value & 0xFF)
  177. || i2c_smbus_write_byte_data(client, reg + 1, value >> 8);
  178. }
  179. /* Probe for temperature sensors. Assumes lock is held */
  180. static int adt7470_read_temperatures(struct i2c_client *client,
  181. struct adt7470_data *data)
  182. {
  183. unsigned long res;
  184. int i;
  185. u8 cfg, pwm[4], pwm_cfg[2];
  186. /* save pwm[1-4] config register */
  187. pwm_cfg[0] = i2c_smbus_read_byte_data(client, ADT7470_REG_PWM_CFG(0));
  188. pwm_cfg[1] = i2c_smbus_read_byte_data(client, ADT7470_REG_PWM_CFG(2));
  189. /* set manual pwm to whatever it is set to now */
  190. for (i = 0; i < ADT7470_FAN_COUNT; i++)
  191. pwm[i] = i2c_smbus_read_byte_data(client, ADT7470_REG_PWM(i));
  192. /* put pwm in manual mode */
  193. i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_CFG(0),
  194. pwm_cfg[0] & ~(ADT7470_PWM_AUTO_MASK));
  195. i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_CFG(2),
  196. pwm_cfg[1] & ~(ADT7470_PWM_AUTO_MASK));
  197. /* write pwm control to whatever it was */
  198. for (i = 0; i < ADT7470_FAN_COUNT; i++)
  199. i2c_smbus_write_byte_data(client, ADT7470_REG_PWM(i), pwm[i]);
  200. /* start reading temperature sensors */
  201. cfg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
  202. cfg |= 0x80;
  203. i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, cfg);
  204. /* Delay is 200ms * number of temp sensors. */
  205. res = msleep_interruptible((data->num_temp_sensors >= 0 ?
  206. data->num_temp_sensors * 200 :
  207. TEMP_COLLECTION_TIME));
  208. /* done reading temperature sensors */
  209. cfg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
  210. cfg &= ~0x80;
  211. i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, cfg);
  212. /* restore pwm[1-4] config registers */
  213. i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_CFG(0), pwm_cfg[0]);
  214. i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_CFG(2), pwm_cfg[1]);
  215. if (res) {
  216. pr_err("ha ha, interrupted\n");
  217. return -EAGAIN;
  218. }
  219. /* Only count fans if we have to */
  220. if (data->num_temp_sensors >= 0)
  221. return 0;
  222. for (i = 0; i < ADT7470_TEMP_COUNT; i++) {
  223. data->temp[i] = i2c_smbus_read_byte_data(client,
  224. ADT7470_TEMP_REG(i));
  225. if (data->temp[i])
  226. data->num_temp_sensors = i + 1;
  227. }
  228. data->temperatures_probed = 1;
  229. return 0;
  230. }
  231. static int adt7470_update_thread(void *p)
  232. {
  233. struct i2c_client *client = p;
  234. struct adt7470_data *data = i2c_get_clientdata(client);
  235. while (!kthread_should_stop()) {
  236. mutex_lock(&data->lock);
  237. adt7470_read_temperatures(client, data);
  238. mutex_unlock(&data->lock);
  239. set_current_state(TASK_INTERRUPTIBLE);
  240. if (kthread_should_stop())
  241. break;
  242. schedule_timeout(msecs_to_jiffies(data->auto_update_interval));
  243. }
  244. return 0;
  245. }
  246. static struct adt7470_data *adt7470_update_device(struct device *dev)
  247. {
  248. struct adt7470_data *data = dev_get_drvdata(dev);
  249. struct i2c_client *client = data->client;
  250. unsigned long local_jiffies = jiffies;
  251. u8 cfg;
  252. int i;
  253. int need_sensors = 1;
  254. int need_limits = 1;
  255. /*
  256. * Figure out if we need to update the shadow registers.
  257. * Lockless means that we may occasionally report out of
  258. * date data.
  259. */
  260. if (time_before(local_jiffies, data->sensors_last_updated +
  261. SENSOR_REFRESH_INTERVAL) &&
  262. data->sensors_valid)
  263. need_sensors = 0;
  264. if (time_before(local_jiffies, data->limits_last_updated +
  265. LIMIT_REFRESH_INTERVAL) &&
  266. data->limits_valid)
  267. need_limits = 0;
  268. if (!need_sensors && !need_limits)
  269. return data;
  270. mutex_lock(&data->lock);
  271. if (!need_sensors)
  272. goto no_sensor_update;
  273. if (!data->temperatures_probed)
  274. adt7470_read_temperatures(client, data);
  275. else
  276. for (i = 0; i < ADT7470_TEMP_COUNT; i++)
  277. data->temp[i] = i2c_smbus_read_byte_data(client,
  278. ADT7470_TEMP_REG(i));
  279. for (i = 0; i < ADT7470_FAN_COUNT; i++)
  280. data->fan[i] = adt7470_read_word_data(client,
  281. ADT7470_REG_FAN(i));
  282. for (i = 0; i < ADT7470_PWM_COUNT; i++) {
  283. int reg;
  284. int reg_mask;
  285. data->pwm[i] = i2c_smbus_read_byte_data(client,
  286. ADT7470_REG_PWM(i));
  287. if (i % 2)
  288. reg_mask = ADT7470_PWM2_AUTO_MASK;
  289. else
  290. reg_mask = ADT7470_PWM1_AUTO_MASK;
  291. reg = ADT7470_REG_PWM_CFG(i);
  292. if (i2c_smbus_read_byte_data(client, reg) & reg_mask)
  293. data->pwm_automatic[i] = 1;
  294. else
  295. data->pwm_automatic[i] = 0;
  296. reg = ADT7470_REG_PWM_AUTO_TEMP(i);
  297. cfg = i2c_smbus_read_byte_data(client, reg);
  298. if (!(i % 2))
  299. data->pwm_auto_temp[i] = cfg >> 4;
  300. else
  301. data->pwm_auto_temp[i] = cfg & 0xF;
  302. }
  303. if (i2c_smbus_read_byte_data(client, ADT7470_REG_CFG) &
  304. ADT7470_FSPD_MASK)
  305. data->force_pwm_max = 1;
  306. else
  307. data->force_pwm_max = 0;
  308. data->alarm = i2c_smbus_read_byte_data(client, ADT7470_REG_ALARM1);
  309. if (data->alarm & ADT7470_OOL_ALARM)
  310. data->alarm |= ALARM2(i2c_smbus_read_byte_data(client,
  311. ADT7470_REG_ALARM2));
  312. data->alarms_mask = adt7470_read_word_data(client,
  313. ADT7470_REG_ALARM1_MASK);
  314. data->sensors_last_updated = local_jiffies;
  315. data->sensors_valid = 1;
  316. no_sensor_update:
  317. if (!need_limits)
  318. goto out;
  319. for (i = 0; i < ADT7470_TEMP_COUNT; i++) {
  320. data->temp_min[i] = i2c_smbus_read_byte_data(client,
  321. ADT7470_TEMP_MIN_REG(i));
  322. data->temp_max[i] = i2c_smbus_read_byte_data(client,
  323. ADT7470_TEMP_MAX_REG(i));
  324. }
  325. for (i = 0; i < ADT7470_FAN_COUNT; i++) {
  326. data->fan_min[i] = adt7470_read_word_data(client,
  327. ADT7470_REG_FAN_MIN(i));
  328. data->fan_max[i] = adt7470_read_word_data(client,
  329. ADT7470_REG_FAN_MAX(i));
  330. }
  331. for (i = 0; i < ADT7470_PWM_COUNT; i++) {
  332. data->pwm_max[i] = i2c_smbus_read_byte_data(client,
  333. ADT7470_REG_PWM_MAX(i));
  334. data->pwm_min[i] = i2c_smbus_read_byte_data(client,
  335. ADT7470_REG_PWM_MIN(i));
  336. data->pwm_tmin[i] = i2c_smbus_read_byte_data(client,
  337. ADT7470_REG_PWM_TMIN(i));
  338. }
  339. data->limits_last_updated = local_jiffies;
  340. data->limits_valid = 1;
  341. out:
  342. mutex_unlock(&data->lock);
  343. return data;
  344. }
  345. static ssize_t show_auto_update_interval(struct device *dev,
  346. struct device_attribute *devattr,
  347. char *buf)
  348. {
  349. struct adt7470_data *data = adt7470_update_device(dev);
  350. return sprintf(buf, "%d\n", data->auto_update_interval);
  351. }
  352. static ssize_t set_auto_update_interval(struct device *dev,
  353. struct device_attribute *devattr,
  354. const char *buf,
  355. size_t count)
  356. {
  357. struct adt7470_data *data = dev_get_drvdata(dev);
  358. long temp;
  359. if (kstrtol(buf, 10, &temp))
  360. return -EINVAL;
  361. temp = clamp_val(temp, 0, 60000);
  362. mutex_lock(&data->lock);
  363. data->auto_update_interval = temp;
  364. mutex_unlock(&data->lock);
  365. return count;
  366. }
  367. static ssize_t show_num_temp_sensors(struct device *dev,
  368. struct device_attribute *devattr,
  369. char *buf)
  370. {
  371. struct adt7470_data *data = adt7470_update_device(dev);
  372. return sprintf(buf, "%d\n", data->num_temp_sensors);
  373. }
  374. static ssize_t set_num_temp_sensors(struct device *dev,
  375. struct device_attribute *devattr,
  376. const char *buf,
  377. size_t count)
  378. {
  379. struct adt7470_data *data = dev_get_drvdata(dev);
  380. long temp;
  381. if (kstrtol(buf, 10, &temp))
  382. return -EINVAL;
  383. temp = clamp_val(temp, -1, 10);
  384. mutex_lock(&data->lock);
  385. data->num_temp_sensors = temp;
  386. if (temp < 0)
  387. data->temperatures_probed = 0;
  388. mutex_unlock(&data->lock);
  389. return count;
  390. }
  391. static ssize_t show_temp_min(struct device *dev,
  392. struct device_attribute *devattr,
  393. char *buf)
  394. {
  395. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  396. struct adt7470_data *data = adt7470_update_device(dev);
  397. return sprintf(buf, "%d\n", 1000 * data->temp_min[attr->index]);
  398. }
  399. static ssize_t set_temp_min(struct device *dev,
  400. struct device_attribute *devattr,
  401. const char *buf,
  402. size_t count)
  403. {
  404. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  405. struct adt7470_data *data = dev_get_drvdata(dev);
  406. struct i2c_client *client = data->client;
  407. long temp;
  408. if (kstrtol(buf, 10, &temp))
  409. return -EINVAL;
  410. temp = DIV_ROUND_CLOSEST(temp, 1000);
  411. temp = clamp_val(temp, -128, 127);
  412. mutex_lock(&data->lock);
  413. data->temp_min[attr->index] = temp;
  414. i2c_smbus_write_byte_data(client, ADT7470_TEMP_MIN_REG(attr->index),
  415. temp);
  416. mutex_unlock(&data->lock);
  417. return count;
  418. }
  419. static ssize_t show_temp_max(struct device *dev,
  420. struct device_attribute *devattr,
  421. char *buf)
  422. {
  423. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  424. struct adt7470_data *data = adt7470_update_device(dev);
  425. return sprintf(buf, "%d\n", 1000 * data->temp_max[attr->index]);
  426. }
  427. static ssize_t set_temp_max(struct device *dev,
  428. struct device_attribute *devattr,
  429. const char *buf,
  430. size_t count)
  431. {
  432. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  433. struct adt7470_data *data = dev_get_drvdata(dev);
  434. struct i2c_client *client = data->client;
  435. long temp;
  436. if (kstrtol(buf, 10, &temp))
  437. return -EINVAL;
  438. temp = DIV_ROUND_CLOSEST(temp, 1000);
  439. temp = clamp_val(temp, -128, 127);
  440. mutex_lock(&data->lock);
  441. data->temp_max[attr->index] = temp;
  442. i2c_smbus_write_byte_data(client, ADT7470_TEMP_MAX_REG(attr->index),
  443. temp);
  444. mutex_unlock(&data->lock);
  445. return count;
  446. }
  447. static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
  448. char *buf)
  449. {
  450. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  451. struct adt7470_data *data = adt7470_update_device(dev);
  452. return sprintf(buf, "%d\n", 1000 * data->temp[attr->index]);
  453. }
  454. static ssize_t show_alarm_mask(struct device *dev,
  455. struct device_attribute *devattr,
  456. char *buf)
  457. {
  458. struct adt7470_data *data = adt7470_update_device(dev);
  459. return sprintf(buf, "%x\n", data->alarms_mask);
  460. }
  461. static ssize_t set_alarm_mask(struct device *dev,
  462. struct device_attribute *devattr,
  463. const char *buf,
  464. size_t count)
  465. {
  466. struct adt7470_data *data = dev_get_drvdata(dev);
  467. long mask;
  468. if (kstrtoul(buf, 0, &mask))
  469. return -EINVAL;
  470. if (mask & ~0xffff)
  471. return -EINVAL;
  472. mutex_lock(&data->lock);
  473. data->alarms_mask = mask;
  474. adt7470_write_word_data(data->client, ADT7470_REG_ALARM1_MASK, mask);
  475. mutex_unlock(&data->lock);
  476. return count;
  477. }
  478. static ssize_t show_fan_max(struct device *dev,
  479. struct device_attribute *devattr,
  480. char *buf)
  481. {
  482. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  483. struct adt7470_data *data = adt7470_update_device(dev);
  484. if (FAN_DATA_VALID(data->fan_max[attr->index]))
  485. return sprintf(buf, "%d\n",
  486. FAN_PERIOD_TO_RPM(data->fan_max[attr->index]));
  487. else
  488. return sprintf(buf, "0\n");
  489. }
  490. static ssize_t set_fan_max(struct device *dev,
  491. struct device_attribute *devattr,
  492. const char *buf, size_t count)
  493. {
  494. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  495. struct adt7470_data *data = dev_get_drvdata(dev);
  496. struct i2c_client *client = data->client;
  497. long temp;
  498. if (kstrtol(buf, 10, &temp) || !temp)
  499. return -EINVAL;
  500. temp = FAN_RPM_TO_PERIOD(temp);
  501. temp = clamp_val(temp, 1, 65534);
  502. mutex_lock(&data->lock);
  503. data->fan_max[attr->index] = temp;
  504. adt7470_write_word_data(client, ADT7470_REG_FAN_MAX(attr->index), temp);
  505. mutex_unlock(&data->lock);
  506. return count;
  507. }
  508. static ssize_t show_fan_min(struct device *dev,
  509. struct device_attribute *devattr,
  510. char *buf)
  511. {
  512. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  513. struct adt7470_data *data = adt7470_update_device(dev);
  514. if (FAN_DATA_VALID(data->fan_min[attr->index]))
  515. return sprintf(buf, "%d\n",
  516. FAN_PERIOD_TO_RPM(data->fan_min[attr->index]));
  517. else
  518. return sprintf(buf, "0\n");
  519. }
  520. static ssize_t set_fan_min(struct device *dev,
  521. struct device_attribute *devattr,
  522. const char *buf, size_t count)
  523. {
  524. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  525. struct adt7470_data *data = dev_get_drvdata(dev);
  526. struct i2c_client *client = data->client;
  527. long temp;
  528. if (kstrtol(buf, 10, &temp) || !temp)
  529. return -EINVAL;
  530. temp = FAN_RPM_TO_PERIOD(temp);
  531. temp = clamp_val(temp, 1, 65534);
  532. mutex_lock(&data->lock);
  533. data->fan_min[attr->index] = temp;
  534. adt7470_write_word_data(client, ADT7470_REG_FAN_MIN(attr->index), temp);
  535. mutex_unlock(&data->lock);
  536. return count;
  537. }
  538. static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
  539. char *buf)
  540. {
  541. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  542. struct adt7470_data *data = adt7470_update_device(dev);
  543. if (FAN_DATA_VALID(data->fan[attr->index]))
  544. return sprintf(buf, "%d\n",
  545. FAN_PERIOD_TO_RPM(data->fan[attr->index]));
  546. else
  547. return sprintf(buf, "0\n");
  548. }
  549. static ssize_t show_force_pwm_max(struct device *dev,
  550. struct device_attribute *devattr,
  551. char *buf)
  552. {
  553. struct adt7470_data *data = adt7470_update_device(dev);
  554. return sprintf(buf, "%d\n", data->force_pwm_max);
  555. }
  556. static ssize_t set_force_pwm_max(struct device *dev,
  557. struct device_attribute *devattr,
  558. const char *buf,
  559. size_t count)
  560. {
  561. struct adt7470_data *data = dev_get_drvdata(dev);
  562. struct i2c_client *client = data->client;
  563. long temp;
  564. u8 reg;
  565. if (kstrtol(buf, 10, &temp))
  566. return -EINVAL;
  567. mutex_lock(&data->lock);
  568. data->force_pwm_max = temp;
  569. reg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
  570. if (temp)
  571. reg |= ADT7470_FSPD_MASK;
  572. else
  573. reg &= ~ADT7470_FSPD_MASK;
  574. i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, reg);
  575. mutex_unlock(&data->lock);
  576. return count;
  577. }
  578. static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr,
  579. char *buf)
  580. {
  581. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  582. struct adt7470_data *data = adt7470_update_device(dev);
  583. return sprintf(buf, "%d\n", data->pwm[attr->index]);
  584. }
  585. static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr,
  586. const char *buf, size_t count)
  587. {
  588. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  589. struct adt7470_data *data = dev_get_drvdata(dev);
  590. struct i2c_client *client = data->client;
  591. long temp;
  592. if (kstrtol(buf, 10, &temp))
  593. return -EINVAL;
  594. temp = clamp_val(temp, 0, 255);
  595. mutex_lock(&data->lock);
  596. data->pwm[attr->index] = temp;
  597. i2c_smbus_write_byte_data(client, ADT7470_REG_PWM(attr->index), temp);
  598. mutex_unlock(&data->lock);
  599. return count;
  600. }
  601. /* These are the valid PWM frequencies to the nearest Hz */
  602. static const int adt7470_freq_map[] = {
  603. 11, 15, 22, 29, 35, 44, 59, 88, 1400, 22500
  604. };
  605. static ssize_t show_pwm_freq(struct device *dev,
  606. struct device_attribute *devattr, char *buf)
  607. {
  608. struct adt7470_data *data = adt7470_update_device(dev);
  609. unsigned char cfg_reg_1;
  610. unsigned char cfg_reg_2;
  611. int index;
  612. mutex_lock(&data->lock);
  613. cfg_reg_1 = i2c_smbus_read_byte_data(data->client, ADT7470_REG_CFG);
  614. cfg_reg_2 = i2c_smbus_read_byte_data(data->client, ADT7470_REG_CFG_2);
  615. mutex_unlock(&data->lock);
  616. index = (cfg_reg_2 & ADT7470_FREQ_MASK) >> ADT7470_FREQ_SHIFT;
  617. if (!(cfg_reg_1 & ADT7470_CFG_LF))
  618. index += 8;
  619. if (index >= ARRAY_SIZE(adt7470_freq_map))
  620. index = ARRAY_SIZE(adt7470_freq_map) - 1;
  621. return scnprintf(buf, PAGE_SIZE, "%d\n", adt7470_freq_map[index]);
  622. }
  623. static ssize_t set_pwm_freq(struct device *dev,
  624. struct device_attribute *devattr,
  625. const char *buf, size_t count)
  626. {
  627. struct adt7470_data *data = dev_get_drvdata(dev);
  628. struct i2c_client *client = data->client;
  629. long freq;
  630. int index;
  631. int low_freq = ADT7470_CFG_LF;
  632. unsigned char val;
  633. if (kstrtol(buf, 10, &freq))
  634. return -EINVAL;
  635. /* Round the user value given to the closest available frequency */
  636. index = find_closest(freq, adt7470_freq_map,
  637. ARRAY_SIZE(adt7470_freq_map));
  638. if (index >= 8) {
  639. index -= 8;
  640. low_freq = 0;
  641. }
  642. mutex_lock(&data->lock);
  643. /* Configuration Register 1 */
  644. val = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
  645. i2c_smbus_write_byte_data(client, ADT7470_REG_CFG,
  646. (val & ~ADT7470_CFG_LF) | low_freq);
  647. /* Configuration Register 2 */
  648. val = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG_2);
  649. i2c_smbus_write_byte_data(client, ADT7470_REG_CFG_2,
  650. (val & ~ADT7470_FREQ_MASK) | (index << ADT7470_FREQ_SHIFT));
  651. mutex_unlock(&data->lock);
  652. return count;
  653. }
  654. static ssize_t show_pwm_max(struct device *dev,
  655. struct device_attribute *devattr,
  656. char *buf)
  657. {
  658. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  659. struct adt7470_data *data = adt7470_update_device(dev);
  660. return sprintf(buf, "%d\n", data->pwm_max[attr->index]);
  661. }
  662. static ssize_t set_pwm_max(struct device *dev,
  663. struct device_attribute *devattr,
  664. const char *buf,
  665. size_t count)
  666. {
  667. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  668. struct adt7470_data *data = dev_get_drvdata(dev);
  669. struct i2c_client *client = data->client;
  670. long temp;
  671. if (kstrtol(buf, 10, &temp))
  672. return -EINVAL;
  673. temp = clamp_val(temp, 0, 255);
  674. mutex_lock(&data->lock);
  675. data->pwm_max[attr->index] = temp;
  676. i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_MAX(attr->index),
  677. temp);
  678. mutex_unlock(&data->lock);
  679. return count;
  680. }
  681. static ssize_t show_pwm_min(struct device *dev,
  682. struct device_attribute *devattr,
  683. char *buf)
  684. {
  685. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  686. struct adt7470_data *data = adt7470_update_device(dev);
  687. return sprintf(buf, "%d\n", data->pwm_min[attr->index]);
  688. }
  689. static ssize_t set_pwm_min(struct device *dev,
  690. struct device_attribute *devattr,
  691. const char *buf,
  692. size_t count)
  693. {
  694. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  695. struct adt7470_data *data = dev_get_drvdata(dev);
  696. struct i2c_client *client = data->client;
  697. long temp;
  698. if (kstrtol(buf, 10, &temp))
  699. return -EINVAL;
  700. temp = clamp_val(temp, 0, 255);
  701. mutex_lock(&data->lock);
  702. data->pwm_min[attr->index] = temp;
  703. i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_MIN(attr->index),
  704. temp);
  705. mutex_unlock(&data->lock);
  706. return count;
  707. }
  708. static ssize_t show_pwm_tmax(struct device *dev,
  709. struct device_attribute *devattr,
  710. char *buf)
  711. {
  712. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  713. struct adt7470_data *data = adt7470_update_device(dev);
  714. /* the datasheet says that tmax = tmin + 20C */
  715. return sprintf(buf, "%d\n", 1000 * (20 + data->pwm_tmin[attr->index]));
  716. }
  717. static ssize_t show_pwm_tmin(struct device *dev,
  718. struct device_attribute *devattr,
  719. char *buf)
  720. {
  721. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  722. struct adt7470_data *data = adt7470_update_device(dev);
  723. return sprintf(buf, "%d\n", 1000 * data->pwm_tmin[attr->index]);
  724. }
  725. static ssize_t set_pwm_tmin(struct device *dev,
  726. struct device_attribute *devattr,
  727. const char *buf,
  728. size_t count)
  729. {
  730. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  731. struct adt7470_data *data = dev_get_drvdata(dev);
  732. struct i2c_client *client = data->client;
  733. long temp;
  734. if (kstrtol(buf, 10, &temp))
  735. return -EINVAL;
  736. temp = DIV_ROUND_CLOSEST(temp, 1000);
  737. temp = clamp_val(temp, -128, 127);
  738. mutex_lock(&data->lock);
  739. data->pwm_tmin[attr->index] = temp;
  740. i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_TMIN(attr->index),
  741. temp);
  742. mutex_unlock(&data->lock);
  743. return count;
  744. }
  745. static ssize_t show_pwm_auto(struct device *dev,
  746. struct device_attribute *devattr,
  747. char *buf)
  748. {
  749. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  750. struct adt7470_data *data = adt7470_update_device(dev);
  751. return sprintf(buf, "%d\n", 1 + data->pwm_automatic[attr->index]);
  752. }
  753. static ssize_t set_pwm_auto(struct device *dev,
  754. struct device_attribute *devattr,
  755. const char *buf,
  756. size_t count)
  757. {
  758. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  759. struct adt7470_data *data = dev_get_drvdata(dev);
  760. struct i2c_client *client = data->client;
  761. int pwm_auto_reg = ADT7470_REG_PWM_CFG(attr->index);
  762. int pwm_auto_reg_mask;
  763. long temp;
  764. u8 reg;
  765. if (kstrtol(buf, 10, &temp))
  766. return -EINVAL;
  767. if (attr->index % 2)
  768. pwm_auto_reg_mask = ADT7470_PWM2_AUTO_MASK;
  769. else
  770. pwm_auto_reg_mask = ADT7470_PWM1_AUTO_MASK;
  771. if (temp != 2 && temp != 1)
  772. return -EINVAL;
  773. temp--;
  774. mutex_lock(&data->lock);
  775. data->pwm_automatic[attr->index] = temp;
  776. reg = i2c_smbus_read_byte_data(client, pwm_auto_reg);
  777. if (temp)
  778. reg |= pwm_auto_reg_mask;
  779. else
  780. reg &= ~pwm_auto_reg_mask;
  781. i2c_smbus_write_byte_data(client, pwm_auto_reg, reg);
  782. mutex_unlock(&data->lock);
  783. return count;
  784. }
  785. static ssize_t show_pwm_auto_temp(struct device *dev,
  786. struct device_attribute *devattr,
  787. char *buf)
  788. {
  789. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  790. struct adt7470_data *data = adt7470_update_device(dev);
  791. u8 ctrl = data->pwm_auto_temp[attr->index];
  792. if (ctrl)
  793. return sprintf(buf, "%d\n", 1 << (ctrl - 1));
  794. else
  795. return sprintf(buf, "%d\n", ADT7470_PWM_ALL_TEMPS);
  796. }
  797. static int cvt_auto_temp(int input)
  798. {
  799. if (input == ADT7470_PWM_ALL_TEMPS)
  800. return 0;
  801. if (input < 1 || !is_power_of_2(input))
  802. return -EINVAL;
  803. return ilog2(input) + 1;
  804. }
  805. static ssize_t set_pwm_auto_temp(struct device *dev,
  806. struct device_attribute *devattr,
  807. const char *buf,
  808. size_t count)
  809. {
  810. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  811. struct adt7470_data *data = dev_get_drvdata(dev);
  812. struct i2c_client *client = data->client;
  813. int pwm_auto_reg = ADT7470_REG_PWM_AUTO_TEMP(attr->index);
  814. long temp;
  815. u8 reg;
  816. if (kstrtol(buf, 10, &temp))
  817. return -EINVAL;
  818. temp = cvt_auto_temp(temp);
  819. if (temp < 0)
  820. return temp;
  821. mutex_lock(&data->lock);
  822. data->pwm_automatic[attr->index] = temp;
  823. reg = i2c_smbus_read_byte_data(client, pwm_auto_reg);
  824. if (!(attr->index % 2)) {
  825. reg &= 0xF;
  826. reg |= (temp << 4) & 0xF0;
  827. } else {
  828. reg &= 0xF0;
  829. reg |= temp & 0xF;
  830. }
  831. i2c_smbus_write_byte_data(client, pwm_auto_reg, reg);
  832. mutex_unlock(&data->lock);
  833. return count;
  834. }
  835. static ssize_t show_alarm(struct device *dev,
  836. struct device_attribute *devattr,
  837. char *buf)
  838. {
  839. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  840. struct adt7470_data *data = adt7470_update_device(dev);
  841. if (data->alarm & attr->index)
  842. return sprintf(buf, "1\n");
  843. else
  844. return sprintf(buf, "0\n");
  845. }
  846. static DEVICE_ATTR(alarm_mask, S_IWUSR | S_IRUGO, show_alarm_mask,
  847. set_alarm_mask);
  848. static DEVICE_ATTR(num_temp_sensors, S_IWUSR | S_IRUGO, show_num_temp_sensors,
  849. set_num_temp_sensors);
  850. static DEVICE_ATTR(auto_update_interval, S_IWUSR | S_IRUGO,
  851. show_auto_update_interval, set_auto_update_interval);
  852. static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max,
  853. set_temp_max, 0);
  854. static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_max,
  855. set_temp_max, 1);
  856. static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_temp_max,
  857. set_temp_max, 2);
  858. static SENSOR_DEVICE_ATTR(temp4_max, S_IWUSR | S_IRUGO, show_temp_max,
  859. set_temp_max, 3);
  860. static SENSOR_DEVICE_ATTR(temp5_max, S_IWUSR | S_IRUGO, show_temp_max,
  861. set_temp_max, 4);
  862. static SENSOR_DEVICE_ATTR(temp6_max, S_IWUSR | S_IRUGO, show_temp_max,
  863. set_temp_max, 5);
  864. static SENSOR_DEVICE_ATTR(temp7_max, S_IWUSR | S_IRUGO, show_temp_max,
  865. set_temp_max, 6);
  866. static SENSOR_DEVICE_ATTR(temp8_max, S_IWUSR | S_IRUGO, show_temp_max,
  867. set_temp_max, 7);
  868. static SENSOR_DEVICE_ATTR(temp9_max, S_IWUSR | S_IRUGO, show_temp_max,
  869. set_temp_max, 8);
  870. static SENSOR_DEVICE_ATTR(temp10_max, S_IWUSR | S_IRUGO, show_temp_max,
  871. set_temp_max, 9);
  872. static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_min,
  873. set_temp_min, 0);
  874. static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_min,
  875. set_temp_min, 1);
  876. static SENSOR_DEVICE_ATTR(temp3_min, S_IWUSR | S_IRUGO, show_temp_min,
  877. set_temp_min, 2);
  878. static SENSOR_DEVICE_ATTR(temp4_min, S_IWUSR | S_IRUGO, show_temp_min,
  879. set_temp_min, 3);
  880. static SENSOR_DEVICE_ATTR(temp5_min, S_IWUSR | S_IRUGO, show_temp_min,
  881. set_temp_min, 4);
  882. static SENSOR_DEVICE_ATTR(temp6_min, S_IWUSR | S_IRUGO, show_temp_min,
  883. set_temp_min, 5);
  884. static SENSOR_DEVICE_ATTR(temp7_min, S_IWUSR | S_IRUGO, show_temp_min,
  885. set_temp_min, 6);
  886. static SENSOR_DEVICE_ATTR(temp8_min, S_IWUSR | S_IRUGO, show_temp_min,
  887. set_temp_min, 7);
  888. static SENSOR_DEVICE_ATTR(temp9_min, S_IWUSR | S_IRUGO, show_temp_min,
  889. set_temp_min, 8);
  890. static SENSOR_DEVICE_ATTR(temp10_min, S_IWUSR | S_IRUGO, show_temp_min,
  891. set_temp_min, 9);
  892. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
  893. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
  894. static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
  895. static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
  896. static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO, show_temp, NULL, 4);
  897. static SENSOR_DEVICE_ATTR(temp6_input, S_IRUGO, show_temp, NULL, 5);
  898. static SENSOR_DEVICE_ATTR(temp7_input, S_IRUGO, show_temp, NULL, 6);
  899. static SENSOR_DEVICE_ATTR(temp8_input, S_IRUGO, show_temp, NULL, 7);
  900. static SENSOR_DEVICE_ATTR(temp9_input, S_IRUGO, show_temp, NULL, 8);
  901. static SENSOR_DEVICE_ATTR(temp10_input, S_IRUGO, show_temp, NULL, 9);
  902. static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL,
  903. ADT7470_R1T_ALARM);
  904. static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL,
  905. ADT7470_R2T_ALARM);
  906. static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL,
  907. ADT7470_R3T_ALARM);
  908. static SENSOR_DEVICE_ATTR(temp4_alarm, S_IRUGO, show_alarm, NULL,
  909. ADT7470_R4T_ALARM);
  910. static SENSOR_DEVICE_ATTR(temp5_alarm, S_IRUGO, show_alarm, NULL,
  911. ADT7470_R5T_ALARM);
  912. static SENSOR_DEVICE_ATTR(temp6_alarm, S_IRUGO, show_alarm, NULL,
  913. ADT7470_R6T_ALARM);
  914. static SENSOR_DEVICE_ATTR(temp7_alarm, S_IRUGO, show_alarm, NULL,
  915. ADT7470_R7T_ALARM);
  916. static SENSOR_DEVICE_ATTR(temp8_alarm, S_IRUGO, show_alarm, NULL,
  917. ALARM2(ADT7470_R8T_ALARM));
  918. static SENSOR_DEVICE_ATTR(temp9_alarm, S_IRUGO, show_alarm, NULL,
  919. ALARM2(ADT7470_R9T_ALARM));
  920. static SENSOR_DEVICE_ATTR(temp10_alarm, S_IRUGO, show_alarm, NULL,
  921. ALARM2(ADT7470_R10T_ALARM));
  922. static SENSOR_DEVICE_ATTR(fan1_max, S_IWUSR | S_IRUGO, show_fan_max,
  923. set_fan_max, 0);
  924. static SENSOR_DEVICE_ATTR(fan2_max, S_IWUSR | S_IRUGO, show_fan_max,
  925. set_fan_max, 1);
  926. static SENSOR_DEVICE_ATTR(fan3_max, S_IWUSR | S_IRUGO, show_fan_max,
  927. set_fan_max, 2);
  928. static SENSOR_DEVICE_ATTR(fan4_max, S_IWUSR | S_IRUGO, show_fan_max,
  929. set_fan_max, 3);
  930. static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min,
  931. set_fan_min, 0);
  932. static SENSOR_DEVICE_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min,
  933. set_fan_min, 1);
  934. static SENSOR_DEVICE_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min,
  935. set_fan_min, 2);
  936. static SENSOR_DEVICE_ATTR(fan4_min, S_IWUSR | S_IRUGO, show_fan_min,
  937. set_fan_min, 3);
  938. static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
  939. static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
  940. static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2);
  941. static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3);
  942. static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL,
  943. ALARM2(ADT7470_FAN1_ALARM));
  944. static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL,
  945. ALARM2(ADT7470_FAN2_ALARM));
  946. static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL,
  947. ALARM2(ADT7470_FAN3_ALARM));
  948. static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL,
  949. ALARM2(ADT7470_FAN4_ALARM));
  950. static SENSOR_DEVICE_ATTR(force_pwm_max, S_IWUSR | S_IRUGO,
  951. show_force_pwm_max, set_force_pwm_max, 0);
  952. static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 0);
  953. static SENSOR_DEVICE_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 1);
  954. static SENSOR_DEVICE_ATTR(pwm3, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 2);
  955. static SENSOR_DEVICE_ATTR(pwm4, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 3);
  956. static DEVICE_ATTR(pwm1_freq, S_IWUSR | S_IRUGO, show_pwm_freq, set_pwm_freq);
  957. static SENSOR_DEVICE_ATTR(pwm1_auto_point1_pwm, S_IWUSR | S_IRUGO,
  958. show_pwm_min, set_pwm_min, 0);
  959. static SENSOR_DEVICE_ATTR(pwm2_auto_point1_pwm, S_IWUSR | S_IRUGO,
  960. show_pwm_min, set_pwm_min, 1);
  961. static SENSOR_DEVICE_ATTR(pwm3_auto_point1_pwm, S_IWUSR | S_IRUGO,
  962. show_pwm_min, set_pwm_min, 2);
  963. static SENSOR_DEVICE_ATTR(pwm4_auto_point1_pwm, S_IWUSR | S_IRUGO,
  964. show_pwm_min, set_pwm_min, 3);
  965. static SENSOR_DEVICE_ATTR(pwm1_auto_point2_pwm, S_IWUSR | S_IRUGO,
  966. show_pwm_max, set_pwm_max, 0);
  967. static SENSOR_DEVICE_ATTR(pwm2_auto_point2_pwm, S_IWUSR | S_IRUGO,
  968. show_pwm_max, set_pwm_max, 1);
  969. static SENSOR_DEVICE_ATTR(pwm3_auto_point2_pwm, S_IWUSR | S_IRUGO,
  970. show_pwm_max, set_pwm_max, 2);
  971. static SENSOR_DEVICE_ATTR(pwm4_auto_point2_pwm, S_IWUSR | S_IRUGO,
  972. show_pwm_max, set_pwm_max, 3);
  973. static SENSOR_DEVICE_ATTR(pwm1_auto_point1_temp, S_IWUSR | S_IRUGO,
  974. show_pwm_tmin, set_pwm_tmin, 0);
  975. static SENSOR_DEVICE_ATTR(pwm2_auto_point1_temp, S_IWUSR | S_IRUGO,
  976. show_pwm_tmin, set_pwm_tmin, 1);
  977. static SENSOR_DEVICE_ATTR(pwm3_auto_point1_temp, S_IWUSR | S_IRUGO,
  978. show_pwm_tmin, set_pwm_tmin, 2);
  979. static SENSOR_DEVICE_ATTR(pwm4_auto_point1_temp, S_IWUSR | S_IRUGO,
  980. show_pwm_tmin, set_pwm_tmin, 3);
  981. static SENSOR_DEVICE_ATTR(pwm1_auto_point2_temp, S_IRUGO, show_pwm_tmax,
  982. NULL, 0);
  983. static SENSOR_DEVICE_ATTR(pwm2_auto_point2_temp, S_IRUGO, show_pwm_tmax,
  984. NULL, 1);
  985. static SENSOR_DEVICE_ATTR(pwm3_auto_point2_temp, S_IRUGO, show_pwm_tmax,
  986. NULL, 2);
  987. static SENSOR_DEVICE_ATTR(pwm4_auto_point2_temp, S_IRUGO, show_pwm_tmax,
  988. NULL, 3);
  989. static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, show_pwm_auto,
  990. set_pwm_auto, 0);
  991. static SENSOR_DEVICE_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, show_pwm_auto,
  992. set_pwm_auto, 1);
  993. static SENSOR_DEVICE_ATTR(pwm3_enable, S_IWUSR | S_IRUGO, show_pwm_auto,
  994. set_pwm_auto, 2);
  995. static SENSOR_DEVICE_ATTR(pwm4_enable, S_IWUSR | S_IRUGO, show_pwm_auto,
  996. set_pwm_auto, 3);
  997. static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IWUSR | S_IRUGO,
  998. show_pwm_auto_temp, set_pwm_auto_temp, 0);
  999. static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IWUSR | S_IRUGO,
  1000. show_pwm_auto_temp, set_pwm_auto_temp, 1);
  1001. static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IWUSR | S_IRUGO,
  1002. show_pwm_auto_temp, set_pwm_auto_temp, 2);
  1003. static SENSOR_DEVICE_ATTR(pwm4_auto_channels_temp, S_IWUSR | S_IRUGO,
  1004. show_pwm_auto_temp, set_pwm_auto_temp, 3);
  1005. static struct attribute *adt7470_attrs[] = {
  1006. &dev_attr_alarm_mask.attr,
  1007. &dev_attr_num_temp_sensors.attr,
  1008. &dev_attr_auto_update_interval.attr,
  1009. &sensor_dev_attr_temp1_max.dev_attr.attr,
  1010. &sensor_dev_attr_temp2_max.dev_attr.attr,
  1011. &sensor_dev_attr_temp3_max.dev_attr.attr,
  1012. &sensor_dev_attr_temp4_max.dev_attr.attr,
  1013. &sensor_dev_attr_temp5_max.dev_attr.attr,
  1014. &sensor_dev_attr_temp6_max.dev_attr.attr,
  1015. &sensor_dev_attr_temp7_max.dev_attr.attr,
  1016. &sensor_dev_attr_temp8_max.dev_attr.attr,
  1017. &sensor_dev_attr_temp9_max.dev_attr.attr,
  1018. &sensor_dev_attr_temp10_max.dev_attr.attr,
  1019. &sensor_dev_attr_temp1_min.dev_attr.attr,
  1020. &sensor_dev_attr_temp2_min.dev_attr.attr,
  1021. &sensor_dev_attr_temp3_min.dev_attr.attr,
  1022. &sensor_dev_attr_temp4_min.dev_attr.attr,
  1023. &sensor_dev_attr_temp5_min.dev_attr.attr,
  1024. &sensor_dev_attr_temp6_min.dev_attr.attr,
  1025. &sensor_dev_attr_temp7_min.dev_attr.attr,
  1026. &sensor_dev_attr_temp8_min.dev_attr.attr,
  1027. &sensor_dev_attr_temp9_min.dev_attr.attr,
  1028. &sensor_dev_attr_temp10_min.dev_attr.attr,
  1029. &sensor_dev_attr_temp1_input.dev_attr.attr,
  1030. &sensor_dev_attr_temp2_input.dev_attr.attr,
  1031. &sensor_dev_attr_temp3_input.dev_attr.attr,
  1032. &sensor_dev_attr_temp4_input.dev_attr.attr,
  1033. &sensor_dev_attr_temp5_input.dev_attr.attr,
  1034. &sensor_dev_attr_temp6_input.dev_attr.attr,
  1035. &sensor_dev_attr_temp7_input.dev_attr.attr,
  1036. &sensor_dev_attr_temp8_input.dev_attr.attr,
  1037. &sensor_dev_attr_temp9_input.dev_attr.attr,
  1038. &sensor_dev_attr_temp10_input.dev_attr.attr,
  1039. &sensor_dev_attr_temp1_alarm.dev_attr.attr,
  1040. &sensor_dev_attr_temp2_alarm.dev_attr.attr,
  1041. &sensor_dev_attr_temp3_alarm.dev_attr.attr,
  1042. &sensor_dev_attr_temp4_alarm.dev_attr.attr,
  1043. &sensor_dev_attr_temp5_alarm.dev_attr.attr,
  1044. &sensor_dev_attr_temp6_alarm.dev_attr.attr,
  1045. &sensor_dev_attr_temp7_alarm.dev_attr.attr,
  1046. &sensor_dev_attr_temp8_alarm.dev_attr.attr,
  1047. &sensor_dev_attr_temp9_alarm.dev_attr.attr,
  1048. &sensor_dev_attr_temp10_alarm.dev_attr.attr,
  1049. &sensor_dev_attr_fan1_max.dev_attr.attr,
  1050. &sensor_dev_attr_fan2_max.dev_attr.attr,
  1051. &sensor_dev_attr_fan3_max.dev_attr.attr,
  1052. &sensor_dev_attr_fan4_max.dev_attr.attr,
  1053. &sensor_dev_attr_fan1_min.dev_attr.attr,
  1054. &sensor_dev_attr_fan2_min.dev_attr.attr,
  1055. &sensor_dev_attr_fan3_min.dev_attr.attr,
  1056. &sensor_dev_attr_fan4_min.dev_attr.attr,
  1057. &sensor_dev_attr_fan1_input.dev_attr.attr,
  1058. &sensor_dev_attr_fan2_input.dev_attr.attr,
  1059. &sensor_dev_attr_fan3_input.dev_attr.attr,
  1060. &sensor_dev_attr_fan4_input.dev_attr.attr,
  1061. &sensor_dev_attr_fan1_alarm.dev_attr.attr,
  1062. &sensor_dev_attr_fan2_alarm.dev_attr.attr,
  1063. &sensor_dev_attr_fan3_alarm.dev_attr.attr,
  1064. &sensor_dev_attr_fan4_alarm.dev_attr.attr,
  1065. &sensor_dev_attr_force_pwm_max.dev_attr.attr,
  1066. &sensor_dev_attr_pwm1.dev_attr.attr,
  1067. &dev_attr_pwm1_freq.attr,
  1068. &sensor_dev_attr_pwm2.dev_attr.attr,
  1069. &sensor_dev_attr_pwm3.dev_attr.attr,
  1070. &sensor_dev_attr_pwm4.dev_attr.attr,
  1071. &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
  1072. &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
  1073. &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
  1074. &sensor_dev_attr_pwm4_auto_point1_pwm.dev_attr.attr,
  1075. &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
  1076. &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
  1077. &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
  1078. &sensor_dev_attr_pwm4_auto_point2_pwm.dev_attr.attr,
  1079. &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
  1080. &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
  1081. &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
  1082. &sensor_dev_attr_pwm4_auto_point1_temp.dev_attr.attr,
  1083. &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
  1084. &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
  1085. &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
  1086. &sensor_dev_attr_pwm4_auto_point2_temp.dev_attr.attr,
  1087. &sensor_dev_attr_pwm1_enable.dev_attr.attr,
  1088. &sensor_dev_attr_pwm2_enable.dev_attr.attr,
  1089. &sensor_dev_attr_pwm3_enable.dev_attr.attr,
  1090. &sensor_dev_attr_pwm4_enable.dev_attr.attr,
  1091. &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
  1092. &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
  1093. &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
  1094. &sensor_dev_attr_pwm4_auto_channels_temp.dev_attr.attr,
  1095. NULL
  1096. };
  1097. ATTRIBUTE_GROUPS(adt7470);
  1098. /* Return 0 if detection is successful, -ENODEV otherwise */
  1099. static int adt7470_detect(struct i2c_client *client,
  1100. struct i2c_board_info *info)
  1101. {
  1102. struct i2c_adapter *adapter = client->adapter;
  1103. int vendor, device, revision;
  1104. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  1105. return -ENODEV;
  1106. vendor = i2c_smbus_read_byte_data(client, ADT7470_REG_VENDOR);
  1107. if (vendor != ADT7470_VENDOR)
  1108. return -ENODEV;
  1109. device = i2c_smbus_read_byte_data(client, ADT7470_REG_DEVICE);
  1110. if (device != ADT7470_DEVICE)
  1111. return -ENODEV;
  1112. revision = i2c_smbus_read_byte_data(client, ADT7470_REG_REVISION);
  1113. if (revision != ADT7470_REVISION)
  1114. return -ENODEV;
  1115. strlcpy(info->type, "adt7470", I2C_NAME_SIZE);
  1116. return 0;
  1117. }
  1118. static void adt7470_init_client(struct i2c_client *client)
  1119. {
  1120. int reg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
  1121. if (reg < 0) {
  1122. dev_err(&client->dev, "cannot read configuration register\n");
  1123. } else {
  1124. /* start monitoring (and do a self-test) */
  1125. i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, reg | 3);
  1126. }
  1127. }
  1128. static int adt7470_probe(struct i2c_client *client,
  1129. const struct i2c_device_id *id)
  1130. {
  1131. struct device *dev = &client->dev;
  1132. struct adt7470_data *data;
  1133. struct device *hwmon_dev;
  1134. data = devm_kzalloc(dev, sizeof(struct adt7470_data), GFP_KERNEL);
  1135. if (!data)
  1136. return -ENOMEM;
  1137. data->num_temp_sensors = -1;
  1138. data->auto_update_interval = AUTO_UPDATE_INTERVAL;
  1139. i2c_set_clientdata(client, data);
  1140. data->client = client;
  1141. mutex_init(&data->lock);
  1142. dev_info(&client->dev, "%s chip found\n", client->name);
  1143. /* Initialize the ADT7470 chip */
  1144. adt7470_init_client(client);
  1145. /* Register sysfs hooks */
  1146. hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
  1147. data,
  1148. adt7470_groups);
  1149. if (IS_ERR(hwmon_dev))
  1150. return PTR_ERR(hwmon_dev);
  1151. data->auto_update = kthread_run(adt7470_update_thread, client, "%s",
  1152. dev_name(hwmon_dev));
  1153. if (IS_ERR(data->auto_update)) {
  1154. return PTR_ERR(data->auto_update);
  1155. }
  1156. return 0;
  1157. }
  1158. static int adt7470_remove(struct i2c_client *client)
  1159. {
  1160. struct adt7470_data *data = i2c_get_clientdata(client);
  1161. kthread_stop(data->auto_update);
  1162. return 0;
  1163. }
  1164. static const struct i2c_device_id adt7470_id[] = {
  1165. { "adt7470", 0 },
  1166. { }
  1167. };
  1168. MODULE_DEVICE_TABLE(i2c, adt7470_id);
  1169. static struct i2c_driver adt7470_driver = {
  1170. .class = I2C_CLASS_HWMON,
  1171. .driver = {
  1172. .name = "adt7470",
  1173. },
  1174. .probe = adt7470_probe,
  1175. .remove = adt7470_remove,
  1176. .id_table = adt7470_id,
  1177. .detect = adt7470_detect,
  1178. .address_list = normal_i2c,
  1179. };
  1180. module_i2c_driver(adt7470_driver);
  1181. MODULE_AUTHOR("Darrick J. Wong <[email protected]>");
  1182. MODULE_DESCRIPTION("ADT7470 driver");
  1183. MODULE_LICENSE("GPL");