nct6775.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  1. /*
  2. * nct6775 - Driver for the hardware monitoring functionality of
  3. * Nuvoton NCT677x Super-I/O chips
  4. *
  5. * Copyright (C) 2012 Guenter Roeck <linux@roeck-us.net>
  6. *
  7. * Derived from w83627ehf driver
  8. * Copyright (C) 2005-2012 Jean Delvare <khali@linux-fr.org>
  9. * Copyright (C) 2006 Yuan Mu (Winbond),
  10. * Rudolf Marek <r.marek@assembler.cz>
  11. * David Hubbard <david.c.hubbard@gmail.com>
  12. * Daniel J Blueman <daniel.blueman@gmail.com>
  13. * Copyright (C) 2010 Sheng-Yuan Huang (Nuvoton) (PS00)
  14. *
  15. * Shamelessly ripped from the w83627hf driver
  16. * Copyright (C) 2003 Mark Studebaker
  17. *
  18. * This program is free software; you can redistribute it and/or modify
  19. * it under the terms of the GNU General Public License as published by
  20. * the Free Software Foundation; either version 2 of the License, or
  21. * (at your option) any later version.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU General Public License
  29. * along with this program; if not, write to the Free Software
  30. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  31. *
  32. *
  33. * Supports the following chips:
  34. *
  35. * Chip #vin #fan #pwm #temp chip IDs man ID
  36. * nct6775f 9 4 3 6+3 0xb470 0xc1 0x5ca3
  37. * nct6776f 9 5 3 6+3 0xc330 0xc1 0x5ca3
  38. * nct6779d 15 5 5 2+6 0xc560 0xc1 0x5ca3
  39. *
  40. * #temp lists the number of monitored temperature sources (first value) plus
  41. * the number of directly connectable temperature sensors (second value).
  42. */
  43. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  44. #include <linux/module.h>
  45. #include <linux/init.h>
  46. #include <linux/slab.h>
  47. #include <linux/jiffies.h>
  48. #include <linux/platform_device.h>
  49. #include <linux/hwmon.h>
  50. #include <linux/hwmon-sysfs.h>
  51. #include <linux/hwmon-vid.h>
  52. #include <linux/err.h>
  53. #include <linux/mutex.h>
  54. #include <linux/acpi.h>
  55. #include <linux/io.h>
  56. #include "lm75.h"
  57. enum kinds { nct6775, nct6776, nct6779 };
  58. /* used to set data->name = nct6775_device_names[data->sio_kind] */
  59. static const char * const nct6775_device_names[] = {
  60. "nct6775",
  61. "nct6776",
  62. "nct6779",
  63. };
  64. static unsigned short force_id;
  65. module_param(force_id, ushort, 0);
  66. MODULE_PARM_DESC(force_id, "Override the detected device ID");
  67. #define DRVNAME "nct6775"
  68. /*
  69. * Super-I/O constants and functions
  70. */
  71. #define NCT6775_LD_ACPI 0x0a
  72. #define NCT6775_LD_HWM 0x0b
  73. #define NCT6775_LD_VID 0x0d
  74. #define SIO_REG_LDSEL 0x07 /* Logical device select */
  75. #define SIO_REG_DEVID 0x20 /* Device ID (2 bytes) */
  76. #define SIO_REG_ENABLE 0x30 /* Logical device enable */
  77. #define SIO_REG_ADDR 0x60 /* Logical device address (2 bytes) */
  78. #define SIO_NCT6775_ID 0xb470
  79. #define SIO_NCT6776_ID 0xc330
  80. #define SIO_NCT6779_ID 0xc560
  81. #define SIO_ID_MASK 0xFFF0
  82. static inline void
  83. superio_outb(int ioreg, int reg, int val)
  84. {
  85. outb(reg, ioreg);
  86. outb(val, ioreg + 1);
  87. }
  88. static inline int
  89. superio_inb(int ioreg, int reg)
  90. {
  91. outb(reg, ioreg);
  92. return inb(ioreg + 1);
  93. }
  94. static inline void
  95. superio_select(int ioreg, int ld)
  96. {
  97. outb(SIO_REG_LDSEL, ioreg);
  98. outb(ld, ioreg + 1);
  99. }
  100. static inline int
  101. superio_enter(int ioreg)
  102. {
  103. /*
  104. * Try to reserve <ioreg> and <ioreg + 1> for exclusive access.
  105. */
  106. if (!request_muxed_region(ioreg, 2, DRVNAME))
  107. return -EBUSY;
  108. outb(0x87, ioreg);
  109. outb(0x87, ioreg);
  110. return 0;
  111. }
  112. static inline void
  113. superio_exit(int ioreg)
  114. {
  115. outb(0xaa, ioreg);
  116. outb(0x02, ioreg);
  117. outb(0x02, ioreg + 1);
  118. release_region(ioreg, 2);
  119. }
  120. /*
  121. * ISA constants
  122. */
  123. #define IOREGION_ALIGNMENT (~7)
  124. #define IOREGION_OFFSET 5
  125. #define IOREGION_LENGTH 2
  126. #define ADDR_REG_OFFSET 0
  127. #define DATA_REG_OFFSET 1
  128. #define NCT6775_REG_BANK 0x4E
  129. #define NCT6775_REG_CONFIG 0x40
  130. /*
  131. * Not currently used:
  132. * REG_MAN_ID has the value 0x5ca3 for all supported chips.
  133. * REG_CHIP_ID == 0x88/0xa1/0xc1 depending on chip model.
  134. * REG_MAN_ID is at port 0x4f
  135. * REG_CHIP_ID is at port 0x58
  136. */
  137. #define NUM_REG_ALARM 4 /* Max number of alarm registers */
  138. /* Common and NCT6775 specific data */
  139. /* Voltage min/max registers for nr=7..14 are in bank 5 */
  140. static const u16 NCT6775_REG_IN_MAX[] = {
  141. 0x2b, 0x2d, 0x2f, 0x31, 0x33, 0x35, 0x37, 0x554, 0x556, 0x558, 0x55a,
  142. 0x55c, 0x55e, 0x560, 0x562 };
  143. static const u16 NCT6775_REG_IN_MIN[] = {
  144. 0x2c, 0x2e, 0x30, 0x32, 0x34, 0x36, 0x38, 0x555, 0x557, 0x559, 0x55b,
  145. 0x55d, 0x55f, 0x561, 0x563 };
  146. static const u16 NCT6775_REG_IN[] = {
  147. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x550, 0x551, 0x552
  148. };
  149. #define NCT6775_REG_VBAT 0x5D
  150. static const u16 NCT6775_REG_ALARM[NUM_REG_ALARM] = { 0x459, 0x45A, 0x45B };
  151. /* 0..15 voltages, 16..23 fans, 24..31 temperatures */
  152. static const s8 NCT6775_ALARM_BITS[] = {
  153. 0, 1, 2, 3, 8, 21, 20, 16, /* in0.. in7 */
  154. 17, -1, -1, -1, -1, -1, -1, /* in8..in14 */
  155. -1, /* unused */
  156. 6, 7, 11, 10, 23, /* fan1..fan5 */
  157. -1, -1, -1, /* unused */
  158. 4, 5, 13, -1, -1, -1, /* temp1..temp6 */
  159. 12, -1 }; /* intrusion0, intrusion1 */
  160. #define INTRUSION_ALARM_BASE 30
  161. static const u8 NCT6775_REG_CR_CASEOPEN_CLR[] = { 0xe6, 0xee };
  162. static const u8 NCT6775_CR_CASEOPEN_CLR_MASK[] = { 0x20, 0x01 };
  163. /* NCT6776 specific data */
  164. static const s8 NCT6776_ALARM_BITS[] = {
  165. 0, 1, 2, 3, 8, 21, 20, 16, /* in0.. in7 */
  166. 17, -1, -1, -1, -1, -1, -1, /* in8..in14 */
  167. -1, /* unused */
  168. 6, 7, 11, 10, 23, /* fan1..fan5 */
  169. -1, -1, -1, /* unused */
  170. 4, 5, 13, -1, -1, -1, /* temp1..temp6 */
  171. 12, 9 }; /* intrusion0, intrusion1 */
  172. /* NCT6779 specific data */
  173. static const u16 NCT6779_REG_IN[] = {
  174. 0x480, 0x481, 0x482, 0x483, 0x484, 0x485, 0x486, 0x487,
  175. 0x488, 0x489, 0x48a, 0x48b, 0x48c, 0x48d, 0x48e };
  176. static const u16 NCT6779_REG_ALARM[NUM_REG_ALARM] = {
  177. 0x459, 0x45A, 0x45B, 0x568 };
  178. static const s8 NCT6779_ALARM_BITS[] = {
  179. 0, 1, 2, 3, 8, 21, 20, 16, /* in0.. in7 */
  180. 17, 24, 25, 26, 27, 28, 29, /* in8..in14 */
  181. -1, /* unused */
  182. 6, 7, 11, 10, 23, /* fan1..fan5 */
  183. -1, -1, -1, /* unused */
  184. 4, 5, 13, -1, -1, -1, /* temp1..temp6 */
  185. 12, 9 }; /* intrusion0, intrusion1 */
  186. /*
  187. * Conversions
  188. */
  189. /*
  190. * Some of the voltage inputs have internal scaling, the tables below
  191. * contain 8 (the ADC LSB in mV) * scaling factor * 100
  192. */
  193. static const u16 scale_in[15] = {
  194. 800, 800, 1600, 1600, 800, 800, 800, 1600, 1600, 800, 800, 800, 800,
  195. 800, 800
  196. };
  197. static inline long in_from_reg(u8 reg, u8 nr)
  198. {
  199. return DIV_ROUND_CLOSEST(reg * scale_in[nr], 100);
  200. }
  201. static inline u8 in_to_reg(u32 val, u8 nr)
  202. {
  203. return clamp_val(DIV_ROUND_CLOSEST(val * 100, scale_in[nr]), 0, 255);
  204. }
  205. /*
  206. * Data structures and manipulation thereof
  207. */
  208. struct nct6775_data {
  209. int addr; /* IO base of hw monitor block */
  210. enum kinds kind;
  211. const char *name;
  212. struct device *hwmon_dev;
  213. struct mutex lock;
  214. u16 REG_CONFIG;
  215. u16 REG_VBAT;
  216. const s8 *ALARM_BITS;
  217. const u16 *REG_VIN;
  218. const u16 *REG_IN_MINMAX[2];
  219. const u16 *REG_ALARM;
  220. struct mutex update_lock;
  221. bool valid; /* true if following fields are valid */
  222. unsigned long last_updated; /* In jiffies */
  223. /* Register values */
  224. u8 bank; /* current register bank */
  225. u8 in_num; /* number of in inputs we have */
  226. u8 in[15][3]; /* [0]=in, [1]=in_max, [2]=in_min */
  227. u64 alarms;
  228. u8 vid;
  229. u8 vrm;
  230. u16 have_in;
  231. };
  232. struct nct6775_sio_data {
  233. int sioreg;
  234. enum kinds kind;
  235. };
  236. static bool is_word_sized(struct nct6775_data *data, u16 reg)
  237. {
  238. switch (data->kind) {
  239. case nct6775:
  240. return (((reg & 0xff00) == 0x100 ||
  241. (reg & 0xff00) == 0x200) &&
  242. ((reg & 0x00ff) == 0x50 ||
  243. (reg & 0x00ff) == 0x53 ||
  244. (reg & 0x00ff) == 0x55)) ||
  245. (reg & 0xfff0) == 0x630 ||
  246. reg == 0x640 || reg == 0x642 ||
  247. reg == 0x662 ||
  248. ((reg & 0xfff0) == 0x650 && (reg & 0x000f) >= 0x06) ||
  249. reg == 0x73 || reg == 0x75 || reg == 0x77;
  250. case nct6776:
  251. return (((reg & 0xff00) == 0x100 ||
  252. (reg & 0xff00) == 0x200) &&
  253. ((reg & 0x00ff) == 0x50 ||
  254. (reg & 0x00ff) == 0x53 ||
  255. (reg & 0x00ff) == 0x55)) ||
  256. (reg & 0xfff0) == 0x630 ||
  257. reg == 0x402 ||
  258. reg == 0x640 || reg == 0x642 ||
  259. ((reg & 0xfff0) == 0x650 && (reg & 0x000f) >= 0x06) ||
  260. reg == 0x73 || reg == 0x75 || reg == 0x77;
  261. case nct6779:
  262. return reg == 0x150 || reg == 0x153 || reg == 0x155 ||
  263. ((reg & 0xfff0) == 0x4b0 && (reg & 0x000f) < 0x09) ||
  264. reg == 0x402 ||
  265. reg == 0x63a || reg == 0x63c || reg == 0x63e ||
  266. reg == 0x640 || reg == 0x642 ||
  267. reg == 0x73 || reg == 0x75 || reg == 0x77 || reg == 0x79 ||
  268. reg == 0x7b;
  269. }
  270. return false;
  271. }
  272. /*
  273. * On older chips, only registers 0x50-0x5f are banked.
  274. * On more recent chips, all registers are banked.
  275. * Assume that is the case and set the bank number for each access.
  276. * Cache the bank number so it only needs to be set if it changes.
  277. */
  278. static inline void nct6775_set_bank(struct nct6775_data *data, u16 reg)
  279. {
  280. u8 bank = reg >> 8;
  281. if (data->bank != bank) {
  282. outb_p(NCT6775_REG_BANK, data->addr + ADDR_REG_OFFSET);
  283. outb_p(bank, data->addr + DATA_REG_OFFSET);
  284. data->bank = bank;
  285. }
  286. }
  287. static u16 nct6775_read_value(struct nct6775_data *data, u16 reg)
  288. {
  289. int res, word_sized = is_word_sized(data, reg);
  290. mutex_lock(&data->lock);
  291. nct6775_set_bank(data, reg);
  292. outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
  293. res = inb_p(data->addr + DATA_REG_OFFSET);
  294. if (word_sized) {
  295. outb_p((reg & 0xff) + 1,
  296. data->addr + ADDR_REG_OFFSET);
  297. res = (res << 8) + inb_p(data->addr + DATA_REG_OFFSET);
  298. }
  299. mutex_unlock(&data->lock);
  300. return res;
  301. }
  302. static int nct6775_write_value(struct nct6775_data *data, u16 reg, u16 value)
  303. {
  304. int word_sized = is_word_sized(data, reg);
  305. mutex_lock(&data->lock);
  306. nct6775_set_bank(data, reg);
  307. outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
  308. if (word_sized) {
  309. outb_p(value >> 8, data->addr + DATA_REG_OFFSET);
  310. outb_p((reg & 0xff) + 1,
  311. data->addr + ADDR_REG_OFFSET);
  312. }
  313. outb_p(value & 0xff, data->addr + DATA_REG_OFFSET);
  314. mutex_unlock(&data->lock);
  315. return 0;
  316. }
  317. static struct nct6775_data *nct6775_update_device(struct device *dev)
  318. {
  319. struct nct6775_data *data = dev_get_drvdata(dev);
  320. int i;
  321. mutex_lock(&data->update_lock);
  322. if (time_after(jiffies, data->last_updated + HZ + HZ/2)
  323. || !data->valid) {
  324. /* Measured voltages and limits */
  325. for (i = 0; i < data->in_num; i++) {
  326. if (!(data->have_in & (1 << i)))
  327. continue;
  328. data->in[i][0] = nct6775_read_value(data,
  329. data->REG_VIN[i]);
  330. data->in[i][1] = nct6775_read_value(data,
  331. data->REG_IN_MINMAX[0][i]);
  332. data->in[i][2] = nct6775_read_value(data,
  333. data->REG_IN_MINMAX[1][i]);
  334. }
  335. data->alarms = 0;
  336. for (i = 0; i < NUM_REG_ALARM; i++) {
  337. u8 alarm;
  338. if (!data->REG_ALARM[i])
  339. continue;
  340. alarm = nct6775_read_value(data, data->REG_ALARM[i]);
  341. data->alarms |= ((u64)alarm) << (i << 3);
  342. }
  343. data->last_updated = jiffies;
  344. data->valid = true;
  345. }
  346. mutex_unlock(&data->update_lock);
  347. return data;
  348. }
  349. /*
  350. * Sysfs callback functions
  351. */
  352. static ssize_t
  353. show_in_reg(struct device *dev, struct device_attribute *attr, char *buf)
  354. {
  355. struct nct6775_data *data = nct6775_update_device(dev);
  356. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  357. int nr = sattr->nr;
  358. int index = sattr->index;
  359. return sprintf(buf, "%ld\n", in_from_reg(data->in[nr][index], nr));
  360. }
  361. static ssize_t
  362. store_in_reg(struct device *dev, struct device_attribute *attr, const char *buf,
  363. size_t count)
  364. {
  365. struct nct6775_data *data = dev_get_drvdata(dev);
  366. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  367. int nr = sattr->nr;
  368. int index = sattr->index;
  369. unsigned long val;
  370. int err = kstrtoul(buf, 10, &val);
  371. if (err < 0)
  372. return err;
  373. mutex_lock(&data->update_lock);
  374. data->in[nr][index] = in_to_reg(val, nr);
  375. nct6775_write_value(data, data->REG_IN_MINMAX[index-1][nr],
  376. data->in[nr][index]);
  377. mutex_unlock(&data->update_lock);
  378. return count;
  379. }
  380. static ssize_t
  381. show_alarm(struct device *dev, struct device_attribute *attr, char *buf)
  382. {
  383. struct nct6775_data *data = nct6775_update_device(dev);
  384. struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
  385. int nr = data->ALARM_BITS[sattr->index];
  386. return sprintf(buf, "%u\n",
  387. (unsigned int)((data->alarms >> nr) & 0x01));
  388. }
  389. static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in_reg, NULL, 0, 0);
  390. static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in_reg, NULL, 1, 0);
  391. static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in_reg, NULL, 2, 0);
  392. static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in_reg, NULL, 3, 0);
  393. static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in_reg, NULL, 4, 0);
  394. static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in_reg, NULL, 5, 0);
  395. static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in_reg, NULL, 6, 0);
  396. static SENSOR_DEVICE_ATTR_2(in7_input, S_IRUGO, show_in_reg, NULL, 7, 0);
  397. static SENSOR_DEVICE_ATTR_2(in8_input, S_IRUGO, show_in_reg, NULL, 8, 0);
  398. static SENSOR_DEVICE_ATTR_2(in9_input, S_IRUGO, show_in_reg, NULL, 9, 0);
  399. static SENSOR_DEVICE_ATTR_2(in10_input, S_IRUGO, show_in_reg, NULL, 10, 0);
  400. static SENSOR_DEVICE_ATTR_2(in11_input, S_IRUGO, show_in_reg, NULL, 11, 0);
  401. static SENSOR_DEVICE_ATTR_2(in12_input, S_IRUGO, show_in_reg, NULL, 12, 0);
  402. static SENSOR_DEVICE_ATTR_2(in13_input, S_IRUGO, show_in_reg, NULL, 13, 0);
  403. static SENSOR_DEVICE_ATTR_2(in14_input, S_IRUGO, show_in_reg, NULL, 14, 0);
  404. static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
  405. static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
  406. static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
  407. static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
  408. static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 4);
  409. static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 5);
  410. static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 6);
  411. static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 7);
  412. static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 8);
  413. static SENSOR_DEVICE_ATTR(in9_alarm, S_IRUGO, show_alarm, NULL, 9);
  414. static SENSOR_DEVICE_ATTR(in10_alarm, S_IRUGO, show_alarm, NULL, 10);
  415. static SENSOR_DEVICE_ATTR(in11_alarm, S_IRUGO, show_alarm, NULL, 11);
  416. static SENSOR_DEVICE_ATTR(in12_alarm, S_IRUGO, show_alarm, NULL, 12);
  417. static SENSOR_DEVICE_ATTR(in13_alarm, S_IRUGO, show_alarm, NULL, 13);
  418. static SENSOR_DEVICE_ATTR(in14_alarm, S_IRUGO, show_alarm, NULL, 14);
  419. static SENSOR_DEVICE_ATTR_2(in0_min, S_IWUSR | S_IRUGO, show_in_reg,
  420. store_in_reg, 0, 1);
  421. static SENSOR_DEVICE_ATTR_2(in1_min, S_IWUSR | S_IRUGO, show_in_reg,
  422. store_in_reg, 1, 1);
  423. static SENSOR_DEVICE_ATTR_2(in2_min, S_IWUSR | S_IRUGO, show_in_reg,
  424. store_in_reg, 2, 1);
  425. static SENSOR_DEVICE_ATTR_2(in3_min, S_IWUSR | S_IRUGO, show_in_reg,
  426. store_in_reg, 3, 1);
  427. static SENSOR_DEVICE_ATTR_2(in4_min, S_IWUSR | S_IRUGO, show_in_reg,
  428. store_in_reg, 4, 1);
  429. static SENSOR_DEVICE_ATTR_2(in5_min, S_IWUSR | S_IRUGO, show_in_reg,
  430. store_in_reg, 5, 1);
  431. static SENSOR_DEVICE_ATTR_2(in6_min, S_IWUSR | S_IRUGO, show_in_reg,
  432. store_in_reg, 6, 1);
  433. static SENSOR_DEVICE_ATTR_2(in7_min, S_IWUSR | S_IRUGO, show_in_reg,
  434. store_in_reg, 7, 1);
  435. static SENSOR_DEVICE_ATTR_2(in8_min, S_IWUSR | S_IRUGO, show_in_reg,
  436. store_in_reg, 8, 1);
  437. static SENSOR_DEVICE_ATTR_2(in9_min, S_IWUSR | S_IRUGO, show_in_reg,
  438. store_in_reg, 9, 1);
  439. static SENSOR_DEVICE_ATTR_2(in10_min, S_IWUSR | S_IRUGO, show_in_reg,
  440. store_in_reg, 10, 1);
  441. static SENSOR_DEVICE_ATTR_2(in11_min, S_IWUSR | S_IRUGO, show_in_reg,
  442. store_in_reg, 11, 1);
  443. static SENSOR_DEVICE_ATTR_2(in12_min, S_IWUSR | S_IRUGO, show_in_reg,
  444. store_in_reg, 12, 1);
  445. static SENSOR_DEVICE_ATTR_2(in13_min, S_IWUSR | S_IRUGO, show_in_reg,
  446. store_in_reg, 13, 1);
  447. static SENSOR_DEVICE_ATTR_2(in14_min, S_IWUSR | S_IRUGO, show_in_reg,
  448. store_in_reg, 14, 1);
  449. static SENSOR_DEVICE_ATTR_2(in0_max, S_IWUSR | S_IRUGO, show_in_reg,
  450. store_in_reg, 0, 2);
  451. static SENSOR_DEVICE_ATTR_2(in1_max, S_IWUSR | S_IRUGO, show_in_reg,
  452. store_in_reg, 1, 2);
  453. static SENSOR_DEVICE_ATTR_2(in2_max, S_IWUSR | S_IRUGO, show_in_reg,
  454. store_in_reg, 2, 2);
  455. static SENSOR_DEVICE_ATTR_2(in3_max, S_IWUSR | S_IRUGO, show_in_reg,
  456. store_in_reg, 3, 2);
  457. static SENSOR_DEVICE_ATTR_2(in4_max, S_IWUSR | S_IRUGO, show_in_reg,
  458. store_in_reg, 4, 2);
  459. static SENSOR_DEVICE_ATTR_2(in5_max, S_IWUSR | S_IRUGO, show_in_reg,
  460. store_in_reg, 5, 2);
  461. static SENSOR_DEVICE_ATTR_2(in6_max, S_IWUSR | S_IRUGO, show_in_reg,
  462. store_in_reg, 6, 2);
  463. static SENSOR_DEVICE_ATTR_2(in7_max, S_IWUSR | S_IRUGO, show_in_reg,
  464. store_in_reg, 7, 2);
  465. static SENSOR_DEVICE_ATTR_2(in8_max, S_IWUSR | S_IRUGO, show_in_reg,
  466. store_in_reg, 8, 2);
  467. static SENSOR_DEVICE_ATTR_2(in9_max, S_IWUSR | S_IRUGO, show_in_reg,
  468. store_in_reg, 9, 2);
  469. static SENSOR_DEVICE_ATTR_2(in10_max, S_IWUSR | S_IRUGO, show_in_reg,
  470. store_in_reg, 10, 2);
  471. static SENSOR_DEVICE_ATTR_2(in11_max, S_IWUSR | S_IRUGO, show_in_reg,
  472. store_in_reg, 11, 2);
  473. static SENSOR_DEVICE_ATTR_2(in12_max, S_IWUSR | S_IRUGO, show_in_reg,
  474. store_in_reg, 12, 2);
  475. static SENSOR_DEVICE_ATTR_2(in13_max, S_IWUSR | S_IRUGO, show_in_reg,
  476. store_in_reg, 13, 2);
  477. static SENSOR_DEVICE_ATTR_2(in14_max, S_IWUSR | S_IRUGO, show_in_reg,
  478. store_in_reg, 14, 2);
  479. static struct attribute *nct6775_attributes_in[15][5] = {
  480. {
  481. &sensor_dev_attr_in0_input.dev_attr.attr,
  482. &sensor_dev_attr_in0_min.dev_attr.attr,
  483. &sensor_dev_attr_in0_max.dev_attr.attr,
  484. &sensor_dev_attr_in0_alarm.dev_attr.attr,
  485. NULL
  486. },
  487. {
  488. &sensor_dev_attr_in1_input.dev_attr.attr,
  489. &sensor_dev_attr_in1_min.dev_attr.attr,
  490. &sensor_dev_attr_in1_max.dev_attr.attr,
  491. &sensor_dev_attr_in1_alarm.dev_attr.attr,
  492. NULL
  493. },
  494. {
  495. &sensor_dev_attr_in2_input.dev_attr.attr,
  496. &sensor_dev_attr_in2_min.dev_attr.attr,
  497. &sensor_dev_attr_in2_max.dev_attr.attr,
  498. &sensor_dev_attr_in2_alarm.dev_attr.attr,
  499. NULL
  500. },
  501. {
  502. &sensor_dev_attr_in3_input.dev_attr.attr,
  503. &sensor_dev_attr_in3_min.dev_attr.attr,
  504. &sensor_dev_attr_in3_max.dev_attr.attr,
  505. &sensor_dev_attr_in3_alarm.dev_attr.attr,
  506. NULL
  507. },
  508. {
  509. &sensor_dev_attr_in4_input.dev_attr.attr,
  510. &sensor_dev_attr_in4_min.dev_attr.attr,
  511. &sensor_dev_attr_in4_max.dev_attr.attr,
  512. &sensor_dev_attr_in4_alarm.dev_attr.attr,
  513. NULL
  514. },
  515. {
  516. &sensor_dev_attr_in5_input.dev_attr.attr,
  517. &sensor_dev_attr_in5_min.dev_attr.attr,
  518. &sensor_dev_attr_in5_max.dev_attr.attr,
  519. &sensor_dev_attr_in5_alarm.dev_attr.attr,
  520. NULL
  521. },
  522. {
  523. &sensor_dev_attr_in6_input.dev_attr.attr,
  524. &sensor_dev_attr_in6_min.dev_attr.attr,
  525. &sensor_dev_attr_in6_max.dev_attr.attr,
  526. &sensor_dev_attr_in6_alarm.dev_attr.attr,
  527. NULL
  528. },
  529. {
  530. &sensor_dev_attr_in7_input.dev_attr.attr,
  531. &sensor_dev_attr_in7_min.dev_attr.attr,
  532. &sensor_dev_attr_in7_max.dev_attr.attr,
  533. &sensor_dev_attr_in7_alarm.dev_attr.attr,
  534. NULL
  535. },
  536. {
  537. &sensor_dev_attr_in8_input.dev_attr.attr,
  538. &sensor_dev_attr_in8_min.dev_attr.attr,
  539. &sensor_dev_attr_in8_max.dev_attr.attr,
  540. &sensor_dev_attr_in8_alarm.dev_attr.attr,
  541. NULL
  542. },
  543. {
  544. &sensor_dev_attr_in9_input.dev_attr.attr,
  545. &sensor_dev_attr_in9_min.dev_attr.attr,
  546. &sensor_dev_attr_in9_max.dev_attr.attr,
  547. &sensor_dev_attr_in9_alarm.dev_attr.attr,
  548. NULL
  549. },
  550. {
  551. &sensor_dev_attr_in10_input.dev_attr.attr,
  552. &sensor_dev_attr_in10_min.dev_attr.attr,
  553. &sensor_dev_attr_in10_max.dev_attr.attr,
  554. &sensor_dev_attr_in10_alarm.dev_attr.attr,
  555. NULL
  556. },
  557. {
  558. &sensor_dev_attr_in11_input.dev_attr.attr,
  559. &sensor_dev_attr_in11_min.dev_attr.attr,
  560. &sensor_dev_attr_in11_max.dev_attr.attr,
  561. &sensor_dev_attr_in11_alarm.dev_attr.attr,
  562. NULL
  563. },
  564. {
  565. &sensor_dev_attr_in12_input.dev_attr.attr,
  566. &sensor_dev_attr_in12_min.dev_attr.attr,
  567. &sensor_dev_attr_in12_max.dev_attr.attr,
  568. &sensor_dev_attr_in12_alarm.dev_attr.attr,
  569. NULL
  570. },
  571. {
  572. &sensor_dev_attr_in13_input.dev_attr.attr,
  573. &sensor_dev_attr_in13_min.dev_attr.attr,
  574. &sensor_dev_attr_in13_max.dev_attr.attr,
  575. &sensor_dev_attr_in13_alarm.dev_attr.attr,
  576. NULL
  577. },
  578. {
  579. &sensor_dev_attr_in14_input.dev_attr.attr,
  580. &sensor_dev_attr_in14_min.dev_attr.attr,
  581. &sensor_dev_attr_in14_max.dev_attr.attr,
  582. &sensor_dev_attr_in14_alarm.dev_attr.attr,
  583. NULL
  584. },
  585. };
  586. static const struct attribute_group nct6775_group_in[15] = {
  587. { .attrs = nct6775_attributes_in[0] },
  588. { .attrs = nct6775_attributes_in[1] },
  589. { .attrs = nct6775_attributes_in[2] },
  590. { .attrs = nct6775_attributes_in[3] },
  591. { .attrs = nct6775_attributes_in[4] },
  592. { .attrs = nct6775_attributes_in[5] },
  593. { .attrs = nct6775_attributes_in[6] },
  594. { .attrs = nct6775_attributes_in[7] },
  595. { .attrs = nct6775_attributes_in[8] },
  596. { .attrs = nct6775_attributes_in[9] },
  597. { .attrs = nct6775_attributes_in[10] },
  598. { .attrs = nct6775_attributes_in[11] },
  599. { .attrs = nct6775_attributes_in[12] },
  600. { .attrs = nct6775_attributes_in[13] },
  601. { .attrs = nct6775_attributes_in[14] },
  602. };
  603. static ssize_t
  604. show_name(struct device *dev, struct device_attribute *attr, char *buf)
  605. {
  606. struct nct6775_data *data = dev_get_drvdata(dev);
  607. return sprintf(buf, "%s\n", data->name);
  608. }
  609. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  610. static ssize_t
  611. show_vid(struct device *dev, struct device_attribute *attr, char *buf)
  612. {
  613. struct nct6775_data *data = dev_get_drvdata(dev);
  614. return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
  615. }
  616. static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
  617. /* Case open detection */
  618. static ssize_t
  619. clear_caseopen(struct device *dev, struct device_attribute *attr,
  620. const char *buf, size_t count)
  621. {
  622. struct nct6775_data *data = dev_get_drvdata(dev);
  623. struct nct6775_sio_data *sio_data = dev->platform_data;
  624. int nr = to_sensor_dev_attr(attr)->index - INTRUSION_ALARM_BASE;
  625. unsigned long val;
  626. u8 reg;
  627. int ret;
  628. if (kstrtoul(buf, 10, &val) || val != 0)
  629. return -EINVAL;
  630. mutex_lock(&data->update_lock);
  631. /*
  632. * Use CR registers to clear caseopen status.
  633. * The CR registers are the same for all chips, and not all chips
  634. * support clearing the caseopen status through "regular" registers.
  635. */
  636. ret = superio_enter(sio_data->sioreg);
  637. if (ret) {
  638. count = ret;
  639. goto error;
  640. }
  641. superio_select(sio_data->sioreg, NCT6775_LD_ACPI);
  642. reg = superio_inb(sio_data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr]);
  643. reg |= NCT6775_CR_CASEOPEN_CLR_MASK[nr];
  644. superio_outb(sio_data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr], reg);
  645. reg &= ~NCT6775_CR_CASEOPEN_CLR_MASK[nr];
  646. superio_outb(sio_data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr], reg);
  647. superio_exit(sio_data->sioreg);
  648. data->valid = false; /* Force cache refresh */
  649. error:
  650. mutex_unlock(&data->update_lock);
  651. return count;
  652. }
  653. static struct sensor_device_attribute sda_caseopen[] = {
  654. SENSOR_ATTR(intrusion0_alarm, S_IWUSR | S_IRUGO, show_alarm,
  655. clear_caseopen, INTRUSION_ALARM_BASE),
  656. SENSOR_ATTR(intrusion1_alarm, S_IWUSR | S_IRUGO, show_alarm,
  657. clear_caseopen, INTRUSION_ALARM_BASE + 1),
  658. };
  659. /*
  660. * Driver and device management
  661. */
  662. static void nct6775_device_remove_files(struct device *dev)
  663. {
  664. /*
  665. * some entries in the following arrays may not have been used in
  666. * device_create_file(), but device_remove_file() will ignore them
  667. */
  668. int i;
  669. struct nct6775_data *data = dev_get_drvdata(dev);
  670. for (i = 0; i < data->in_num; i++)
  671. sysfs_remove_group(&dev->kobj, &nct6775_group_in[i]);
  672. device_remove_file(dev, &sda_caseopen[0].dev_attr);
  673. device_remove_file(dev, &sda_caseopen[1].dev_attr);
  674. device_remove_file(dev, &dev_attr_name);
  675. device_remove_file(dev, &dev_attr_cpu0_vid);
  676. }
  677. /* Get the monitoring functions started */
  678. static inline void nct6775_init_device(struct nct6775_data *data)
  679. {
  680. u8 tmp;
  681. /* Start monitoring if needed */
  682. if (data->REG_CONFIG) {
  683. tmp = nct6775_read_value(data, data->REG_CONFIG);
  684. if (!(tmp & 0x01))
  685. nct6775_write_value(data, data->REG_CONFIG, tmp | 0x01);
  686. }
  687. /* Enable VBAT monitoring if needed */
  688. tmp = nct6775_read_value(data, data->REG_VBAT);
  689. if (!(tmp & 0x01))
  690. nct6775_write_value(data, data->REG_VBAT, tmp | 0x01);
  691. }
  692. static int nct6775_probe(struct platform_device *pdev)
  693. {
  694. struct device *dev = &pdev->dev;
  695. struct nct6775_sio_data *sio_data = dev->platform_data;
  696. struct nct6775_data *data;
  697. struct resource *res;
  698. int i, err = 0;
  699. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  700. if (!devm_request_region(&pdev->dev, res->start, IOREGION_LENGTH,
  701. DRVNAME))
  702. return -EBUSY;
  703. data = devm_kzalloc(&pdev->dev, sizeof(struct nct6775_data),
  704. GFP_KERNEL);
  705. if (!data)
  706. return -ENOMEM;
  707. data->kind = sio_data->kind;
  708. data->addr = res->start;
  709. mutex_init(&data->lock);
  710. mutex_init(&data->update_lock);
  711. data->name = nct6775_device_names[data->kind];
  712. data->bank = 0xff; /* Force initial bank selection */
  713. platform_set_drvdata(pdev, data);
  714. switch (data->kind) {
  715. case nct6775:
  716. data->in_num = 9;
  717. data->ALARM_BITS = NCT6775_ALARM_BITS;
  718. data->REG_CONFIG = NCT6775_REG_CONFIG;
  719. data->REG_VBAT = NCT6775_REG_VBAT;
  720. data->REG_VIN = NCT6775_REG_IN;
  721. data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
  722. data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
  723. data->REG_ALARM = NCT6775_REG_ALARM;
  724. break;
  725. case nct6776:
  726. data->in_num = 9;
  727. data->ALARM_BITS = NCT6776_ALARM_BITS;
  728. data->REG_CONFIG = NCT6775_REG_CONFIG;
  729. data->REG_VBAT = NCT6775_REG_VBAT;
  730. data->REG_VIN = NCT6775_REG_IN;
  731. data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
  732. data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
  733. data->REG_ALARM = NCT6775_REG_ALARM;
  734. break;
  735. case nct6779:
  736. data->in_num = 15;
  737. data->ALARM_BITS = NCT6779_ALARM_BITS;
  738. data->REG_CONFIG = NCT6775_REG_CONFIG;
  739. data->REG_VBAT = NCT6775_REG_VBAT;
  740. data->REG_VIN = NCT6779_REG_IN;
  741. data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
  742. data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
  743. data->REG_ALARM = NCT6779_REG_ALARM;
  744. break;
  745. default:
  746. return -ENODEV;
  747. }
  748. data->have_in = (1 << data->in_num) - 1;
  749. /* Initialize the chip */
  750. nct6775_init_device(data);
  751. data->vrm = vid_which_vrm();
  752. err = superio_enter(sio_data->sioreg);
  753. if (err)
  754. return err;
  755. /*
  756. * Read VID value
  757. * We can get the VID input values directly at logical device D 0xe3.
  758. */
  759. superio_select(sio_data->sioreg, NCT6775_LD_VID);
  760. data->vid = superio_inb(sio_data->sioreg, 0xe3);
  761. superio_exit(sio_data->sioreg);
  762. err = device_create_file(dev, &dev_attr_cpu0_vid);
  763. if (err)
  764. return err;
  765. for (i = 0; i < data->in_num; i++) {
  766. if (!(data->have_in & (1 << i)))
  767. continue;
  768. err = sysfs_create_group(&dev->kobj, &nct6775_group_in[i]);
  769. if (err)
  770. goto exit_remove;
  771. }
  772. for (i = 0; i < ARRAY_SIZE(sda_caseopen); i++) {
  773. if (data->ALARM_BITS[INTRUSION_ALARM_BASE + i] < 0)
  774. continue;
  775. err = device_create_file(dev, &sda_caseopen[i].dev_attr);
  776. if (err)
  777. goto exit_remove;
  778. }
  779. err = device_create_file(dev, &dev_attr_name);
  780. if (err)
  781. goto exit_remove;
  782. data->hwmon_dev = hwmon_device_register(dev);
  783. if (IS_ERR(data->hwmon_dev)) {
  784. err = PTR_ERR(data->hwmon_dev);
  785. goto exit_remove;
  786. }
  787. return 0;
  788. exit_remove:
  789. nct6775_device_remove_files(dev);
  790. return err;
  791. }
  792. static int nct6775_remove(struct platform_device *pdev)
  793. {
  794. struct nct6775_data *data = platform_get_drvdata(pdev);
  795. hwmon_device_unregister(data->hwmon_dev);
  796. nct6775_device_remove_files(&pdev->dev);
  797. return 0;
  798. }
  799. static struct platform_driver nct6775_driver = {
  800. .driver = {
  801. .owner = THIS_MODULE,
  802. .name = DRVNAME,
  803. },
  804. .probe = nct6775_probe,
  805. .remove = nct6775_remove,
  806. };
  807. /* nct6775_find() looks for a '627 in the Super-I/O config space */
  808. static int __init nct6775_find(int sioaddr, unsigned short *addr,
  809. struct nct6775_sio_data *sio_data)
  810. {
  811. static const char sio_name_NCT6775[] __initconst = "NCT6775F";
  812. static const char sio_name_NCT6776[] __initconst = "NCT6776F";
  813. static const char sio_name_NCT6779[] __initconst = "NCT6779D";
  814. u16 val;
  815. const char *sio_name;
  816. int err;
  817. err = superio_enter(sioaddr);
  818. if (err)
  819. return err;
  820. if (force_id)
  821. val = force_id;
  822. else
  823. val = (superio_inb(sioaddr, SIO_REG_DEVID) << 8)
  824. | superio_inb(sioaddr, SIO_REG_DEVID + 1);
  825. switch (val & SIO_ID_MASK) {
  826. case SIO_NCT6775_ID:
  827. sio_data->kind = nct6775;
  828. sio_name = sio_name_NCT6775;
  829. break;
  830. case SIO_NCT6776_ID:
  831. sio_data->kind = nct6776;
  832. sio_name = sio_name_NCT6776;
  833. break;
  834. case SIO_NCT6779_ID:
  835. sio_data->kind = nct6779;
  836. sio_name = sio_name_NCT6779;
  837. break;
  838. default:
  839. if (val != 0xffff)
  840. pr_debug("unsupported chip ID: 0x%04x\n", val);
  841. superio_exit(sioaddr);
  842. return -ENODEV;
  843. }
  844. /* We have a known chip, find the HWM I/O address */
  845. superio_select(sioaddr, NCT6775_LD_HWM);
  846. val = (superio_inb(sioaddr, SIO_REG_ADDR) << 8)
  847. | superio_inb(sioaddr, SIO_REG_ADDR + 1);
  848. *addr = val & IOREGION_ALIGNMENT;
  849. if (*addr == 0) {
  850. pr_err("Refusing to enable a Super-I/O device with a base I/O port 0\n");
  851. superio_exit(sioaddr);
  852. return -ENODEV;
  853. }
  854. /* Activate logical device if needed */
  855. val = superio_inb(sioaddr, SIO_REG_ENABLE);
  856. if (!(val & 0x01)) {
  857. pr_warn("Forcibly enabling Super-I/O. Sensor is probably unusable.\n");
  858. superio_outb(sioaddr, SIO_REG_ENABLE, val | 0x01);
  859. }
  860. superio_exit(sioaddr);
  861. pr_info("Found %s chip at %#x\n", sio_name, *addr);
  862. sio_data->sioreg = sioaddr;
  863. return 0;
  864. }
  865. /*
  866. * when Super-I/O functions move to a separate file, the Super-I/O
  867. * bus will manage the lifetime of the device and this module will only keep
  868. * track of the nct6775 driver. But since we platform_device_alloc(), we
  869. * must keep track of the device
  870. */
  871. static struct platform_device *pdev;
  872. static int __init sensors_nct6775_init(void)
  873. {
  874. int err;
  875. unsigned short address;
  876. struct resource res;
  877. struct nct6775_sio_data sio_data;
  878. /*
  879. * initialize sio_data->kind and sio_data->sioreg.
  880. *
  881. * when Super-I/O functions move to a separate file, the Super-I/O
  882. * driver will probe 0x2e and 0x4e and auto-detect the presence of a
  883. * nct6775 hardware monitor, and call probe()
  884. */
  885. if (nct6775_find(0x2e, &address, &sio_data) &&
  886. nct6775_find(0x4e, &address, &sio_data))
  887. return -ENODEV;
  888. err = platform_driver_register(&nct6775_driver);
  889. if (err)
  890. goto exit;
  891. pdev = platform_device_alloc(DRVNAME, address);
  892. if (!pdev) {
  893. err = -ENOMEM;
  894. pr_err("Device allocation failed\n");
  895. goto exit_unregister;
  896. }
  897. err = platform_device_add_data(pdev, &sio_data,
  898. sizeof(struct nct6775_sio_data));
  899. if (err) {
  900. pr_err("Platform data allocation failed\n");
  901. goto exit_device_put;
  902. }
  903. memset(&res, 0, sizeof(res));
  904. res.name = DRVNAME;
  905. res.start = address + IOREGION_OFFSET;
  906. res.end = address + IOREGION_OFFSET + IOREGION_LENGTH - 1;
  907. res.flags = IORESOURCE_IO;
  908. err = acpi_check_resource_conflict(&res);
  909. if (err)
  910. goto exit_device_put;
  911. err = platform_device_add_resources(pdev, &res, 1);
  912. if (err) {
  913. pr_err("Device resource addition failed (%d)\n", err);
  914. goto exit_device_put;
  915. }
  916. /* platform_device_add calls probe() */
  917. err = platform_device_add(pdev);
  918. if (err) {
  919. pr_err("Device addition failed (%d)\n", err);
  920. goto exit_device_put;
  921. }
  922. return 0;
  923. exit_device_put:
  924. platform_device_put(pdev);
  925. exit_unregister:
  926. platform_driver_unregister(&nct6775_driver);
  927. exit:
  928. return err;
  929. }
  930. static void __exit sensors_nct6775_exit(void)
  931. {
  932. platform_device_unregister(pdev);
  933. platform_driver_unregister(&nct6775_driver);
  934. }
  935. MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
  936. MODULE_DESCRIPTION("NCT6775F/NCT6776F/NCT6779D driver");
  937. MODULE_LICENSE("GPL");
  938. module_init(sensors_nct6775_init);
  939. module_exit(sensors_nct6775_exit);