amc6821.c 28 KB

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