adt7470.c 41 KB

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