amc6821.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  1. /*
  2. * amc6821.c - Part of lm_sensors, Linux kernel modules for hardware
  3. * monitoring
  4. * Copyright (C) 2009 T. Mertelj <tomaz.mertelj@guest.arnes.si>
  5. *
  6. * Based on max6650.c:
  7. * Copyright (C) 2007 Hans J. Koch <hjk@hansjkoch.de>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. */
  23. #include <linux/kernel.h> /* Needed for KERN_INFO */
  24. #include <linux/module.h>
  25. #include <linux/init.h>
  26. #include <linux/slab.h>
  27. #include <linux/jiffies.h>
  28. #include <linux/i2c.h>
  29. #include <linux/hwmon.h>
  30. #include <linux/hwmon-sysfs.h>
  31. #include <linux/err.h>
  32. #include <linux/mutex.h>
  33. /*
  34. * Addresses to scan.
  35. */
  36. static const unsigned short normal_i2c[] = {0x18, 0x19, 0x1a, 0x2c, 0x2d, 0x2e,
  37. 0x4c, 0x4d, 0x4e, I2C_CLIENT_END};
  38. /*
  39. * Insmod parameters
  40. */
  41. static int pwminv; /*Inverted PWM output. */
  42. module_param(pwminv, int, S_IRUGO);
  43. static int init = 1; /*Power-on initialization.*/
  44. module_param(init, int, S_IRUGO);
  45. enum chips { amc6821 };
  46. #define AMC6821_REG_DEV_ID 0x3D
  47. #define AMC6821_REG_COMP_ID 0x3E
  48. #define AMC6821_REG_CONF1 0x00
  49. #define AMC6821_REG_CONF2 0x01
  50. #define AMC6821_REG_CONF3 0x3F
  51. #define AMC6821_REG_CONF4 0x04
  52. #define AMC6821_REG_STAT1 0x02
  53. #define AMC6821_REG_STAT2 0x03
  54. #define AMC6821_REG_TDATA_LOW 0x08
  55. #define AMC6821_REG_TDATA_HI 0x09
  56. #define AMC6821_REG_LTEMP_HI 0x0A
  57. #define AMC6821_REG_RTEMP_HI 0x0B
  58. #define AMC6821_REG_LTEMP_LIMIT_MIN 0x15
  59. #define AMC6821_REG_LTEMP_LIMIT_MAX 0x14
  60. #define AMC6821_REG_RTEMP_LIMIT_MIN 0x19
  61. #define AMC6821_REG_RTEMP_LIMIT_MAX 0x18
  62. #define AMC6821_REG_LTEMP_CRIT 0x1B
  63. #define AMC6821_REG_RTEMP_CRIT 0x1D
  64. #define AMC6821_REG_PSV_TEMP 0x1C
  65. #define AMC6821_REG_DCY 0x22
  66. #define AMC6821_REG_LTEMP_FAN_CTRL 0x24
  67. #define AMC6821_REG_RTEMP_FAN_CTRL 0x25
  68. #define AMC6821_REG_DCY_LOW_TEMP 0x21
  69. #define AMC6821_REG_TACH_LLIMITL 0x10
  70. #define AMC6821_REG_TACH_LLIMITH 0x11
  71. #define AMC6821_REG_TACH_HLIMITL 0x12
  72. #define AMC6821_REG_TACH_HLIMITH 0x13
  73. #define AMC6821_CONF1_START 0x01
  74. #define AMC6821_CONF1_FAN_INT_EN 0x02
  75. #define AMC6821_CONF1_FANIE 0x04
  76. #define AMC6821_CONF1_PWMINV 0x08
  77. #define AMC6821_CONF1_FAN_FAULT_EN 0x10
  78. #define AMC6821_CONF1_FDRC0 0x20
  79. #define AMC6821_CONF1_FDRC1 0x40
  80. #define AMC6821_CONF1_THERMOVIE 0x80
  81. #define AMC6821_CONF2_PWM_EN 0x01
  82. #define AMC6821_CONF2_TACH_MODE 0x02
  83. #define AMC6821_CONF2_TACH_EN 0x04
  84. #define AMC6821_CONF2_RTFIE 0x08
  85. #define AMC6821_CONF2_LTOIE 0x10
  86. #define AMC6821_CONF2_RTOIE 0x20
  87. #define AMC6821_CONF2_PSVIE 0x40
  88. #define AMC6821_CONF2_RST 0x80
  89. #define AMC6821_CONF3_THERM_FAN_EN 0x80
  90. #define AMC6821_CONF3_REV_MASK 0x0F
  91. #define AMC6821_CONF4_OVREN 0x10
  92. #define AMC6821_CONF4_TACH_FAST 0x20
  93. #define AMC6821_CONF4_PSPR 0x40
  94. #define AMC6821_CONF4_MODE 0x80
  95. #define AMC6821_STAT1_RPM_ALARM 0x01
  96. #define AMC6821_STAT1_FANS 0x02
  97. #define AMC6821_STAT1_RTH 0x04
  98. #define AMC6821_STAT1_RTL 0x08
  99. #define AMC6821_STAT1_R_THERM 0x10
  100. #define AMC6821_STAT1_RTF 0x20
  101. #define AMC6821_STAT1_LTH 0x40
  102. #define AMC6821_STAT1_LTL 0x80
  103. #define AMC6821_STAT2_RTC 0x08
  104. #define AMC6821_STAT2_LTC 0x10
  105. #define AMC6821_STAT2_LPSV 0x20
  106. #define AMC6821_STAT2_L_THERM 0x40
  107. #define AMC6821_STAT2_THERM_IN 0x80
  108. enum {IDX_TEMP1_INPUT = 0, IDX_TEMP1_MIN, IDX_TEMP1_MAX,
  109. IDX_TEMP1_CRIT, IDX_TEMP2_INPUT, IDX_TEMP2_MIN,
  110. IDX_TEMP2_MAX, IDX_TEMP2_CRIT,
  111. TEMP_IDX_LEN, };
  112. static const u8 temp_reg[] = {AMC6821_REG_LTEMP_HI,
  113. AMC6821_REG_LTEMP_LIMIT_MIN,
  114. AMC6821_REG_LTEMP_LIMIT_MAX,
  115. AMC6821_REG_LTEMP_CRIT,
  116. AMC6821_REG_RTEMP_HI,
  117. AMC6821_REG_RTEMP_LIMIT_MIN,
  118. AMC6821_REG_RTEMP_LIMIT_MAX,
  119. AMC6821_REG_RTEMP_CRIT, };
  120. enum {IDX_FAN1_INPUT = 0, IDX_FAN1_MIN, IDX_FAN1_MAX,
  121. FAN1_IDX_LEN, };
  122. static const u8 fan_reg_low[] = {AMC6821_REG_TDATA_LOW,
  123. AMC6821_REG_TACH_LLIMITL,
  124. AMC6821_REG_TACH_HLIMITL, };
  125. static const u8 fan_reg_hi[] = {AMC6821_REG_TDATA_HI,
  126. AMC6821_REG_TACH_LLIMITH,
  127. AMC6821_REG_TACH_HLIMITH, };
  128. /*
  129. * Client data (each client gets its own)
  130. */
  131. struct amc6821_data {
  132. struct i2c_client *client;
  133. struct mutex update_lock;
  134. char valid; /* zero until following fields are valid */
  135. unsigned long last_updated; /* in jiffies */
  136. /* register values */
  137. int temp[TEMP_IDX_LEN];
  138. u16 fan[FAN1_IDX_LEN];
  139. u8 fan1_div;
  140. u8 pwm1;
  141. u8 temp1_auto_point_temp[3];
  142. u8 temp2_auto_point_temp[3];
  143. u8 pwm1_auto_point_pwm[3];
  144. u8 pwm1_enable;
  145. u8 pwm1_auto_channels_temp;
  146. u8 stat1;
  147. u8 stat2;
  148. };
  149. static struct amc6821_data *amc6821_update_device(struct device *dev)
  150. {
  151. struct amc6821_data *data = dev_get_drvdata(dev);
  152. struct i2c_client *client = data->client;
  153. int timeout = HZ;
  154. u8 reg;
  155. int i;
  156. mutex_lock(&data->update_lock);
  157. if (time_after(jiffies, data->last_updated + timeout) ||
  158. !data->valid) {
  159. for (i = 0; i < TEMP_IDX_LEN; i++)
  160. data->temp[i] = (int8_t)i2c_smbus_read_byte_data(
  161. client, temp_reg[i]);
  162. data->stat1 = i2c_smbus_read_byte_data(client,
  163. AMC6821_REG_STAT1);
  164. data->stat2 = i2c_smbus_read_byte_data(client,
  165. AMC6821_REG_STAT2);
  166. data->pwm1 = i2c_smbus_read_byte_data(client,
  167. AMC6821_REG_DCY);
  168. for (i = 0; i < FAN1_IDX_LEN; i++) {
  169. data->fan[i] = i2c_smbus_read_byte_data(
  170. client,
  171. fan_reg_low[i]);
  172. data->fan[i] += i2c_smbus_read_byte_data(
  173. client,
  174. fan_reg_hi[i]) << 8;
  175. }
  176. data->fan1_div = i2c_smbus_read_byte_data(client,
  177. AMC6821_REG_CONF4);
  178. data->fan1_div = data->fan1_div & AMC6821_CONF4_PSPR ? 4 : 2;
  179. data->pwm1_auto_point_pwm[0] = 0;
  180. data->pwm1_auto_point_pwm[2] = 255;
  181. data->pwm1_auto_point_pwm[1] = i2c_smbus_read_byte_data(client,
  182. AMC6821_REG_DCY_LOW_TEMP);
  183. data->temp1_auto_point_temp[0] =
  184. i2c_smbus_read_byte_data(client,
  185. AMC6821_REG_PSV_TEMP);
  186. data->temp2_auto_point_temp[0] =
  187. data->temp1_auto_point_temp[0];
  188. reg = i2c_smbus_read_byte_data(client,
  189. AMC6821_REG_LTEMP_FAN_CTRL);
  190. data->temp1_auto_point_temp[1] = (reg & 0xF8) >> 1;
  191. reg &= 0x07;
  192. reg = 0x20 >> reg;
  193. if (reg > 0)
  194. data->temp1_auto_point_temp[2] =
  195. data->temp1_auto_point_temp[1] +
  196. (data->pwm1_auto_point_pwm[2] -
  197. data->pwm1_auto_point_pwm[1]) / reg;
  198. else
  199. data->temp1_auto_point_temp[2] = 255;
  200. reg = i2c_smbus_read_byte_data(client,
  201. AMC6821_REG_RTEMP_FAN_CTRL);
  202. data->temp2_auto_point_temp[1] = (reg & 0xF8) >> 1;
  203. reg &= 0x07;
  204. reg = 0x20 >> reg;
  205. if (reg > 0)
  206. data->temp2_auto_point_temp[2] =
  207. data->temp2_auto_point_temp[1] +
  208. (data->pwm1_auto_point_pwm[2] -
  209. data->pwm1_auto_point_pwm[1]) / reg;
  210. else
  211. data->temp2_auto_point_temp[2] = 255;
  212. reg = i2c_smbus_read_byte_data(client, AMC6821_REG_CONF1);
  213. reg = (reg >> 5) & 0x3;
  214. switch (reg) {
  215. case 0: /*open loop: software sets pwm1*/
  216. data->pwm1_auto_channels_temp = 0;
  217. data->pwm1_enable = 1;
  218. break;
  219. case 2: /*closed loop: remote T (temp2)*/
  220. data->pwm1_auto_channels_temp = 2;
  221. data->pwm1_enable = 2;
  222. break;
  223. case 3: /*closed loop: local and remote T (temp2)*/
  224. data->pwm1_auto_channels_temp = 3;
  225. data->pwm1_enable = 3;
  226. break;
  227. case 1: /*
  228. * semi-open loop: software sets rpm, chip controls
  229. * pwm1, currently not implemented
  230. */
  231. data->pwm1_auto_channels_temp = 0;
  232. data->pwm1_enable = 0;
  233. break;
  234. }
  235. data->last_updated = jiffies;
  236. data->valid = 1;
  237. }
  238. mutex_unlock(&data->update_lock);
  239. return data;
  240. }
  241. static ssize_t get_temp(
  242. struct device *dev,
  243. struct device_attribute *devattr,
  244. char *buf)
  245. {
  246. struct amc6821_data *data = amc6821_update_device(dev);
  247. int ix = to_sensor_dev_attr(devattr)->index;
  248. return sprintf(buf, "%d\n", data->temp[ix] * 1000);
  249. }
  250. static ssize_t set_temp(
  251. struct device *dev,
  252. struct device_attribute *attr,
  253. const char *buf,
  254. size_t count)
  255. {
  256. struct amc6821_data *data = dev_get_drvdata(dev);
  257. struct i2c_client *client = data->client;
  258. int ix = to_sensor_dev_attr(attr)->index;
  259. long val;
  260. int ret = kstrtol(buf, 10, &val);
  261. if (ret)
  262. return ret;
  263. val = clamp_val(val / 1000, -128, 127);
  264. mutex_lock(&data->update_lock);
  265. data->temp[ix] = val;
  266. if (i2c_smbus_write_byte_data(client, temp_reg[ix], data->temp[ix])) {
  267. dev_err(&client->dev, "Register write error, aborting.\n");
  268. count = -EIO;
  269. }
  270. mutex_unlock(&data->update_lock);
  271. return count;
  272. }
  273. static ssize_t get_temp_alarm(
  274. struct device *dev,
  275. struct device_attribute *devattr,
  276. char *buf)
  277. {
  278. struct amc6821_data *data = amc6821_update_device(dev);
  279. int ix = to_sensor_dev_attr(devattr)->index;
  280. u8 flag;
  281. switch (ix) {
  282. case IDX_TEMP1_MIN:
  283. flag = data->stat1 & AMC6821_STAT1_LTL;
  284. break;
  285. case IDX_TEMP1_MAX:
  286. flag = data->stat1 & AMC6821_STAT1_LTH;
  287. break;
  288. case IDX_TEMP1_CRIT:
  289. flag = data->stat2 & AMC6821_STAT2_LTC;
  290. break;
  291. case IDX_TEMP2_MIN:
  292. flag = data->stat1 & AMC6821_STAT1_RTL;
  293. break;
  294. case IDX_TEMP2_MAX:
  295. flag = data->stat1 & AMC6821_STAT1_RTH;
  296. break;
  297. case IDX_TEMP2_CRIT:
  298. flag = data->stat2 & AMC6821_STAT2_RTC;
  299. break;
  300. default:
  301. dev_dbg(dev, "Unknown attr->index (%d).\n", ix);
  302. return -EINVAL;
  303. }
  304. if (flag)
  305. return sprintf(buf, "1");
  306. else
  307. return sprintf(buf, "0");
  308. }
  309. static ssize_t get_temp2_fault(
  310. struct device *dev,
  311. struct device_attribute *devattr,
  312. char *buf)
  313. {
  314. struct amc6821_data *data = amc6821_update_device(dev);
  315. if (data->stat1 & AMC6821_STAT1_RTF)
  316. return sprintf(buf, "1");
  317. else
  318. return sprintf(buf, "0");
  319. }
  320. static ssize_t get_pwm1(
  321. struct device *dev,
  322. struct device_attribute *devattr,
  323. char *buf)
  324. {
  325. struct amc6821_data *data = amc6821_update_device(dev);
  326. return sprintf(buf, "%d\n", data->pwm1);
  327. }
  328. static ssize_t set_pwm1(
  329. struct device *dev,
  330. struct device_attribute *devattr,
  331. const char *buf,
  332. size_t count)
  333. {
  334. struct amc6821_data *data = dev_get_drvdata(dev);
  335. struct i2c_client *client = data->client;
  336. long val;
  337. int ret = kstrtol(buf, 10, &val);
  338. if (ret)
  339. return ret;
  340. mutex_lock(&data->update_lock);
  341. data->pwm1 = clamp_val(val , 0, 255);
  342. i2c_smbus_write_byte_data(client, AMC6821_REG_DCY, data->pwm1);
  343. mutex_unlock(&data->update_lock);
  344. return count;
  345. }
  346. static ssize_t get_pwm1_enable(
  347. struct device *dev,
  348. struct device_attribute *devattr,
  349. char *buf)
  350. {
  351. struct amc6821_data *data = amc6821_update_device(dev);
  352. return sprintf(buf, "%d\n", data->pwm1_enable);
  353. }
  354. static ssize_t set_pwm1_enable(
  355. struct device *dev,
  356. struct device_attribute *attr,
  357. const char *buf,
  358. size_t count)
  359. {
  360. struct amc6821_data *data = dev_get_drvdata(dev);
  361. struct i2c_client *client = data->client;
  362. long val;
  363. int config = kstrtol(buf, 10, &val);
  364. if (config)
  365. return config;
  366. mutex_lock(&data->update_lock);
  367. config = i2c_smbus_read_byte_data(client, AMC6821_REG_CONF1);
  368. if (config < 0) {
  369. dev_err(&client->dev,
  370. "Error reading configuration register, aborting.\n");
  371. count = config;
  372. goto unlock;
  373. }
  374. switch (val) {
  375. case 1:
  376. config &= ~AMC6821_CONF1_FDRC0;
  377. config &= ~AMC6821_CONF1_FDRC1;
  378. break;
  379. case 2:
  380. config &= ~AMC6821_CONF1_FDRC0;
  381. config |= AMC6821_CONF1_FDRC1;
  382. break;
  383. case 3:
  384. config |= AMC6821_CONF1_FDRC0;
  385. config |= AMC6821_CONF1_FDRC1;
  386. break;
  387. default:
  388. count = -EINVAL;
  389. goto unlock;
  390. }
  391. if (i2c_smbus_write_byte_data(client, AMC6821_REG_CONF1, config)) {
  392. dev_err(&client->dev,
  393. "Configuration register write error, aborting.\n");
  394. count = -EIO;
  395. }
  396. unlock:
  397. mutex_unlock(&data->update_lock);
  398. return count;
  399. }
  400. static ssize_t get_pwm1_auto_channels_temp(
  401. struct device *dev,
  402. struct device_attribute *devattr,
  403. char *buf)
  404. {
  405. struct amc6821_data *data = amc6821_update_device(dev);
  406. return sprintf(buf, "%d\n", data->pwm1_auto_channels_temp);
  407. }
  408. static ssize_t get_temp_auto_point_temp(
  409. struct device *dev,
  410. struct device_attribute *devattr,
  411. char *buf)
  412. {
  413. int ix = to_sensor_dev_attr_2(devattr)->index;
  414. int nr = to_sensor_dev_attr_2(devattr)->nr;
  415. struct amc6821_data *data = amc6821_update_device(dev);
  416. switch (nr) {
  417. case 1:
  418. return sprintf(buf, "%d\n",
  419. data->temp1_auto_point_temp[ix] * 1000);
  420. case 2:
  421. return sprintf(buf, "%d\n",
  422. data->temp2_auto_point_temp[ix] * 1000);
  423. default:
  424. dev_dbg(dev, "Unknown attr->nr (%d).\n", nr);
  425. return -EINVAL;
  426. }
  427. }
  428. static ssize_t get_pwm1_auto_point_pwm(
  429. struct device *dev,
  430. struct device_attribute *devattr,
  431. char *buf)
  432. {
  433. int ix = to_sensor_dev_attr(devattr)->index;
  434. struct amc6821_data *data = amc6821_update_device(dev);
  435. return sprintf(buf, "%d\n", data->pwm1_auto_point_pwm[ix]);
  436. }
  437. static inline ssize_t set_slope_register(struct i2c_client *client,
  438. u8 reg,
  439. u8 dpwm,
  440. u8 *ptemp)
  441. {
  442. int dt;
  443. u8 tmp;
  444. dt = ptemp[2]-ptemp[1];
  445. for (tmp = 4; tmp > 0; tmp--) {
  446. if (dt * (0x20 >> tmp) >= dpwm)
  447. break;
  448. }
  449. tmp |= (ptemp[1] & 0x7C) << 1;
  450. if (i2c_smbus_write_byte_data(client,
  451. reg, tmp)) {
  452. dev_err(&client->dev, "Register write error, aborting.\n");
  453. return -EIO;
  454. }
  455. return 0;
  456. }
  457. static ssize_t set_temp_auto_point_temp(
  458. struct device *dev,
  459. struct device_attribute *attr,
  460. const char *buf,
  461. size_t count)
  462. {
  463. struct amc6821_data *data = amc6821_update_device(dev);
  464. struct i2c_client *client = data->client;
  465. int ix = to_sensor_dev_attr_2(attr)->index;
  466. int nr = to_sensor_dev_attr_2(attr)->nr;
  467. u8 *ptemp;
  468. u8 reg;
  469. int dpwm;
  470. long val;
  471. int ret = kstrtol(buf, 10, &val);
  472. if (ret)
  473. return ret;
  474. switch (nr) {
  475. case 1:
  476. ptemp = data->temp1_auto_point_temp;
  477. reg = AMC6821_REG_LTEMP_FAN_CTRL;
  478. break;
  479. case 2:
  480. ptemp = data->temp2_auto_point_temp;
  481. reg = AMC6821_REG_RTEMP_FAN_CTRL;
  482. break;
  483. default:
  484. dev_dbg(dev, "Unknown attr->nr (%d).\n", nr);
  485. return -EINVAL;
  486. }
  487. mutex_lock(&data->update_lock);
  488. data->valid = 0;
  489. switch (ix) {
  490. case 0:
  491. ptemp[0] = clamp_val(val / 1000, 0,
  492. data->temp1_auto_point_temp[1]);
  493. ptemp[0] = clamp_val(ptemp[0], 0,
  494. data->temp2_auto_point_temp[1]);
  495. ptemp[0] = clamp_val(ptemp[0], 0, 63);
  496. if (i2c_smbus_write_byte_data(
  497. client,
  498. AMC6821_REG_PSV_TEMP,
  499. ptemp[0])) {
  500. dev_err(&client->dev,
  501. "Register write error, aborting.\n");
  502. count = -EIO;
  503. }
  504. goto EXIT;
  505. case 1:
  506. ptemp[1] = clamp_val(val / 1000, (ptemp[0] & 0x7C) + 4, 124);
  507. ptemp[1] &= 0x7C;
  508. ptemp[2] = clamp_val(ptemp[2], ptemp[1] + 1, 255);
  509. break;
  510. case 2:
  511. ptemp[2] = clamp_val(val / 1000, ptemp[1]+1, 255);
  512. break;
  513. default:
  514. dev_dbg(dev, "Unknown attr->index (%d).\n", ix);
  515. count = -EINVAL;
  516. goto EXIT;
  517. }
  518. dpwm = data->pwm1_auto_point_pwm[2] - data->pwm1_auto_point_pwm[1];
  519. if (set_slope_register(client, reg, dpwm, ptemp))
  520. count = -EIO;
  521. EXIT:
  522. mutex_unlock(&data->update_lock);
  523. return count;
  524. }
  525. static ssize_t set_pwm1_auto_point_pwm(
  526. struct device *dev,
  527. struct device_attribute *attr,
  528. const char *buf,
  529. size_t count)
  530. {
  531. struct amc6821_data *data = dev_get_drvdata(dev);
  532. struct i2c_client *client = data->client;
  533. int dpwm;
  534. long val;
  535. int ret = kstrtol(buf, 10, &val);
  536. if (ret)
  537. return ret;
  538. mutex_lock(&data->update_lock);
  539. data->pwm1_auto_point_pwm[1] = clamp_val(val, 0, 254);
  540. if (i2c_smbus_write_byte_data(client, AMC6821_REG_DCY_LOW_TEMP,
  541. data->pwm1_auto_point_pwm[1])) {
  542. dev_err(&client->dev, "Register write error, aborting.\n");
  543. count = -EIO;
  544. goto EXIT;
  545. }
  546. dpwm = data->pwm1_auto_point_pwm[2] - data->pwm1_auto_point_pwm[1];
  547. if (set_slope_register(client, AMC6821_REG_LTEMP_FAN_CTRL, dpwm,
  548. data->temp1_auto_point_temp)) {
  549. count = -EIO;
  550. goto EXIT;
  551. }
  552. if (set_slope_register(client, AMC6821_REG_RTEMP_FAN_CTRL, dpwm,
  553. data->temp2_auto_point_temp)) {
  554. count = -EIO;
  555. goto EXIT;
  556. }
  557. EXIT:
  558. data->valid = 0;
  559. mutex_unlock(&data->update_lock);
  560. return count;
  561. }
  562. static ssize_t get_fan(
  563. struct device *dev,
  564. struct device_attribute *devattr,
  565. char *buf)
  566. {
  567. struct amc6821_data *data = amc6821_update_device(dev);
  568. int ix = to_sensor_dev_attr(devattr)->index;
  569. if (0 == data->fan[ix])
  570. return sprintf(buf, "0");
  571. return sprintf(buf, "%d\n", (int)(6000000 / data->fan[ix]));
  572. }
  573. static ssize_t get_fan1_fault(
  574. struct device *dev,
  575. struct device_attribute *devattr,
  576. char *buf)
  577. {
  578. struct amc6821_data *data = amc6821_update_device(dev);
  579. if (data->stat1 & AMC6821_STAT1_FANS)
  580. return sprintf(buf, "1");
  581. else
  582. return sprintf(buf, "0");
  583. }
  584. static ssize_t set_fan(
  585. struct device *dev,
  586. struct device_attribute *attr,
  587. const char *buf, size_t count)
  588. {
  589. struct amc6821_data *data = dev_get_drvdata(dev);
  590. struct i2c_client *client = data->client;
  591. long val;
  592. int ix = to_sensor_dev_attr(attr)->index;
  593. int ret = kstrtol(buf, 10, &val);
  594. if (ret)
  595. return ret;
  596. val = 1 > val ? 0xFFFF : 6000000/val;
  597. mutex_lock(&data->update_lock);
  598. data->fan[ix] = (u16) clamp_val(val, 1, 0xFFFF);
  599. if (i2c_smbus_write_byte_data(client, fan_reg_low[ix],
  600. data->fan[ix] & 0xFF)) {
  601. dev_err(&client->dev, "Register write error, aborting.\n");
  602. count = -EIO;
  603. goto EXIT;
  604. }
  605. if (i2c_smbus_write_byte_data(client,
  606. fan_reg_hi[ix], data->fan[ix] >> 8)) {
  607. dev_err(&client->dev, "Register write error, aborting.\n");
  608. count = -EIO;
  609. }
  610. EXIT:
  611. mutex_unlock(&data->update_lock);
  612. return count;
  613. }
  614. static ssize_t get_fan1_div(
  615. struct device *dev,
  616. struct device_attribute *devattr,
  617. char *buf)
  618. {
  619. struct amc6821_data *data = amc6821_update_device(dev);
  620. return sprintf(buf, "%d\n", data->fan1_div);
  621. }
  622. static ssize_t set_fan1_div(
  623. struct device *dev,
  624. struct device_attribute *attr,
  625. const char *buf, size_t count)
  626. {
  627. struct amc6821_data *data = dev_get_drvdata(dev);
  628. struct i2c_client *client = data->client;
  629. long val;
  630. int config = kstrtol(buf, 10, &val);
  631. if (config)
  632. return config;
  633. mutex_lock(&data->update_lock);
  634. config = i2c_smbus_read_byte_data(client, AMC6821_REG_CONF4);
  635. if (config < 0) {
  636. dev_err(&client->dev,
  637. "Error reading configuration register, aborting.\n");
  638. count = config;
  639. goto EXIT;
  640. }
  641. switch (val) {
  642. case 2:
  643. config &= ~AMC6821_CONF4_PSPR;
  644. data->fan1_div = 2;
  645. break;
  646. case 4:
  647. config |= AMC6821_CONF4_PSPR;
  648. data->fan1_div = 4;
  649. break;
  650. default:
  651. count = -EINVAL;
  652. goto EXIT;
  653. }
  654. if (i2c_smbus_write_byte_data(client, AMC6821_REG_CONF4, config)) {
  655. dev_err(&client->dev,
  656. "Configuration register write error, aborting.\n");
  657. count = -EIO;
  658. }
  659. EXIT:
  660. mutex_unlock(&data->update_lock);
  661. return count;
  662. }
  663. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
  664. get_temp, NULL, IDX_TEMP1_INPUT);
  665. static SENSOR_DEVICE_ATTR(temp1_min, S_IRUGO | S_IWUSR, get_temp,
  666. set_temp, IDX_TEMP1_MIN);
  667. static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, get_temp,
  668. set_temp, IDX_TEMP1_MAX);
  669. static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO | S_IWUSR, get_temp,
  670. set_temp, IDX_TEMP1_CRIT);
  671. static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO,
  672. get_temp_alarm, NULL, IDX_TEMP1_MIN);
  673. static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO,
  674. get_temp_alarm, NULL, IDX_TEMP1_MAX);
  675. static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO,
  676. get_temp_alarm, NULL, IDX_TEMP1_CRIT);
  677. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO,
  678. get_temp, NULL, IDX_TEMP2_INPUT);
  679. static SENSOR_DEVICE_ATTR(temp2_min, S_IRUGO | S_IWUSR, get_temp,
  680. set_temp, IDX_TEMP2_MIN);
  681. static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO | S_IWUSR, get_temp,
  682. set_temp, IDX_TEMP2_MAX);
  683. static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO | S_IWUSR, get_temp,
  684. set_temp, IDX_TEMP2_CRIT);
  685. static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO,
  686. get_temp2_fault, NULL, 0);
  687. static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO,
  688. get_temp_alarm, NULL, IDX_TEMP2_MIN);
  689. static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO,
  690. get_temp_alarm, NULL, IDX_TEMP2_MAX);
  691. static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO,
  692. get_temp_alarm, NULL, IDX_TEMP2_CRIT);
  693. static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, get_fan, NULL, IDX_FAN1_INPUT);
  694. static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO | S_IWUSR,
  695. get_fan, set_fan, IDX_FAN1_MIN);
  696. static SENSOR_DEVICE_ATTR(fan1_max, S_IRUGO | S_IWUSR,
  697. get_fan, set_fan, IDX_FAN1_MAX);
  698. static SENSOR_DEVICE_ATTR(fan1_fault, S_IRUGO, get_fan1_fault, NULL, 0);
  699. static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR,
  700. get_fan1_div, set_fan1_div, 0);
  701. static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, get_pwm1, set_pwm1, 0);
  702. static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
  703. get_pwm1_enable, set_pwm1_enable, 0);
  704. static SENSOR_DEVICE_ATTR(pwm1_auto_point1_pwm, S_IRUGO,
  705. get_pwm1_auto_point_pwm, NULL, 0);
  706. static SENSOR_DEVICE_ATTR(pwm1_auto_point2_pwm, S_IWUSR | S_IRUGO,
  707. get_pwm1_auto_point_pwm, set_pwm1_auto_point_pwm, 1);
  708. static SENSOR_DEVICE_ATTR(pwm1_auto_point3_pwm, S_IRUGO,
  709. get_pwm1_auto_point_pwm, NULL, 2);
  710. static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IRUGO,
  711. get_pwm1_auto_channels_temp, NULL, 0);
  712. static SENSOR_DEVICE_ATTR_2(temp1_auto_point1_temp, S_IRUGO,
  713. get_temp_auto_point_temp, NULL, 1, 0);
  714. static SENSOR_DEVICE_ATTR_2(temp1_auto_point2_temp, S_IWUSR | S_IRUGO,
  715. get_temp_auto_point_temp, set_temp_auto_point_temp, 1, 1);
  716. static SENSOR_DEVICE_ATTR_2(temp1_auto_point3_temp, S_IWUSR | S_IRUGO,
  717. get_temp_auto_point_temp, set_temp_auto_point_temp, 1, 2);
  718. static SENSOR_DEVICE_ATTR_2(temp2_auto_point1_temp, S_IWUSR | S_IRUGO,
  719. get_temp_auto_point_temp, set_temp_auto_point_temp, 2, 0);
  720. static SENSOR_DEVICE_ATTR_2(temp2_auto_point2_temp, S_IWUSR | S_IRUGO,
  721. get_temp_auto_point_temp, set_temp_auto_point_temp, 2, 1);
  722. static SENSOR_DEVICE_ATTR_2(temp2_auto_point3_temp, S_IWUSR | S_IRUGO,
  723. get_temp_auto_point_temp, set_temp_auto_point_temp, 2, 2);
  724. static struct attribute *amc6821_attrs[] = {
  725. &sensor_dev_attr_temp1_input.dev_attr.attr,
  726. &sensor_dev_attr_temp1_min.dev_attr.attr,
  727. &sensor_dev_attr_temp1_max.dev_attr.attr,
  728. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  729. &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
  730. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  731. &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
  732. &sensor_dev_attr_temp2_input.dev_attr.attr,
  733. &sensor_dev_attr_temp2_min.dev_attr.attr,
  734. &sensor_dev_attr_temp2_max.dev_attr.attr,
  735. &sensor_dev_attr_temp2_crit.dev_attr.attr,
  736. &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
  737. &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
  738. &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
  739. &sensor_dev_attr_temp2_fault.dev_attr.attr,
  740. &sensor_dev_attr_fan1_input.dev_attr.attr,
  741. &sensor_dev_attr_fan1_min.dev_attr.attr,
  742. &sensor_dev_attr_fan1_max.dev_attr.attr,
  743. &sensor_dev_attr_fan1_fault.dev_attr.attr,
  744. &sensor_dev_attr_fan1_div.dev_attr.attr,
  745. &sensor_dev_attr_pwm1.dev_attr.attr,
  746. &sensor_dev_attr_pwm1_enable.dev_attr.attr,
  747. &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
  748. &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
  749. &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
  750. &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
  751. &sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
  752. &sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr,
  753. &sensor_dev_attr_temp1_auto_point3_temp.dev_attr.attr,
  754. &sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr,
  755. &sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr,
  756. &sensor_dev_attr_temp2_auto_point3_temp.dev_attr.attr,
  757. NULL
  758. };
  759. ATTRIBUTE_GROUPS(amc6821);
  760. /* Return 0 if detection is successful, -ENODEV otherwise */
  761. static int amc6821_detect(
  762. struct i2c_client *client,
  763. struct i2c_board_info *info)
  764. {
  765. struct i2c_adapter *adapter = client->adapter;
  766. int address = client->addr;
  767. int dev_id, comp_id;
  768. dev_dbg(&adapter->dev, "amc6821_detect called.\n");
  769. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
  770. dev_dbg(&adapter->dev,
  771. "amc6821: I2C bus doesn't support byte mode, "
  772. "skipping.\n");
  773. return -ENODEV;
  774. }
  775. dev_id = i2c_smbus_read_byte_data(client, AMC6821_REG_DEV_ID);
  776. comp_id = i2c_smbus_read_byte_data(client, AMC6821_REG_COMP_ID);
  777. if (dev_id != 0x21 || comp_id != 0x49) {
  778. dev_dbg(&adapter->dev,
  779. "amc6821: detection failed at 0x%02x.\n",
  780. address);
  781. return -ENODEV;
  782. }
  783. /*
  784. * Bit 7 of the address register is ignored, so we can check the
  785. * ID registers again
  786. */
  787. dev_id = i2c_smbus_read_byte_data(client, 0x80 | AMC6821_REG_DEV_ID);
  788. comp_id = i2c_smbus_read_byte_data(client, 0x80 | AMC6821_REG_COMP_ID);
  789. if (dev_id != 0x21 || comp_id != 0x49) {
  790. dev_dbg(&adapter->dev,
  791. "amc6821: detection failed at 0x%02x.\n",
  792. address);
  793. return -ENODEV;
  794. }
  795. dev_info(&adapter->dev, "amc6821: chip found at 0x%02x.\n", address);
  796. strlcpy(info->type, "amc6821", I2C_NAME_SIZE);
  797. return 0;
  798. }
  799. static int amc6821_init_client(struct i2c_client *client)
  800. {
  801. int config;
  802. int err = -EIO;
  803. if (init) {
  804. config = i2c_smbus_read_byte_data(client, AMC6821_REG_CONF4);
  805. if (config < 0) {
  806. dev_err(&client->dev,
  807. "Error reading configuration register, aborting.\n");
  808. return err;
  809. }
  810. config |= AMC6821_CONF4_MODE;
  811. if (i2c_smbus_write_byte_data(client, AMC6821_REG_CONF4,
  812. config)) {
  813. dev_err(&client->dev,
  814. "Configuration register write error, aborting.\n");
  815. return err;
  816. }
  817. config = i2c_smbus_read_byte_data(client, AMC6821_REG_CONF3);
  818. if (config < 0) {
  819. dev_err(&client->dev,
  820. "Error reading configuration register, aborting.\n");
  821. return err;
  822. }
  823. dev_info(&client->dev, "Revision %d\n", config & 0x0f);
  824. config &= ~AMC6821_CONF3_THERM_FAN_EN;
  825. if (i2c_smbus_write_byte_data(client, AMC6821_REG_CONF3,
  826. config)) {
  827. dev_err(&client->dev,
  828. "Configuration register write error, aborting.\n");
  829. return err;
  830. }
  831. config = i2c_smbus_read_byte_data(client, AMC6821_REG_CONF2);
  832. if (config < 0) {
  833. dev_err(&client->dev,
  834. "Error reading configuration register, aborting.\n");
  835. return err;
  836. }
  837. config &= ~AMC6821_CONF2_RTFIE;
  838. config &= ~AMC6821_CONF2_LTOIE;
  839. config &= ~AMC6821_CONF2_RTOIE;
  840. if (i2c_smbus_write_byte_data(client,
  841. AMC6821_REG_CONF2, config)) {
  842. dev_err(&client->dev,
  843. "Configuration register write error, aborting.\n");
  844. return err;
  845. }
  846. config = i2c_smbus_read_byte_data(client, AMC6821_REG_CONF1);
  847. if (config < 0) {
  848. dev_err(&client->dev,
  849. "Error reading configuration register, aborting.\n");
  850. return err;
  851. }
  852. config &= ~AMC6821_CONF1_THERMOVIE;
  853. config &= ~AMC6821_CONF1_FANIE;
  854. config |= AMC6821_CONF1_START;
  855. if (pwminv)
  856. config |= AMC6821_CONF1_PWMINV;
  857. else
  858. config &= ~AMC6821_CONF1_PWMINV;
  859. if (i2c_smbus_write_byte_data(
  860. client, AMC6821_REG_CONF1, config)) {
  861. dev_err(&client->dev,
  862. "Configuration register write error, aborting.\n");
  863. return err;
  864. }
  865. }
  866. return 0;
  867. }
  868. static int amc6821_probe(struct i2c_client *client,
  869. const struct i2c_device_id *id)
  870. {
  871. struct device *dev = &client->dev;
  872. struct amc6821_data *data;
  873. struct device *hwmon_dev;
  874. int err;
  875. data = devm_kzalloc(dev, sizeof(struct amc6821_data), GFP_KERNEL);
  876. if (!data)
  877. return -ENOMEM;
  878. data->client = client;
  879. mutex_init(&data->update_lock);
  880. /*
  881. * Initialize the amc6821 chip
  882. */
  883. err = amc6821_init_client(client);
  884. if (err)
  885. return err;
  886. hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
  887. data,
  888. amc6821_groups);
  889. return PTR_ERR_OR_ZERO(hwmon_dev);
  890. }
  891. static const struct i2c_device_id amc6821_id[] = {
  892. { "amc6821", amc6821 },
  893. { }
  894. };
  895. MODULE_DEVICE_TABLE(i2c, amc6821_id);
  896. static struct i2c_driver amc6821_driver = {
  897. .class = I2C_CLASS_HWMON,
  898. .driver = {
  899. .name = "amc6821",
  900. },
  901. .probe = amc6821_probe,
  902. .id_table = amc6821_id,
  903. .detect = amc6821_detect,
  904. .address_list = normal_i2c,
  905. };
  906. module_i2c_driver(amc6821_driver);
  907. MODULE_LICENSE("GPL");
  908. MODULE_AUTHOR("T. Mertelj <tomaz.mertelj@guest.arnes.si>");
  909. MODULE_DESCRIPTION("Texas Instruments amc6821 hwmon driver");