nct6683.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458
  1. /*
  2. * nct6683 - Driver for the hardware monitoring functionality of
  3. * Nuvoton NCT6683D eSIO
  4. *
  5. * Copyright (C) 2013 Guenter Roeck <linux@roeck-us.net>
  6. *
  7. * Derived from nct6775 driver
  8. * Copyright (C) 2012, 2013 Guenter Roeck <linux@roeck-us.net>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * Supports the following chips:
  21. *
  22. * Chip #vin #fan #pwm #temp chip ID
  23. * nct6683d 21(1) 16 8 32(1) 0xc730
  24. *
  25. * Notes:
  26. * (1) Total number of vin and temp inputs is 32.
  27. */
  28. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  29. #include <linux/acpi.h>
  30. #include <linux/dmi.h>
  31. #include <linux/err.h>
  32. #include <linux/init.h>
  33. #include <linux/io.h>
  34. #include <linux/jiffies.h>
  35. #include <linux/hwmon.h>
  36. #include <linux/hwmon-sysfs.h>
  37. #include <linux/module.h>
  38. #include <linux/mutex.h>
  39. #include <linux/platform_device.h>
  40. #include <linux/slab.h>
  41. enum kinds { nct6683 };
  42. static bool force;
  43. module_param(force, bool, 0);
  44. MODULE_PARM_DESC(force, "Set to one to enable detection on non-Intel boards");
  45. static const char * const nct6683_device_names[] = {
  46. "nct6683",
  47. };
  48. static const char * const nct6683_chip_names[] = {
  49. "NCT6683D",
  50. };
  51. #define DRVNAME "nct6683"
  52. /*
  53. * Super-I/O constants and functions
  54. */
  55. #define NCT6683_LD_ACPI 0x0a
  56. #define NCT6683_LD_HWM 0x0b
  57. #define NCT6683_LD_VID 0x0d
  58. #define SIO_REG_LDSEL 0x07 /* Logical device select */
  59. #define SIO_REG_DEVID 0x20 /* Device ID (2 bytes) */
  60. #define SIO_REG_ENABLE 0x30 /* Logical device enable */
  61. #define SIO_REG_ADDR 0x60 /* Logical device address (2 bytes) */
  62. #define SIO_NCT6681_ID 0xb270 /* for later */
  63. #define SIO_NCT6683_ID 0xc730
  64. #define SIO_ID_MASK 0xFFF0
  65. static inline void
  66. superio_outb(int ioreg, int reg, int val)
  67. {
  68. outb(reg, ioreg);
  69. outb(val, ioreg + 1);
  70. }
  71. static inline int
  72. superio_inb(int ioreg, int reg)
  73. {
  74. outb(reg, ioreg);
  75. return inb(ioreg + 1);
  76. }
  77. static inline void
  78. superio_select(int ioreg, int ld)
  79. {
  80. outb(SIO_REG_LDSEL, ioreg);
  81. outb(ld, ioreg + 1);
  82. }
  83. static inline int
  84. superio_enter(int ioreg)
  85. {
  86. /*
  87. * Try to reserve <ioreg> and <ioreg + 1> for exclusive access.
  88. */
  89. if (!request_muxed_region(ioreg, 2, DRVNAME))
  90. return -EBUSY;
  91. outb(0x87, ioreg);
  92. outb(0x87, ioreg);
  93. return 0;
  94. }
  95. static inline void
  96. superio_exit(int ioreg)
  97. {
  98. outb(0xaa, ioreg);
  99. outb(0x02, ioreg);
  100. outb(0x02, ioreg + 1);
  101. release_region(ioreg, 2);
  102. }
  103. /*
  104. * ISA constants
  105. */
  106. #define IOREGION_ALIGNMENT (~7)
  107. #define IOREGION_OFFSET 4 /* Use EC port 1 */
  108. #define IOREGION_LENGTH 4
  109. #define EC_PAGE_REG 0
  110. #define EC_INDEX_REG 1
  111. #define EC_DATA_REG 2
  112. #define EC_EVENT_REG 3
  113. /* Common and NCT6683 specific data */
  114. #define NCT6683_NUM_REG_MON 32
  115. #define NCT6683_NUM_REG_FAN 16
  116. #define NCT6683_NUM_REG_PWM 8
  117. #define NCT6683_REG_MON(x) (0x100 + (x) * 2)
  118. #define NCT6683_REG_FAN_RPM(x) (0x140 + (x) * 2)
  119. #define NCT6683_REG_PWM(x) (0x160 + (x))
  120. #define NCT6683_REG_MON_STS(x) (0x174 + (x))
  121. #define NCT6683_REG_IDLE(x) (0x178 + (x))
  122. #define NCT6683_REG_FAN_STS(x) (0x17c + (x))
  123. #define NCT6683_REG_FAN_ERRSTS 0x17e
  124. #define NCT6683_REG_FAN_INITSTS 0x17f
  125. #define NCT6683_HWM_CFG 0x180
  126. #define NCT6683_REG_MON_CFG(x) (0x1a0 + (x))
  127. #define NCT6683_REG_FANIN_CFG(x) (0x1c0 + (x))
  128. #define NCT6683_REG_FANOUT_CFG(x) (0x1d0 + (x))
  129. #define NCT6683_REG_INTEL_TEMP_MAX(x) (0x901 + (x) * 16)
  130. #define NCT6683_REG_INTEL_TEMP_CRIT(x) (0x90d + (x) * 16)
  131. #define NCT6683_REG_TEMP_HYST(x) (0x330 + (x)) /* 8 bit */
  132. #define NCT6683_REG_TEMP_MAX(x) (0x350 + (x)) /* 8 bit */
  133. #define NCT6683_REG_MON_HIGH(x) (0x370 + (x) * 2) /* 8 bit */
  134. #define NCT6683_REG_MON_LOW(x) (0x371 + (x) * 2) /* 8 bit */
  135. #define NCT6683_REG_FAN_MIN(x) (0x3b8 + (x) * 2) /* 16 bit */
  136. #define NCT6683_REG_CUSTOMER_ID 0x602
  137. #define NCT6683_CUSTOMER_ID_INTEL 0x805
  138. #define NCT6683_REG_BUILD_YEAR 0x604
  139. #define NCT6683_REG_BUILD_MONTH 0x605
  140. #define NCT6683_REG_BUILD_DAY 0x606
  141. #define NCT6683_REG_SERIAL 0x607
  142. #define NCT6683_REG_VERSION_HI 0x608
  143. #define NCT6683_REG_VERSION_LO 0x609
  144. #define NCT6683_REG_CR_CASEOPEN 0xe8
  145. #define NCT6683_CR_CASEOPEN_MASK (1 << 7)
  146. #define NCT6683_REG_CR_BEEP 0xe0
  147. #define NCT6683_CR_BEEP_MASK (1 << 6)
  148. static const char *const nct6683_mon_label[] = {
  149. NULL, /* disabled */
  150. "Local",
  151. "Diode 0 (curr)",
  152. "Diode 1 (curr)",
  153. "Diode 2 (curr)",
  154. "Diode 0 (volt)",
  155. "Diode 1 (volt)",
  156. "Diode 2 (volt)",
  157. "Thermistor 14",
  158. "Thermistor 15",
  159. "Thermistor 16",
  160. "Thermistor 0",
  161. "Thermistor 1",
  162. "Thermistor 2",
  163. "Thermistor 3",
  164. "Thermistor 4",
  165. "Thermistor 5", /* 0x10 */
  166. "Thermistor 6",
  167. "Thermistor 7",
  168. "Thermistor 8",
  169. "Thermistor 9",
  170. "Thermistor 10",
  171. "Thermistor 11",
  172. "Thermistor 12",
  173. "Thermistor 13",
  174. NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  175. "PECI 0.0", /* 0x20 */
  176. "PECI 1.0",
  177. "PECI 2.0",
  178. "PECI 3.0",
  179. "PECI 0.1",
  180. "PECI 1.1",
  181. "PECI 2.1",
  182. "PECI 3.1",
  183. "PECI DIMM 0",
  184. "PECI DIMM 1",
  185. "PECI DIMM 2",
  186. "PECI DIMM 3",
  187. NULL, NULL, NULL, NULL,
  188. "PCH CPU", /* 0x30 */
  189. "PCH CHIP",
  190. "PCH CHIP CPU MAX",
  191. "PCH MCH",
  192. "PCH DIMM 0",
  193. "PCH DIMM 1",
  194. "PCH DIMM 2",
  195. "PCH DIMM 3",
  196. "SMBus 0",
  197. "SMBus 1",
  198. "SMBus 2",
  199. "SMBus 3",
  200. "SMBus 4",
  201. "SMBus 5",
  202. "DIMM 0",
  203. "DIMM 1",
  204. "DIMM 2", /* 0x40 */
  205. "DIMM 3",
  206. "AMD TSI Addr 90h",
  207. "AMD TSI Addr 92h",
  208. "AMD TSI Addr 94h",
  209. "AMD TSI Addr 96h",
  210. "AMD TSI Addr 98h",
  211. "AMD TSI Addr 9ah",
  212. "AMD TSI Addr 9ch",
  213. "AMD TSI Addr 9dh",
  214. NULL, NULL, NULL, NULL, NULL, NULL,
  215. "Virtual 0", /* 0x50 */
  216. "Virtual 1",
  217. "Virtual 2",
  218. "Virtual 3",
  219. "Virtual 4",
  220. "Virtual 5",
  221. "Virtual 6",
  222. "Virtual 7",
  223. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  224. "VCC", /* 0x60 voltage sensors */
  225. "VSB",
  226. "AVSB",
  227. "VTT",
  228. "VBAT",
  229. "VREF",
  230. "VIN0",
  231. "VIN1",
  232. "VIN2",
  233. "VIN3",
  234. "VIN4",
  235. "VIN5",
  236. "VIN6",
  237. "VIN7",
  238. "VIN8",
  239. "VIN9",
  240. "VIN10",
  241. "VIN11",
  242. "VIN12",
  243. "VIN13",
  244. "VIN14",
  245. "VIN15",
  246. "VIN16",
  247. };
  248. #define NUM_MON_LABELS ARRAY_SIZE(nct6683_mon_label)
  249. #define MON_VOLTAGE_START 0x60
  250. /* ------------------------------------------------------- */
  251. struct nct6683_data {
  252. int addr; /* IO base of EC space */
  253. int sioreg; /* SIO register */
  254. enum kinds kind;
  255. u16 customer_id;
  256. struct device *hwmon_dev;
  257. const struct attribute_group *groups[6];
  258. int temp_num; /* number of temperature attributes */
  259. u8 temp_index[NCT6683_NUM_REG_MON];
  260. u8 temp_src[NCT6683_NUM_REG_MON];
  261. u8 in_num; /* number of voltage attributes */
  262. u8 in_index[NCT6683_NUM_REG_MON];
  263. u8 in_src[NCT6683_NUM_REG_MON];
  264. struct mutex update_lock; /* used to protect sensor updates */
  265. bool valid; /* true if following fields are valid */
  266. unsigned long last_updated; /* In jiffies */
  267. /* Voltage attribute values */
  268. u8 in[3][NCT6683_NUM_REG_MON]; /* [0]=in, [1]=in_max, [2]=in_min */
  269. /* Temperature attribute values */
  270. s16 temp_in[NCT6683_NUM_REG_MON];
  271. s8 temp[4][NCT6683_NUM_REG_MON];/* [0]=min, [1]=max, [2]=hyst,
  272. * [3]=crit
  273. */
  274. /* Fan attribute values */
  275. unsigned int rpm[NCT6683_NUM_REG_FAN];
  276. u16 fan_min[NCT6683_NUM_REG_FAN];
  277. u8 fanin_cfg[NCT6683_NUM_REG_FAN];
  278. u8 fanout_cfg[NCT6683_NUM_REG_FAN];
  279. u16 have_fan; /* some fan inputs can be disabled */
  280. u8 have_pwm;
  281. u8 pwm[NCT6683_NUM_REG_PWM];
  282. #ifdef CONFIG_PM
  283. /* Remember extra register values over suspend/resume */
  284. u8 hwm_cfg;
  285. #endif
  286. };
  287. struct nct6683_sio_data {
  288. int sioreg;
  289. enum kinds kind;
  290. };
  291. struct sensor_device_template {
  292. struct device_attribute dev_attr;
  293. union {
  294. struct {
  295. u8 nr;
  296. u8 index;
  297. } s;
  298. int index;
  299. } u;
  300. bool s2; /* true if both index and nr are used */
  301. };
  302. struct sensor_device_attr_u {
  303. union {
  304. struct sensor_device_attribute a1;
  305. struct sensor_device_attribute_2 a2;
  306. } u;
  307. char name[32];
  308. };
  309. #define __TEMPLATE_ATTR(_template, _mode, _show, _store) { \
  310. .attr = {.name = _template, .mode = _mode }, \
  311. .show = _show, \
  312. .store = _store, \
  313. }
  314. #define SENSOR_DEVICE_TEMPLATE(_template, _mode, _show, _store, _index) \
  315. { .dev_attr = __TEMPLATE_ATTR(_template, _mode, _show, _store), \
  316. .u.index = _index, \
  317. .s2 = false }
  318. #define SENSOR_DEVICE_TEMPLATE_2(_template, _mode, _show, _store, \
  319. _nr, _index) \
  320. { .dev_attr = __TEMPLATE_ATTR(_template, _mode, _show, _store), \
  321. .u.s.index = _index, \
  322. .u.s.nr = _nr, \
  323. .s2 = true }
  324. #define SENSOR_TEMPLATE(_name, _template, _mode, _show, _store, _index) \
  325. static struct sensor_device_template sensor_dev_template_##_name \
  326. = SENSOR_DEVICE_TEMPLATE(_template, _mode, _show, _store, \
  327. _index)
  328. #define SENSOR_TEMPLATE_2(_name, _template, _mode, _show, _store, \
  329. _nr, _index) \
  330. static struct sensor_device_template sensor_dev_template_##_name \
  331. = SENSOR_DEVICE_TEMPLATE_2(_template, _mode, _show, _store, \
  332. _nr, _index)
  333. struct sensor_template_group {
  334. struct sensor_device_template **templates;
  335. umode_t (*is_visible)(struct kobject *, struct attribute *, int);
  336. int base;
  337. };
  338. static struct attribute_group *
  339. nct6683_create_attr_group(struct device *dev, struct sensor_template_group *tg,
  340. int repeat)
  341. {
  342. struct sensor_device_attribute_2 *a2;
  343. struct sensor_device_attribute *a;
  344. struct sensor_device_template **t;
  345. struct sensor_device_attr_u *su;
  346. struct attribute_group *group;
  347. struct attribute **attrs;
  348. int i, j, count;
  349. if (repeat <= 0)
  350. return ERR_PTR(-EINVAL);
  351. t = tg->templates;
  352. for (count = 0; *t; t++, count++)
  353. ;
  354. if (count == 0)
  355. return ERR_PTR(-EINVAL);
  356. group = devm_kzalloc(dev, sizeof(*group), GFP_KERNEL);
  357. if (group == NULL)
  358. return ERR_PTR(-ENOMEM);
  359. attrs = devm_kzalloc(dev, sizeof(*attrs) * (repeat * count + 1),
  360. GFP_KERNEL);
  361. if (attrs == NULL)
  362. return ERR_PTR(-ENOMEM);
  363. su = devm_kzalloc(dev, sizeof(*su) * repeat * count,
  364. GFP_KERNEL);
  365. if (su == NULL)
  366. return ERR_PTR(-ENOMEM);
  367. group->attrs = attrs;
  368. group->is_visible = tg->is_visible;
  369. for (i = 0; i < repeat; i++) {
  370. t = tg->templates;
  371. for (j = 0; *t != NULL; j++) {
  372. snprintf(su->name, sizeof(su->name),
  373. (*t)->dev_attr.attr.name, tg->base + i);
  374. if ((*t)->s2) {
  375. a2 = &su->u.a2;
  376. sysfs_attr_init(&a2->dev_attr.attr);
  377. a2->dev_attr.attr.name = su->name;
  378. a2->nr = (*t)->u.s.nr + i;
  379. a2->index = (*t)->u.s.index;
  380. a2->dev_attr.attr.mode =
  381. (*t)->dev_attr.attr.mode;
  382. a2->dev_attr.show = (*t)->dev_attr.show;
  383. a2->dev_attr.store = (*t)->dev_attr.store;
  384. *attrs = &a2->dev_attr.attr;
  385. } else {
  386. a = &su->u.a1;
  387. sysfs_attr_init(&a->dev_attr.attr);
  388. a->dev_attr.attr.name = su->name;
  389. a->index = (*t)->u.index + i;
  390. a->dev_attr.attr.mode =
  391. (*t)->dev_attr.attr.mode;
  392. a->dev_attr.show = (*t)->dev_attr.show;
  393. a->dev_attr.store = (*t)->dev_attr.store;
  394. *attrs = &a->dev_attr.attr;
  395. }
  396. attrs++;
  397. su++;
  398. t++;
  399. }
  400. }
  401. return group;
  402. }
  403. /* LSB is 16 mV, except for the following sources, where it is 32 mV */
  404. #define MON_SRC_VCC 0x60
  405. #define MON_SRC_VSB 0x61
  406. #define MON_SRC_AVSB 0x62
  407. #define MON_SRC_VBAT 0x64
  408. static inline long in_from_reg(u16 reg, u8 src)
  409. {
  410. int scale = 16;
  411. if (src == MON_SRC_VCC || src == MON_SRC_VSB || src == MON_SRC_AVSB ||
  412. src == MON_SRC_VBAT)
  413. scale <<= 1;
  414. return reg * scale;
  415. }
  416. static inline u16 in_to_reg(u32 val, u8 src)
  417. {
  418. int scale = 16;
  419. if (src == MON_SRC_VCC || src == MON_SRC_VSB || src == MON_SRC_AVSB ||
  420. src == MON_SRC_VBAT)
  421. scale <<= 1;
  422. return clamp_val(DIV_ROUND_CLOSEST(val, scale), 0, 127);
  423. }
  424. static u16 nct6683_read(struct nct6683_data *data, u16 reg)
  425. {
  426. int res;
  427. outb_p(0xff, data->addr + EC_PAGE_REG); /* unlock */
  428. outb_p(reg >> 8, data->addr + EC_PAGE_REG);
  429. outb_p(reg & 0xff, data->addr + EC_INDEX_REG);
  430. res = inb_p(data->addr + EC_DATA_REG);
  431. return res;
  432. }
  433. static u16 nct6683_read16(struct nct6683_data *data, u16 reg)
  434. {
  435. return (nct6683_read(data, reg) << 8) | nct6683_read(data, reg + 1);
  436. }
  437. static void nct6683_write(struct nct6683_data *data, u16 reg, u16 value)
  438. {
  439. outb_p(0xff, data->addr + EC_PAGE_REG); /* unlock */
  440. outb_p(reg >> 8, data->addr + EC_PAGE_REG);
  441. outb_p(reg & 0xff, data->addr + EC_INDEX_REG);
  442. outb_p(value & 0xff, data->addr + EC_DATA_REG);
  443. }
  444. static int get_in_reg(struct nct6683_data *data, int nr, int index)
  445. {
  446. int ch = data->in_index[index];
  447. int reg = -EINVAL;
  448. switch (nr) {
  449. case 0:
  450. reg = NCT6683_REG_MON(ch);
  451. break;
  452. case 1:
  453. if (data->customer_id != NCT6683_CUSTOMER_ID_INTEL)
  454. reg = NCT6683_REG_MON_LOW(ch);
  455. break;
  456. case 2:
  457. if (data->customer_id != NCT6683_CUSTOMER_ID_INTEL)
  458. reg = NCT6683_REG_MON_HIGH(ch);
  459. break;
  460. default:
  461. break;
  462. }
  463. return reg;
  464. }
  465. static int get_temp_reg(struct nct6683_data *data, int nr, int index)
  466. {
  467. int ch = data->temp_index[index];
  468. int reg = -EINVAL;
  469. switch (data->customer_id) {
  470. case NCT6683_CUSTOMER_ID_INTEL:
  471. switch (nr) {
  472. default:
  473. case 1: /* max */
  474. reg = NCT6683_REG_INTEL_TEMP_MAX(ch);
  475. break;
  476. case 3: /* crit */
  477. reg = NCT6683_REG_INTEL_TEMP_CRIT(ch);
  478. break;
  479. }
  480. break;
  481. default:
  482. switch (nr) {
  483. default:
  484. case 0: /* min */
  485. reg = NCT6683_REG_MON_LOW(ch);
  486. break;
  487. case 1: /* max */
  488. reg = NCT6683_REG_TEMP_MAX(ch);
  489. break;
  490. case 2: /* hyst */
  491. reg = NCT6683_REG_TEMP_HYST(ch);
  492. break;
  493. case 3: /* crit */
  494. reg = NCT6683_REG_MON_HIGH(ch);
  495. break;
  496. }
  497. break;
  498. }
  499. return reg;
  500. }
  501. static void nct6683_update_pwm(struct device *dev)
  502. {
  503. struct nct6683_data *data = dev_get_drvdata(dev);
  504. int i;
  505. for (i = 0; i < NCT6683_NUM_REG_PWM; i++) {
  506. if (!(data->have_pwm & (1 << i)))
  507. continue;
  508. data->pwm[i] = nct6683_read(data, NCT6683_REG_PWM(i));
  509. }
  510. }
  511. static struct nct6683_data *nct6683_update_device(struct device *dev)
  512. {
  513. struct nct6683_data *data = dev_get_drvdata(dev);
  514. int i, j;
  515. mutex_lock(&data->update_lock);
  516. if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
  517. /* Measured voltages and limits */
  518. for (i = 0; i < data->in_num; i++) {
  519. for (j = 0; j < 3; j++) {
  520. int reg = get_in_reg(data, j, i);
  521. if (reg >= 0)
  522. data->in[j][i] =
  523. nct6683_read(data, reg);
  524. }
  525. }
  526. /* Measured temperatures and limits */
  527. for (i = 0; i < data->temp_num; i++) {
  528. u8 ch = data->temp_index[i];
  529. data->temp_in[i] = nct6683_read16(data,
  530. NCT6683_REG_MON(ch));
  531. for (j = 0; j < 4; j++) {
  532. int reg = get_temp_reg(data, j, i);
  533. if (reg >= 0)
  534. data->temp[j][i] =
  535. nct6683_read(data, reg);
  536. }
  537. }
  538. /* Measured fan speeds and limits */
  539. for (i = 0; i < ARRAY_SIZE(data->rpm); i++) {
  540. if (!(data->have_fan & (1 << i)))
  541. continue;
  542. data->rpm[i] = nct6683_read16(data,
  543. NCT6683_REG_FAN_RPM(i));
  544. data->fan_min[i] = nct6683_read16(data,
  545. NCT6683_REG_FAN_MIN(i));
  546. }
  547. nct6683_update_pwm(dev);
  548. data->last_updated = jiffies;
  549. data->valid = true;
  550. }
  551. mutex_unlock(&data->update_lock);
  552. return data;
  553. }
  554. /*
  555. * Sysfs callback functions
  556. */
  557. static ssize_t
  558. show_in_label(struct device *dev, struct device_attribute *attr, char *buf)
  559. {
  560. struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
  561. struct nct6683_data *data = nct6683_update_device(dev);
  562. int nr = sattr->index;
  563. return sprintf(buf, "%s\n", nct6683_mon_label[data->in_src[nr]]);
  564. }
  565. static ssize_t
  566. show_in_reg(struct device *dev, struct device_attribute *attr, char *buf)
  567. {
  568. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  569. struct nct6683_data *data = nct6683_update_device(dev);
  570. int index = sattr->index;
  571. int nr = sattr->nr;
  572. return sprintf(buf, "%ld\n",
  573. in_from_reg(data->in[index][nr], data->in_index[index]));
  574. }
  575. static umode_t nct6683_in_is_visible(struct kobject *kobj,
  576. struct attribute *attr, int index)
  577. {
  578. struct device *dev = container_of(kobj, struct device, kobj);
  579. struct nct6683_data *data = dev_get_drvdata(dev);
  580. int nr = index % 4; /* attribute */
  581. /*
  582. * Voltage limits exist for Intel boards,
  583. * but register location and encoding is unknown
  584. */
  585. if ((nr == 2 || nr == 3) &&
  586. data->customer_id == NCT6683_CUSTOMER_ID_INTEL)
  587. return 0;
  588. return attr->mode;
  589. }
  590. SENSOR_TEMPLATE(in_label, "in%d_label", S_IRUGO, show_in_label, NULL, 0);
  591. SENSOR_TEMPLATE_2(in_input, "in%d_input", S_IRUGO, show_in_reg, NULL, 0, 0);
  592. SENSOR_TEMPLATE_2(in_min, "in%d_min", S_IRUGO, show_in_reg, NULL, 0, 1);
  593. SENSOR_TEMPLATE_2(in_max, "in%d_max", S_IRUGO, show_in_reg, NULL, 0, 2);
  594. static struct sensor_device_template *nct6683_attributes_in_template[] = {
  595. &sensor_dev_template_in_label,
  596. &sensor_dev_template_in_input,
  597. &sensor_dev_template_in_min,
  598. &sensor_dev_template_in_max,
  599. NULL
  600. };
  601. static struct sensor_template_group nct6683_in_template_group = {
  602. .templates = nct6683_attributes_in_template,
  603. .is_visible = nct6683_in_is_visible,
  604. };
  605. static ssize_t
  606. show_fan(struct device *dev, struct device_attribute *attr, char *buf)
  607. {
  608. struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
  609. struct nct6683_data *data = nct6683_update_device(dev);
  610. return sprintf(buf, "%d\n", data->rpm[sattr->index]);
  611. }
  612. static ssize_t
  613. show_fan_min(struct device *dev, struct device_attribute *attr, char *buf)
  614. {
  615. struct nct6683_data *data = nct6683_update_device(dev);
  616. struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
  617. int nr = sattr->index;
  618. return sprintf(buf, "%d\n", data->fan_min[nr]);
  619. }
  620. static ssize_t
  621. show_fan_pulses(struct device *dev, struct device_attribute *attr, char *buf)
  622. {
  623. struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
  624. struct nct6683_data *data = nct6683_update_device(dev);
  625. return sprintf(buf, "%d\n",
  626. ((data->fanin_cfg[sattr->index] >> 5) & 0x03) + 1);
  627. }
  628. static umode_t nct6683_fan_is_visible(struct kobject *kobj,
  629. struct attribute *attr, int index)
  630. {
  631. struct device *dev = container_of(kobj, struct device, kobj);
  632. struct nct6683_data *data = dev_get_drvdata(dev);
  633. int fan = index / 3; /* fan index */
  634. int nr = index % 3; /* attribute index */
  635. if (!(data->have_fan & (1 << fan)))
  636. return 0;
  637. /*
  638. * Intel may have minimum fan speed limits,
  639. * but register location and encoding are unknown.
  640. */
  641. if (nr == 2 && data->customer_id == NCT6683_CUSTOMER_ID_INTEL)
  642. return 0;
  643. return attr->mode;
  644. }
  645. SENSOR_TEMPLATE(fan_input, "fan%d_input", S_IRUGO, show_fan, NULL, 0);
  646. SENSOR_TEMPLATE(fan_pulses, "fan%d_pulses", S_IRUGO, show_fan_pulses, NULL, 0);
  647. SENSOR_TEMPLATE(fan_min, "fan%d_min", S_IRUGO, show_fan_min, NULL, 0);
  648. /*
  649. * nct6683_fan_is_visible uses the index into the following array
  650. * to determine if attributes should be created or not.
  651. * Any change in order or content must be matched.
  652. */
  653. static struct sensor_device_template *nct6683_attributes_fan_template[] = {
  654. &sensor_dev_template_fan_input,
  655. &sensor_dev_template_fan_pulses,
  656. &sensor_dev_template_fan_min,
  657. NULL
  658. };
  659. static struct sensor_template_group nct6683_fan_template_group = {
  660. .templates = nct6683_attributes_fan_template,
  661. .is_visible = nct6683_fan_is_visible,
  662. .base = 1,
  663. };
  664. static ssize_t
  665. show_temp_label(struct device *dev, struct device_attribute *attr, char *buf)
  666. {
  667. struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
  668. struct nct6683_data *data = nct6683_update_device(dev);
  669. int nr = sattr->index;
  670. return sprintf(buf, "%s\n", nct6683_mon_label[data->temp_src[nr]]);
  671. }
  672. static ssize_t
  673. show_temp8(struct device *dev, struct device_attribute *attr, char *buf)
  674. {
  675. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  676. struct nct6683_data *data = nct6683_update_device(dev);
  677. int index = sattr->index;
  678. int nr = sattr->nr;
  679. return sprintf(buf, "%d\n", data->temp[index][nr] * 1000);
  680. }
  681. static ssize_t
  682. show_temp_hyst(struct device *dev, struct device_attribute *attr, char *buf)
  683. {
  684. struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
  685. struct nct6683_data *data = nct6683_update_device(dev);
  686. int nr = sattr->index;
  687. int temp = data->temp[1][nr] - data->temp[2][nr];
  688. return sprintf(buf, "%d\n", temp * 1000);
  689. }
  690. static ssize_t
  691. show_temp16(struct device *dev, struct device_attribute *attr, char *buf)
  692. {
  693. struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
  694. struct nct6683_data *data = nct6683_update_device(dev);
  695. int index = sattr->index;
  696. return sprintf(buf, "%d\n", (data->temp_in[index] / 128) * 500);
  697. }
  698. /*
  699. * Temperature sensor type is determined by temperature source
  700. * and can not be modified.
  701. * 0x02..0x07: Thermal diode
  702. * 0x08..0x18: Thermistor
  703. * 0x20..0x2b: Intel PECI
  704. * 0x42..0x49: AMD TSI
  705. * Others are unspecified (not visible)
  706. */
  707. static int get_temp_type(u8 src)
  708. {
  709. if (src >= 0x02 && src <= 0x07)
  710. return 3; /* thermal diode */
  711. else if (src >= 0x08 && src <= 0x18)
  712. return 4; /* thermistor */
  713. else if (src >= 0x20 && src <= 0x2b)
  714. return 6; /* PECI */
  715. else if (src >= 0x42 && src <= 0x49)
  716. return 5;
  717. return 0;
  718. }
  719. static ssize_t
  720. show_temp_type(struct device *dev, struct device_attribute *attr, char *buf)
  721. {
  722. struct nct6683_data *data = nct6683_update_device(dev);
  723. struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
  724. int nr = sattr->index;
  725. return sprintf(buf, "%d\n", get_temp_type(data->temp_src[nr]));
  726. }
  727. static umode_t nct6683_temp_is_visible(struct kobject *kobj,
  728. struct attribute *attr, int index)
  729. {
  730. struct device *dev = container_of(kobj, struct device, kobj);
  731. struct nct6683_data *data = dev_get_drvdata(dev);
  732. int temp = index / 7; /* temp index */
  733. int nr = index % 7; /* attribute index */
  734. /*
  735. * Intel does not have low temperature limits or temperature hysteresis
  736. * registers, or at least register location and encoding is unknown.
  737. */
  738. if ((nr == 2 || nr == 4) &&
  739. data->customer_id == NCT6683_CUSTOMER_ID_INTEL)
  740. return 0;
  741. if (nr == 6 && get_temp_type(data->temp_src[temp]) == 0)
  742. return 0; /* type */
  743. return attr->mode;
  744. }
  745. SENSOR_TEMPLATE(temp_input, "temp%d_input", S_IRUGO, show_temp16, NULL, 0);
  746. SENSOR_TEMPLATE(temp_label, "temp%d_label", S_IRUGO, show_temp_label, NULL, 0);
  747. SENSOR_TEMPLATE_2(temp_min, "temp%d_min", S_IRUGO, show_temp8, NULL, 0, 0);
  748. SENSOR_TEMPLATE_2(temp_max, "temp%d_max", S_IRUGO, show_temp8, NULL, 0, 1);
  749. SENSOR_TEMPLATE(temp_max_hyst, "temp%d_max_hyst", S_IRUGO, show_temp_hyst, NULL,
  750. 0);
  751. SENSOR_TEMPLATE_2(temp_crit, "temp%d_crit", S_IRUGO, show_temp8, NULL, 0, 3);
  752. SENSOR_TEMPLATE(temp_type, "temp%d_type", S_IRUGO, show_temp_type, NULL, 0);
  753. /*
  754. * nct6683_temp_is_visible uses the index into the following array
  755. * to determine if attributes should be created or not.
  756. * Any change in order or content must be matched.
  757. */
  758. static struct sensor_device_template *nct6683_attributes_temp_template[] = {
  759. &sensor_dev_template_temp_input,
  760. &sensor_dev_template_temp_label,
  761. &sensor_dev_template_temp_min, /* 2 */
  762. &sensor_dev_template_temp_max, /* 3 */
  763. &sensor_dev_template_temp_max_hyst, /* 4 */
  764. &sensor_dev_template_temp_crit, /* 5 */
  765. &sensor_dev_template_temp_type, /* 6 */
  766. NULL
  767. };
  768. static struct sensor_template_group nct6683_temp_template_group = {
  769. .templates = nct6683_attributes_temp_template,
  770. .is_visible = nct6683_temp_is_visible,
  771. .base = 1,
  772. };
  773. static ssize_t
  774. show_pwm(struct device *dev, struct device_attribute *attr, char *buf)
  775. {
  776. struct nct6683_data *data = nct6683_update_device(dev);
  777. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  778. int index = sattr->index;
  779. return sprintf(buf, "%d\n", data->pwm[index]);
  780. }
  781. SENSOR_TEMPLATE(pwm, "pwm%d", S_IRUGO, show_pwm, NULL, 0);
  782. static umode_t nct6683_pwm_is_visible(struct kobject *kobj,
  783. struct attribute *attr, int index)
  784. {
  785. struct device *dev = container_of(kobj, struct device, kobj);
  786. struct nct6683_data *data = dev_get_drvdata(dev);
  787. int pwm = index; /* pwm index */
  788. if (!(data->have_pwm & (1 << pwm)))
  789. return 0;
  790. return attr->mode;
  791. }
  792. static struct sensor_device_template *nct6683_attributes_pwm_template[] = {
  793. &sensor_dev_template_pwm,
  794. NULL
  795. };
  796. static struct sensor_template_group nct6683_pwm_template_group = {
  797. .templates = nct6683_attributes_pwm_template,
  798. .is_visible = nct6683_pwm_is_visible,
  799. .base = 1,
  800. };
  801. static ssize_t
  802. show_global_beep(struct device *dev, struct device_attribute *attr, char *buf)
  803. {
  804. struct nct6683_data *data = dev_get_drvdata(dev);
  805. int ret;
  806. u8 reg;
  807. mutex_lock(&data->update_lock);
  808. ret = superio_enter(data->sioreg);
  809. if (ret)
  810. goto error;
  811. superio_select(data->sioreg, NCT6683_LD_HWM);
  812. reg = superio_inb(data->sioreg, NCT6683_REG_CR_BEEP);
  813. superio_exit(data->sioreg);
  814. mutex_unlock(&data->update_lock);
  815. return sprintf(buf, "%u\n", !!(reg & NCT6683_CR_BEEP_MASK));
  816. error:
  817. mutex_unlock(&data->update_lock);
  818. return ret;
  819. }
  820. static ssize_t
  821. store_global_beep(struct device *dev, struct device_attribute *attr,
  822. const char *buf, size_t count)
  823. {
  824. struct nct6683_data *data = dev_get_drvdata(dev);
  825. unsigned long val;
  826. u8 reg;
  827. int ret;
  828. if (kstrtoul(buf, 10, &val) || (val != 0 && val != 1))
  829. return -EINVAL;
  830. mutex_lock(&data->update_lock);
  831. ret = superio_enter(data->sioreg);
  832. if (ret) {
  833. count = ret;
  834. goto error;
  835. }
  836. superio_select(data->sioreg, NCT6683_LD_HWM);
  837. reg = superio_inb(data->sioreg, NCT6683_REG_CR_BEEP);
  838. if (val)
  839. reg |= NCT6683_CR_BEEP_MASK;
  840. else
  841. reg &= ~NCT6683_CR_BEEP_MASK;
  842. superio_outb(data->sioreg, NCT6683_REG_CR_BEEP, reg);
  843. superio_exit(data->sioreg);
  844. error:
  845. mutex_unlock(&data->update_lock);
  846. return count;
  847. }
  848. /* Case open detection */
  849. static ssize_t
  850. show_caseopen(struct device *dev, struct device_attribute *attr, char *buf)
  851. {
  852. struct nct6683_data *data = dev_get_drvdata(dev);
  853. int ret;
  854. u8 reg;
  855. mutex_lock(&data->update_lock);
  856. ret = superio_enter(data->sioreg);
  857. if (ret)
  858. goto error;
  859. superio_select(data->sioreg, NCT6683_LD_ACPI);
  860. reg = superio_inb(data->sioreg, NCT6683_REG_CR_CASEOPEN);
  861. superio_exit(data->sioreg);
  862. mutex_unlock(&data->update_lock);
  863. return sprintf(buf, "%u\n", !(reg & NCT6683_CR_CASEOPEN_MASK));
  864. error:
  865. mutex_unlock(&data->update_lock);
  866. return ret;
  867. }
  868. static ssize_t
  869. clear_caseopen(struct device *dev, struct device_attribute *attr,
  870. const char *buf, size_t count)
  871. {
  872. struct nct6683_data *data = dev_get_drvdata(dev);
  873. unsigned long val;
  874. u8 reg;
  875. int ret;
  876. if (kstrtoul(buf, 10, &val) || val != 0)
  877. return -EINVAL;
  878. mutex_lock(&data->update_lock);
  879. /*
  880. * Use CR registers to clear caseopen status.
  881. * Caseopen is activ low, clear by writing 1 into the register.
  882. */
  883. ret = superio_enter(data->sioreg);
  884. if (ret) {
  885. count = ret;
  886. goto error;
  887. }
  888. superio_select(data->sioreg, NCT6683_LD_ACPI);
  889. reg = superio_inb(data->sioreg, NCT6683_REG_CR_CASEOPEN);
  890. reg |= NCT6683_CR_CASEOPEN_MASK;
  891. superio_outb(data->sioreg, NCT6683_REG_CR_CASEOPEN, reg);
  892. reg &= ~NCT6683_CR_CASEOPEN_MASK;
  893. superio_outb(data->sioreg, NCT6683_REG_CR_CASEOPEN, reg);
  894. superio_exit(data->sioreg);
  895. data->valid = false; /* Force cache refresh */
  896. error:
  897. mutex_unlock(&data->update_lock);
  898. return count;
  899. }
  900. static DEVICE_ATTR(intrusion0_alarm, S_IWUSR | S_IRUGO, show_caseopen,
  901. clear_caseopen);
  902. static DEVICE_ATTR(beep_enable, S_IWUSR | S_IRUGO, show_global_beep,
  903. store_global_beep);
  904. static struct attribute *nct6683_attributes_other[] = {
  905. &dev_attr_intrusion0_alarm.attr,
  906. &dev_attr_beep_enable.attr,
  907. NULL
  908. };
  909. static const struct attribute_group nct6683_group_other = {
  910. .attrs = nct6683_attributes_other,
  911. };
  912. /* Get the monitoring functions started */
  913. static inline void nct6683_init_device(struct nct6683_data *data)
  914. {
  915. u8 tmp;
  916. /* Start hardware monitoring if needed */
  917. tmp = nct6683_read(data, NCT6683_HWM_CFG);
  918. if (!(tmp & 0x80))
  919. nct6683_write(data, NCT6683_HWM_CFG, tmp | 0x80);
  920. }
  921. /*
  922. * There are a total of 24 fan inputs. Each can be configured as input
  923. * or as output. A maximum of 16 inputs and 8 outputs is configurable.
  924. */
  925. static void
  926. nct6683_setup_fans(struct nct6683_data *data)
  927. {
  928. int i;
  929. u8 reg;
  930. for (i = 0; i < NCT6683_NUM_REG_FAN; i++) {
  931. reg = nct6683_read(data, NCT6683_REG_FANIN_CFG(i));
  932. if (reg & 0x80)
  933. data->have_fan |= 1 << i;
  934. data->fanin_cfg[i] = reg;
  935. }
  936. for (i = 0; i < NCT6683_NUM_REG_PWM; i++) {
  937. reg = nct6683_read(data, NCT6683_REG_FANOUT_CFG(i));
  938. if (reg & 0x80)
  939. data->have_pwm |= 1 << i;
  940. data->fanout_cfg[i] = reg;
  941. }
  942. }
  943. /*
  944. * Translation from monitoring register to temperature and voltage attributes
  945. * ==========================================================================
  946. *
  947. * There are a total of 32 monitoring registers. Each can be assigned to either
  948. * a temperature or voltage monitoring source.
  949. * NCT6683_REG_MON_CFG(x) defines assignment for each monitoring source.
  950. *
  951. * Temperature and voltage attribute mapping is determined by walking through
  952. * the NCT6683_REG_MON_CFG registers. If the assigned source is
  953. * a temperature, temp_index[n] is set to the monitor register index, and
  954. * temp_src[n] is set to the temperature source. If the assigned source is
  955. * a voltage, the respective values are stored in in_index[] and in_src[],
  956. * respectively.
  957. */
  958. static void nct6683_setup_sensors(struct nct6683_data *data)
  959. {
  960. u8 reg;
  961. int i;
  962. data->temp_num = 0;
  963. data->in_num = 0;
  964. for (i = 0; i < NCT6683_NUM_REG_MON; i++) {
  965. reg = nct6683_read(data, NCT6683_REG_MON_CFG(i)) & 0x7f;
  966. /* Ignore invalid assignments */
  967. if (reg >= NUM_MON_LABELS)
  968. continue;
  969. /* Skip if disabled or reserved */
  970. if (nct6683_mon_label[reg] == NULL)
  971. continue;
  972. if (reg < MON_VOLTAGE_START) {
  973. data->temp_index[data->temp_num] = i;
  974. data->temp_src[data->temp_num] = reg;
  975. data->temp_num++;
  976. } else {
  977. data->in_index[data->in_num] = i;
  978. data->in_src[data->in_num] = reg;
  979. data->in_num++;
  980. }
  981. }
  982. }
  983. static int nct6683_probe(struct platform_device *pdev)
  984. {
  985. struct device *dev = &pdev->dev;
  986. struct nct6683_sio_data *sio_data = dev->platform_data;
  987. struct attribute_group *group;
  988. struct nct6683_data *data;
  989. struct device *hwmon_dev;
  990. struct resource *res;
  991. int groups = 0;
  992. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  993. if (!devm_request_region(dev, res->start, IOREGION_LENGTH, DRVNAME))
  994. return -EBUSY;
  995. data = devm_kzalloc(dev, sizeof(struct nct6683_data), GFP_KERNEL);
  996. if (!data)
  997. return -ENOMEM;
  998. data->kind = sio_data->kind;
  999. data->sioreg = sio_data->sioreg;
  1000. data->addr = res->start;
  1001. mutex_init(&data->update_lock);
  1002. platform_set_drvdata(pdev, data);
  1003. data->customer_id = nct6683_read16(data, NCT6683_REG_CUSTOMER_ID);
  1004. nct6683_init_device(data);
  1005. nct6683_setup_fans(data);
  1006. nct6683_setup_sensors(data);
  1007. /* Register sysfs hooks */
  1008. if (data->have_pwm) {
  1009. group = nct6683_create_attr_group(dev,
  1010. &nct6683_pwm_template_group,
  1011. fls(data->have_pwm));
  1012. if (IS_ERR(group))
  1013. return PTR_ERR(group);
  1014. data->groups[groups++] = group;
  1015. }
  1016. if (data->in_num) {
  1017. group = nct6683_create_attr_group(dev,
  1018. &nct6683_in_template_group,
  1019. data->in_num);
  1020. if (IS_ERR(group))
  1021. return PTR_ERR(group);
  1022. data->groups[groups++] = group;
  1023. }
  1024. if (data->have_fan) {
  1025. group = nct6683_create_attr_group(dev,
  1026. &nct6683_fan_template_group,
  1027. fls(data->have_fan));
  1028. if (IS_ERR(group))
  1029. return PTR_ERR(group);
  1030. data->groups[groups++] = group;
  1031. }
  1032. if (data->temp_num) {
  1033. group = nct6683_create_attr_group(dev,
  1034. &nct6683_temp_template_group,
  1035. data->temp_num);
  1036. if (IS_ERR(group))
  1037. return PTR_ERR(group);
  1038. data->groups[groups++] = group;
  1039. }
  1040. data->groups[groups++] = &nct6683_group_other;
  1041. dev_info(dev, "%s EC firmware version %d.%d build %02x/%02x/%02x\n",
  1042. nct6683_chip_names[data->kind],
  1043. nct6683_read(data, NCT6683_REG_VERSION_HI),
  1044. nct6683_read(data, NCT6683_REG_VERSION_LO),
  1045. nct6683_read(data, NCT6683_REG_BUILD_MONTH),
  1046. nct6683_read(data, NCT6683_REG_BUILD_DAY),
  1047. nct6683_read(data, NCT6683_REG_BUILD_YEAR));
  1048. hwmon_dev = devm_hwmon_device_register_with_groups(dev,
  1049. nct6683_device_names[data->kind], data, data->groups);
  1050. return PTR_ERR_OR_ZERO(hwmon_dev);
  1051. }
  1052. #ifdef CONFIG_PM
  1053. static int nct6683_suspend(struct device *dev)
  1054. {
  1055. struct nct6683_data *data = nct6683_update_device(dev);
  1056. mutex_lock(&data->update_lock);
  1057. data->hwm_cfg = nct6683_read(data, NCT6683_HWM_CFG);
  1058. mutex_unlock(&data->update_lock);
  1059. return 0;
  1060. }
  1061. static int nct6683_resume(struct device *dev)
  1062. {
  1063. struct nct6683_data *data = dev_get_drvdata(dev);
  1064. mutex_lock(&data->update_lock);
  1065. nct6683_write(data, NCT6683_HWM_CFG, data->hwm_cfg);
  1066. /* Force re-reading all values */
  1067. data->valid = false;
  1068. mutex_unlock(&data->update_lock);
  1069. return 0;
  1070. }
  1071. static const struct dev_pm_ops nct6683_dev_pm_ops = {
  1072. .suspend = nct6683_suspend,
  1073. .resume = nct6683_resume,
  1074. .freeze = nct6683_suspend,
  1075. .restore = nct6683_resume,
  1076. };
  1077. #define NCT6683_DEV_PM_OPS (&nct6683_dev_pm_ops)
  1078. #else
  1079. #define NCT6683_DEV_PM_OPS NULL
  1080. #endif /* CONFIG_PM */
  1081. static struct platform_driver nct6683_driver = {
  1082. .driver = {
  1083. .name = DRVNAME,
  1084. .pm = NCT6683_DEV_PM_OPS,
  1085. },
  1086. .probe = nct6683_probe,
  1087. };
  1088. static int __init nct6683_find(int sioaddr, struct nct6683_sio_data *sio_data)
  1089. {
  1090. const char *board_vendor;
  1091. int addr;
  1092. u16 val;
  1093. int err;
  1094. /*
  1095. * Only run on Intel boards unless the 'force' module parameter is set
  1096. */
  1097. if (!force) {
  1098. board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
  1099. if (!board_vendor || strcmp(board_vendor, "Intel Corporation"))
  1100. return -ENODEV;
  1101. }
  1102. err = superio_enter(sioaddr);
  1103. if (err)
  1104. return err;
  1105. val = (superio_inb(sioaddr, SIO_REG_DEVID) << 8)
  1106. | superio_inb(sioaddr, SIO_REG_DEVID + 1);
  1107. switch (val & SIO_ID_MASK) {
  1108. case SIO_NCT6683_ID:
  1109. sio_data->kind = nct6683;
  1110. break;
  1111. default:
  1112. if (val != 0xffff)
  1113. pr_debug("unsupported chip ID: 0x%04x\n", val);
  1114. goto fail;
  1115. }
  1116. /* We have a known chip, find the HWM I/O address */
  1117. superio_select(sioaddr, NCT6683_LD_HWM);
  1118. val = (superio_inb(sioaddr, SIO_REG_ADDR) << 8)
  1119. | superio_inb(sioaddr, SIO_REG_ADDR + 1);
  1120. addr = val & IOREGION_ALIGNMENT;
  1121. if (addr == 0) {
  1122. pr_err("EC base I/O port unconfigured\n");
  1123. goto fail;
  1124. }
  1125. /* Activate logical device if needed */
  1126. val = superio_inb(sioaddr, SIO_REG_ENABLE);
  1127. if (!(val & 0x01)) {
  1128. pr_err("EC is disabled\n");
  1129. goto fail;
  1130. }
  1131. superio_exit(sioaddr);
  1132. pr_info("Found %s or compatible chip at %#x:%#x\n",
  1133. nct6683_chip_names[sio_data->kind], sioaddr, addr);
  1134. sio_data->sioreg = sioaddr;
  1135. return addr;
  1136. fail:
  1137. superio_exit(sioaddr);
  1138. return -ENODEV;
  1139. }
  1140. /*
  1141. * when Super-I/O functions move to a separate file, the Super-I/O
  1142. * bus will manage the lifetime of the device and this module will only keep
  1143. * track of the nct6683 driver. But since we use platform_device_alloc(), we
  1144. * must keep track of the device
  1145. */
  1146. static struct platform_device *pdev[2];
  1147. static int __init sensors_nct6683_init(void)
  1148. {
  1149. struct nct6683_sio_data sio_data;
  1150. int sioaddr[2] = { 0x2e, 0x4e };
  1151. struct resource res;
  1152. bool found = false;
  1153. int address;
  1154. int i, err;
  1155. err = platform_driver_register(&nct6683_driver);
  1156. if (err)
  1157. return err;
  1158. /*
  1159. * initialize sio_data->kind and sio_data->sioreg.
  1160. *
  1161. * when Super-I/O functions move to a separate file, the Super-I/O
  1162. * driver will probe 0x2e and 0x4e and auto-detect the presence of a
  1163. * nct6683 hardware monitor, and call probe()
  1164. */
  1165. for (i = 0; i < ARRAY_SIZE(pdev); i++) {
  1166. address = nct6683_find(sioaddr[i], &sio_data);
  1167. if (address <= 0)
  1168. continue;
  1169. found = true;
  1170. pdev[i] = platform_device_alloc(DRVNAME, address);
  1171. if (!pdev[i]) {
  1172. err = -ENOMEM;
  1173. goto exit_device_unregister;
  1174. }
  1175. err = platform_device_add_data(pdev[i], &sio_data,
  1176. sizeof(struct nct6683_sio_data));
  1177. if (err)
  1178. goto exit_device_put;
  1179. memset(&res, 0, sizeof(res));
  1180. res.name = DRVNAME;
  1181. res.start = address + IOREGION_OFFSET;
  1182. res.end = address + IOREGION_OFFSET + IOREGION_LENGTH - 1;
  1183. res.flags = IORESOURCE_IO;
  1184. err = acpi_check_resource_conflict(&res);
  1185. if (err) {
  1186. platform_device_put(pdev[i]);
  1187. pdev[i] = NULL;
  1188. continue;
  1189. }
  1190. err = platform_device_add_resources(pdev[i], &res, 1);
  1191. if (err)
  1192. goto exit_device_put;
  1193. /* platform_device_add calls probe() */
  1194. err = platform_device_add(pdev[i]);
  1195. if (err)
  1196. goto exit_device_put;
  1197. }
  1198. if (!found) {
  1199. err = -ENODEV;
  1200. goto exit_unregister;
  1201. }
  1202. return 0;
  1203. exit_device_put:
  1204. platform_device_put(pdev[i]);
  1205. exit_device_unregister:
  1206. while (--i >= 0) {
  1207. if (pdev[i])
  1208. platform_device_unregister(pdev[i]);
  1209. }
  1210. exit_unregister:
  1211. platform_driver_unregister(&nct6683_driver);
  1212. return err;
  1213. }
  1214. static void __exit sensors_nct6683_exit(void)
  1215. {
  1216. int i;
  1217. for (i = 0; i < ARRAY_SIZE(pdev); i++) {
  1218. if (pdev[i])
  1219. platform_device_unregister(pdev[i]);
  1220. }
  1221. platform_driver_unregister(&nct6683_driver);
  1222. }
  1223. MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
  1224. MODULE_DESCRIPTION("NCT6683D driver");
  1225. MODULE_LICENSE("GPL");
  1226. module_init(sensors_nct6683_init);
  1227. module_exit(sensors_nct6683_exit);