ftsteutates.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  1. /*
  2. * Support for the FTS Systemmonitoring Chip "Teutates"
  3. *
  4. * Copyright (C) 2016 Fujitsu Technology Solutions GmbH,
  5. * Thilo Cestonaro <[email protected]>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. */
  18. #include <linux/err.h>
  19. #include <linux/fs.h>
  20. #include <linux/hwmon.h>
  21. #include <linux/hwmon-sysfs.h>
  22. #include <linux/i2c.h>
  23. #include <linux/init.h>
  24. #include <linux/jiffies.h>
  25. #include <linux/module.h>
  26. #include <linux/mutex.h>
  27. #include <linux/slab.h>
  28. #include <linux/sysfs.h>
  29. #include <linux/uaccess.h>
  30. #include <linux/watchdog.h>
  31. #define FTS_DEVICE_ID_REG 0x0000
  32. #define FTS_DEVICE_REVISION_REG 0x0001
  33. #define FTS_DEVICE_STATUS_REG 0x0004
  34. #define FTS_SATELLITE_STATUS_REG 0x0005
  35. #define FTS_EVENT_STATUS_REG 0x0006
  36. #define FTS_GLOBAL_CONTROL_REG 0x0007
  37. #define FTS_DEVICE_DETECT_REG_1 0x0C
  38. #define FTS_DEVICE_DETECT_REG_2 0x0D
  39. #define FTS_DEVICE_DETECT_REG_3 0x0E
  40. #define FTS_SENSOR_EVENT_REG 0x0010
  41. #define FTS_FAN_EVENT_REG 0x0014
  42. #define FTS_FAN_PRESENT_REG 0x0015
  43. #define FTS_POWER_ON_TIME_COUNTER_A 0x007A
  44. #define FTS_POWER_ON_TIME_COUNTER_B 0x007B
  45. #define FTS_POWER_ON_TIME_COUNTER_C 0x007C
  46. #define FTS_PAGE_SELECT_REG 0x007F
  47. #define FTS_WATCHDOG_TIME_PRESET 0x000B
  48. #define FTS_WATCHDOG_CONTROL 0x5081
  49. #define FTS_NO_FAN_SENSORS 0x08
  50. #define FTS_NO_TEMP_SENSORS 0x10
  51. #define FTS_NO_VOLT_SENSORS 0x04
  52. static const unsigned short normal_i2c[] = { 0x73, I2C_CLIENT_END };
  53. static struct i2c_device_id fts_id[] = {
  54. { "ftsteutates", 0 },
  55. { }
  56. };
  57. MODULE_DEVICE_TABLE(i2c, fts_id);
  58. enum WATCHDOG_RESOLUTION {
  59. seconds = 1,
  60. minutes = 60
  61. };
  62. struct fts_data {
  63. struct i2c_client *client;
  64. /* update sensor data lock */
  65. struct mutex update_lock;
  66. /* read/write register lock */
  67. struct mutex access_lock;
  68. unsigned long last_updated; /* in jiffies */
  69. struct watchdog_device wdd;
  70. enum WATCHDOG_RESOLUTION resolution;
  71. bool valid; /* false until following fields are valid */
  72. u8 volt[FTS_NO_VOLT_SENSORS];
  73. u8 temp_input[FTS_NO_TEMP_SENSORS];
  74. u8 temp_alarm;
  75. u8 fan_present;
  76. u8 fan_input[FTS_NO_FAN_SENSORS]; /* in rps */
  77. u8 fan_source[FTS_NO_FAN_SENSORS];
  78. u8 fan_alarm;
  79. };
  80. #define FTS_REG_FAN_INPUT(idx) ((idx) + 0x20)
  81. #define FTS_REG_FAN_SOURCE(idx) ((idx) + 0x30)
  82. #define FTS_REG_FAN_CONTROL(idx) (((idx) << 16) + 0x4881)
  83. #define FTS_REG_TEMP_INPUT(idx) ((idx) + 0x40)
  84. #define FTS_REG_TEMP_CONTROL(idx) (((idx) << 16) + 0x0681)
  85. #define FTS_REG_VOLT(idx) ((idx) + 0x18)
  86. /*****************************************************************************/
  87. /* I2C Helper functions */
  88. /*****************************************************************************/
  89. static int fts_read_byte(struct i2c_client *client, unsigned short reg)
  90. {
  91. int ret;
  92. unsigned char page = reg >> 8;
  93. struct fts_data *data = dev_get_drvdata(&client->dev);
  94. mutex_lock(&data->access_lock);
  95. dev_dbg(&client->dev, "page select - page: 0x%.02x\n", page);
  96. ret = i2c_smbus_write_byte_data(client, FTS_PAGE_SELECT_REG, page);
  97. if (ret < 0)
  98. goto error;
  99. reg &= 0xFF;
  100. ret = i2c_smbus_read_byte_data(client, reg);
  101. dev_dbg(&client->dev, "read - reg: 0x%.02x: val: 0x%.02x\n", reg, ret);
  102. error:
  103. mutex_unlock(&data->access_lock);
  104. return ret;
  105. }
  106. static int fts_write_byte(struct i2c_client *client, unsigned short reg,
  107. unsigned char value)
  108. {
  109. int ret;
  110. unsigned char page = reg >> 8;
  111. struct fts_data *data = dev_get_drvdata(&client->dev);
  112. mutex_lock(&data->access_lock);
  113. dev_dbg(&client->dev, "page select - page: 0x%.02x\n", page);
  114. ret = i2c_smbus_write_byte_data(client, FTS_PAGE_SELECT_REG, page);
  115. if (ret < 0)
  116. goto error;
  117. reg &= 0xFF;
  118. dev_dbg(&client->dev,
  119. "write - reg: 0x%.02x: val: 0x%.02x\n", reg, value);
  120. ret = i2c_smbus_write_byte_data(client, reg, value);
  121. error:
  122. mutex_unlock(&data->access_lock);
  123. return ret;
  124. }
  125. /*****************************************************************************/
  126. /* Data Updater Helper function */
  127. /*****************************************************************************/
  128. static int fts_update_device(struct fts_data *data)
  129. {
  130. int i;
  131. int err = 0;
  132. mutex_lock(&data->update_lock);
  133. if (!time_after(jiffies, data->last_updated + 2 * HZ) && data->valid)
  134. goto exit;
  135. err = fts_read_byte(data->client, FTS_DEVICE_STATUS_REG);
  136. if (err < 0)
  137. goto exit;
  138. data->valid = !!(err & 0x02); /* Data not ready yet */
  139. if (unlikely(!data->valid)) {
  140. err = -EAGAIN;
  141. goto exit;
  142. }
  143. err = fts_read_byte(data->client, FTS_FAN_PRESENT_REG);
  144. if (err < 0)
  145. goto exit;
  146. data->fan_present = err;
  147. err = fts_read_byte(data->client, FTS_FAN_EVENT_REG);
  148. if (err < 0)
  149. goto exit;
  150. data->fan_alarm = err;
  151. for (i = 0; i < FTS_NO_FAN_SENSORS; i++) {
  152. if (data->fan_present & BIT(i)) {
  153. err = fts_read_byte(data->client, FTS_REG_FAN_INPUT(i));
  154. if (err < 0)
  155. goto exit;
  156. data->fan_input[i] = err;
  157. err = fts_read_byte(data->client,
  158. FTS_REG_FAN_SOURCE(i));
  159. if (err < 0)
  160. goto exit;
  161. data->fan_source[i] = err;
  162. } else {
  163. data->fan_input[i] = 0;
  164. data->fan_source[i] = 0;
  165. }
  166. }
  167. err = fts_read_byte(data->client, FTS_SENSOR_EVENT_REG);
  168. if (err < 0)
  169. goto exit;
  170. data->temp_alarm = err;
  171. for (i = 0; i < FTS_NO_TEMP_SENSORS; i++) {
  172. err = fts_read_byte(data->client, FTS_REG_TEMP_INPUT(i));
  173. if (err < 0)
  174. goto exit;
  175. data->temp_input[i] = err;
  176. }
  177. for (i = 0; i < FTS_NO_VOLT_SENSORS; i++) {
  178. err = fts_read_byte(data->client, FTS_REG_VOLT(i));
  179. if (err < 0)
  180. goto exit;
  181. data->volt[i] = err;
  182. }
  183. data->last_updated = jiffies;
  184. err = 0;
  185. exit:
  186. mutex_unlock(&data->update_lock);
  187. return err;
  188. }
  189. /*****************************************************************************/
  190. /* Watchdog functions */
  191. /*****************************************************************************/
  192. static int fts_wd_set_resolution(struct fts_data *data,
  193. enum WATCHDOG_RESOLUTION resolution)
  194. {
  195. int ret;
  196. if (data->resolution == resolution)
  197. return 0;
  198. ret = fts_read_byte(data->client, FTS_WATCHDOG_CONTROL);
  199. if (ret < 0)
  200. return ret;
  201. if ((resolution == seconds && ret & BIT(1)) ||
  202. (resolution == minutes && (ret & BIT(1)) == 0)) {
  203. data->resolution = resolution;
  204. return 0;
  205. }
  206. if (resolution == seconds)
  207. ret |= BIT(1);
  208. else
  209. ret &= ~BIT(1);
  210. ret = fts_write_byte(data->client, FTS_WATCHDOG_CONTROL, ret);
  211. if (ret < 0)
  212. return ret;
  213. data->resolution = resolution;
  214. return ret;
  215. }
  216. static int fts_wd_set_timeout(struct watchdog_device *wdd, unsigned int timeout)
  217. {
  218. struct fts_data *data;
  219. enum WATCHDOG_RESOLUTION resolution = seconds;
  220. int ret;
  221. data = watchdog_get_drvdata(wdd);
  222. /* switch watchdog resolution to minutes if timeout does not fit
  223. * into a byte
  224. */
  225. if (timeout > 0xFF) {
  226. timeout = DIV_ROUND_UP(timeout, 60) * 60;
  227. resolution = minutes;
  228. }
  229. ret = fts_wd_set_resolution(data, resolution);
  230. if (ret < 0)
  231. return ret;
  232. wdd->timeout = timeout;
  233. return 0;
  234. }
  235. static int fts_wd_start(struct watchdog_device *wdd)
  236. {
  237. struct fts_data *data = watchdog_get_drvdata(wdd);
  238. return fts_write_byte(data->client, FTS_WATCHDOG_TIME_PRESET,
  239. wdd->timeout / (u8)data->resolution);
  240. }
  241. static int fts_wd_stop(struct watchdog_device *wdd)
  242. {
  243. struct fts_data *data;
  244. data = watchdog_get_drvdata(wdd);
  245. return fts_write_byte(data->client, FTS_WATCHDOG_TIME_PRESET, 0);
  246. }
  247. static const struct watchdog_info fts_wd_info = {
  248. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
  249. .identity = "FTS Teutates Hardware Watchdog",
  250. };
  251. static const struct watchdog_ops fts_wd_ops = {
  252. .owner = THIS_MODULE,
  253. .start = fts_wd_start,
  254. .stop = fts_wd_stop,
  255. .set_timeout = fts_wd_set_timeout,
  256. };
  257. static int fts_watchdog_init(struct fts_data *data)
  258. {
  259. int timeout, ret;
  260. watchdog_set_drvdata(&data->wdd, data);
  261. timeout = fts_read_byte(data->client, FTS_WATCHDOG_TIME_PRESET);
  262. if (timeout < 0)
  263. return timeout;
  264. /* watchdog not running, set timeout to a default of 60 sec. */
  265. if (timeout == 0) {
  266. ret = fts_wd_set_resolution(data, seconds);
  267. if (ret < 0)
  268. return ret;
  269. data->wdd.timeout = 60;
  270. } else {
  271. ret = fts_read_byte(data->client, FTS_WATCHDOG_CONTROL);
  272. if (ret < 0)
  273. return ret;
  274. data->resolution = ret & BIT(1) ? seconds : minutes;
  275. data->wdd.timeout = timeout * (u8)data->resolution;
  276. set_bit(WDOG_HW_RUNNING, &data->wdd.status);
  277. }
  278. /* Register our watchdog part */
  279. data->wdd.info = &fts_wd_info;
  280. data->wdd.ops = &fts_wd_ops;
  281. data->wdd.parent = &data->client->dev;
  282. data->wdd.min_timeout = 1;
  283. /* max timeout 255 minutes. */
  284. data->wdd.max_hw_heartbeat_ms = 0xFF * 60 * MSEC_PER_SEC;
  285. return watchdog_register_device(&data->wdd);
  286. }
  287. /*****************************************************************************/
  288. /* SysFS handler functions */
  289. /*****************************************************************************/
  290. static ssize_t show_in_value(struct device *dev,
  291. struct device_attribute *devattr, char *buf)
  292. {
  293. struct fts_data *data = dev_get_drvdata(dev);
  294. int index = to_sensor_dev_attr(devattr)->index;
  295. int err;
  296. err = fts_update_device(data);
  297. if (err < 0)
  298. return err;
  299. return sprintf(buf, "%u\n", data->volt[index]);
  300. }
  301. static ssize_t show_temp_value(struct device *dev,
  302. struct device_attribute *devattr, char *buf)
  303. {
  304. struct fts_data *data = dev_get_drvdata(dev);
  305. int index = to_sensor_dev_attr(devattr)->index;
  306. int err;
  307. err = fts_update_device(data);
  308. if (err < 0)
  309. return err;
  310. return sprintf(buf, "%u\n", data->temp_input[index]);
  311. }
  312. static ssize_t show_temp_fault(struct device *dev,
  313. struct device_attribute *devattr, char *buf)
  314. {
  315. struct fts_data *data = dev_get_drvdata(dev);
  316. int index = to_sensor_dev_attr(devattr)->index;
  317. int err;
  318. err = fts_update_device(data);
  319. if (err < 0)
  320. return err;
  321. /* 00h Temperature = Sensor Error */
  322. return sprintf(buf, "%d\n", data->temp_input[index] == 0);
  323. }
  324. static ssize_t show_temp_alarm(struct device *dev,
  325. struct device_attribute *devattr, char *buf)
  326. {
  327. struct fts_data *data = dev_get_drvdata(dev);
  328. int index = to_sensor_dev_attr(devattr)->index;
  329. int err;
  330. err = fts_update_device(data);
  331. if (err < 0)
  332. return err;
  333. return sprintf(buf, "%u\n", !!(data->temp_alarm & BIT(index)));
  334. }
  335. static ssize_t
  336. clear_temp_alarm(struct device *dev, struct device_attribute *devattr,
  337. const char *buf, size_t count)
  338. {
  339. struct fts_data *data = dev_get_drvdata(dev);
  340. int index = to_sensor_dev_attr(devattr)->index;
  341. long ret;
  342. ret = fts_update_device(data);
  343. if (ret < 0)
  344. return ret;
  345. if (kstrtoul(buf, 10, &ret) || ret != 0)
  346. return -EINVAL;
  347. mutex_lock(&data->update_lock);
  348. ret = fts_read_byte(data->client, FTS_REG_TEMP_CONTROL(index));
  349. if (ret < 0)
  350. goto error;
  351. ret = fts_write_byte(data->client, FTS_REG_TEMP_CONTROL(index),
  352. ret | 0x1);
  353. if (ret < 0)
  354. goto error;
  355. data->valid = false;
  356. error:
  357. mutex_unlock(&data->update_lock);
  358. return ret;
  359. }
  360. static ssize_t show_fan_value(struct device *dev,
  361. struct device_attribute *devattr, char *buf)
  362. {
  363. struct fts_data *data = dev_get_drvdata(dev);
  364. int index = to_sensor_dev_attr(devattr)->index;
  365. int err;
  366. err = fts_update_device(data);
  367. if (err < 0)
  368. return err;
  369. return sprintf(buf, "%u\n", data->fan_input[index]);
  370. }
  371. static ssize_t show_fan_source(struct device *dev,
  372. struct device_attribute *devattr, char *buf)
  373. {
  374. struct fts_data *data = dev_get_drvdata(dev);
  375. int index = to_sensor_dev_attr(devattr)->index;
  376. int err;
  377. err = fts_update_device(data);
  378. if (err < 0)
  379. return err;
  380. return sprintf(buf, "%u\n", data->fan_source[index]);
  381. }
  382. static ssize_t show_fan_alarm(struct device *dev,
  383. struct device_attribute *devattr, char *buf)
  384. {
  385. struct fts_data *data = dev_get_drvdata(dev);
  386. int index = to_sensor_dev_attr(devattr)->index;
  387. int err;
  388. err = fts_update_device(data);
  389. if (err < 0)
  390. return err;
  391. return sprintf(buf, "%d\n", !!(data->fan_alarm & BIT(index)));
  392. }
  393. static ssize_t
  394. clear_fan_alarm(struct device *dev, struct device_attribute *devattr,
  395. const char *buf, size_t count)
  396. {
  397. struct fts_data *data = dev_get_drvdata(dev);
  398. int index = to_sensor_dev_attr(devattr)->index;
  399. long ret;
  400. ret = fts_update_device(data);
  401. if (ret < 0)
  402. return ret;
  403. if (kstrtoul(buf, 10, &ret) || ret != 0)
  404. return -EINVAL;
  405. mutex_lock(&data->update_lock);
  406. ret = fts_read_byte(data->client, FTS_REG_FAN_CONTROL(index));
  407. if (ret < 0)
  408. goto error;
  409. ret = fts_write_byte(data->client, FTS_REG_FAN_CONTROL(index),
  410. ret | 0x1);
  411. if (ret < 0)
  412. goto error;
  413. data->valid = false;
  414. error:
  415. mutex_unlock(&data->update_lock);
  416. return ret;
  417. }
  418. /*****************************************************************************/
  419. /* SysFS structs */
  420. /*****************************************************************************/
  421. /* Temprature sensors */
  422. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_value, NULL, 0);
  423. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp_value, NULL, 1);
  424. static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp_value, NULL, 2);
  425. static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp_value, NULL, 3);
  426. static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO, show_temp_value, NULL, 4);
  427. static SENSOR_DEVICE_ATTR(temp6_input, S_IRUGO, show_temp_value, NULL, 5);
  428. static SENSOR_DEVICE_ATTR(temp7_input, S_IRUGO, show_temp_value, NULL, 6);
  429. static SENSOR_DEVICE_ATTR(temp8_input, S_IRUGO, show_temp_value, NULL, 7);
  430. static SENSOR_DEVICE_ATTR(temp9_input, S_IRUGO, show_temp_value, NULL, 8);
  431. static SENSOR_DEVICE_ATTR(temp10_input, S_IRUGO, show_temp_value, NULL, 9);
  432. static SENSOR_DEVICE_ATTR(temp11_input, S_IRUGO, show_temp_value, NULL, 10);
  433. static SENSOR_DEVICE_ATTR(temp12_input, S_IRUGO, show_temp_value, NULL, 11);
  434. static SENSOR_DEVICE_ATTR(temp13_input, S_IRUGO, show_temp_value, NULL, 12);
  435. static SENSOR_DEVICE_ATTR(temp14_input, S_IRUGO, show_temp_value, NULL, 13);
  436. static SENSOR_DEVICE_ATTR(temp15_input, S_IRUGO, show_temp_value, NULL, 14);
  437. static SENSOR_DEVICE_ATTR(temp16_input, S_IRUGO, show_temp_value, NULL, 15);
  438. static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, show_temp_fault, NULL, 0);
  439. static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_temp_fault, NULL, 1);
  440. static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_temp_fault, NULL, 2);
  441. static SENSOR_DEVICE_ATTR(temp4_fault, S_IRUGO, show_temp_fault, NULL, 3);
  442. static SENSOR_DEVICE_ATTR(temp5_fault, S_IRUGO, show_temp_fault, NULL, 4);
  443. static SENSOR_DEVICE_ATTR(temp6_fault, S_IRUGO, show_temp_fault, NULL, 5);
  444. static SENSOR_DEVICE_ATTR(temp7_fault, S_IRUGO, show_temp_fault, NULL, 6);
  445. static SENSOR_DEVICE_ATTR(temp8_fault, S_IRUGO, show_temp_fault, NULL, 7);
  446. static SENSOR_DEVICE_ATTR(temp9_fault, S_IRUGO, show_temp_fault, NULL, 8);
  447. static SENSOR_DEVICE_ATTR(temp10_fault, S_IRUGO, show_temp_fault, NULL, 9);
  448. static SENSOR_DEVICE_ATTR(temp11_fault, S_IRUGO, show_temp_fault, NULL, 10);
  449. static SENSOR_DEVICE_ATTR(temp12_fault, S_IRUGO, show_temp_fault, NULL, 11);
  450. static SENSOR_DEVICE_ATTR(temp13_fault, S_IRUGO, show_temp_fault, NULL, 12);
  451. static SENSOR_DEVICE_ATTR(temp14_fault, S_IRUGO, show_temp_fault, NULL, 13);
  452. static SENSOR_DEVICE_ATTR(temp15_fault, S_IRUGO, show_temp_fault, NULL, 14);
  453. static SENSOR_DEVICE_ATTR(temp16_fault, S_IRUGO, show_temp_fault, NULL, 15);
  454. static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO | S_IWUSR, show_temp_alarm,
  455. clear_temp_alarm, 0);
  456. static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO | S_IWUSR, show_temp_alarm,
  457. clear_temp_alarm, 1);
  458. static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO | S_IWUSR, show_temp_alarm,
  459. clear_temp_alarm, 2);
  460. static SENSOR_DEVICE_ATTR(temp4_alarm, S_IRUGO | S_IWUSR, show_temp_alarm,
  461. clear_temp_alarm, 3);
  462. static SENSOR_DEVICE_ATTR(temp5_alarm, S_IRUGO | S_IWUSR, show_temp_alarm,
  463. clear_temp_alarm, 4);
  464. static SENSOR_DEVICE_ATTR(temp6_alarm, S_IRUGO | S_IWUSR, show_temp_alarm,
  465. clear_temp_alarm, 5);
  466. static SENSOR_DEVICE_ATTR(temp7_alarm, S_IRUGO | S_IWUSR, show_temp_alarm,
  467. clear_temp_alarm, 6);
  468. static SENSOR_DEVICE_ATTR(temp8_alarm, S_IRUGO | S_IWUSR, show_temp_alarm,
  469. clear_temp_alarm, 7);
  470. static SENSOR_DEVICE_ATTR(temp9_alarm, S_IRUGO | S_IWUSR, show_temp_alarm,
  471. clear_temp_alarm, 8);
  472. static SENSOR_DEVICE_ATTR(temp10_alarm, S_IRUGO | S_IWUSR, show_temp_alarm,
  473. clear_temp_alarm, 9);
  474. static SENSOR_DEVICE_ATTR(temp11_alarm, S_IRUGO | S_IWUSR, show_temp_alarm,
  475. clear_temp_alarm, 10);
  476. static SENSOR_DEVICE_ATTR(temp12_alarm, S_IRUGO | S_IWUSR, show_temp_alarm,
  477. clear_temp_alarm, 11);
  478. static SENSOR_DEVICE_ATTR(temp13_alarm, S_IRUGO | S_IWUSR, show_temp_alarm,
  479. clear_temp_alarm, 12);
  480. static SENSOR_DEVICE_ATTR(temp14_alarm, S_IRUGO | S_IWUSR, show_temp_alarm,
  481. clear_temp_alarm, 13);
  482. static SENSOR_DEVICE_ATTR(temp15_alarm, S_IRUGO | S_IWUSR, show_temp_alarm,
  483. clear_temp_alarm, 14);
  484. static SENSOR_DEVICE_ATTR(temp16_alarm, S_IRUGO | S_IWUSR, show_temp_alarm,
  485. clear_temp_alarm, 15);
  486. static struct attribute *fts_temp_attrs[] = {
  487. &sensor_dev_attr_temp1_input.dev_attr.attr,
  488. &sensor_dev_attr_temp2_input.dev_attr.attr,
  489. &sensor_dev_attr_temp3_input.dev_attr.attr,
  490. &sensor_dev_attr_temp4_input.dev_attr.attr,
  491. &sensor_dev_attr_temp5_input.dev_attr.attr,
  492. &sensor_dev_attr_temp6_input.dev_attr.attr,
  493. &sensor_dev_attr_temp7_input.dev_attr.attr,
  494. &sensor_dev_attr_temp8_input.dev_attr.attr,
  495. &sensor_dev_attr_temp9_input.dev_attr.attr,
  496. &sensor_dev_attr_temp10_input.dev_attr.attr,
  497. &sensor_dev_attr_temp11_input.dev_attr.attr,
  498. &sensor_dev_attr_temp12_input.dev_attr.attr,
  499. &sensor_dev_attr_temp13_input.dev_attr.attr,
  500. &sensor_dev_attr_temp14_input.dev_attr.attr,
  501. &sensor_dev_attr_temp15_input.dev_attr.attr,
  502. &sensor_dev_attr_temp16_input.dev_attr.attr,
  503. &sensor_dev_attr_temp1_fault.dev_attr.attr,
  504. &sensor_dev_attr_temp2_fault.dev_attr.attr,
  505. &sensor_dev_attr_temp3_fault.dev_attr.attr,
  506. &sensor_dev_attr_temp4_fault.dev_attr.attr,
  507. &sensor_dev_attr_temp5_fault.dev_attr.attr,
  508. &sensor_dev_attr_temp6_fault.dev_attr.attr,
  509. &sensor_dev_attr_temp7_fault.dev_attr.attr,
  510. &sensor_dev_attr_temp8_fault.dev_attr.attr,
  511. &sensor_dev_attr_temp9_fault.dev_attr.attr,
  512. &sensor_dev_attr_temp10_fault.dev_attr.attr,
  513. &sensor_dev_attr_temp11_fault.dev_attr.attr,
  514. &sensor_dev_attr_temp12_fault.dev_attr.attr,
  515. &sensor_dev_attr_temp13_fault.dev_attr.attr,
  516. &sensor_dev_attr_temp14_fault.dev_attr.attr,
  517. &sensor_dev_attr_temp15_fault.dev_attr.attr,
  518. &sensor_dev_attr_temp16_fault.dev_attr.attr,
  519. &sensor_dev_attr_temp1_alarm.dev_attr.attr,
  520. &sensor_dev_attr_temp2_alarm.dev_attr.attr,
  521. &sensor_dev_attr_temp3_alarm.dev_attr.attr,
  522. &sensor_dev_attr_temp4_alarm.dev_attr.attr,
  523. &sensor_dev_attr_temp5_alarm.dev_attr.attr,
  524. &sensor_dev_attr_temp6_alarm.dev_attr.attr,
  525. &sensor_dev_attr_temp7_alarm.dev_attr.attr,
  526. &sensor_dev_attr_temp8_alarm.dev_attr.attr,
  527. &sensor_dev_attr_temp9_alarm.dev_attr.attr,
  528. &sensor_dev_attr_temp10_alarm.dev_attr.attr,
  529. &sensor_dev_attr_temp11_alarm.dev_attr.attr,
  530. &sensor_dev_attr_temp12_alarm.dev_attr.attr,
  531. &sensor_dev_attr_temp13_alarm.dev_attr.attr,
  532. &sensor_dev_attr_temp14_alarm.dev_attr.attr,
  533. &sensor_dev_attr_temp15_alarm.dev_attr.attr,
  534. &sensor_dev_attr_temp16_alarm.dev_attr.attr,
  535. NULL
  536. };
  537. /* Fans */
  538. static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan_value, NULL, 0);
  539. static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan_value, NULL, 1);
  540. static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan_value, NULL, 2);
  541. static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, show_fan_value, NULL, 3);
  542. static SENSOR_DEVICE_ATTR(fan5_input, S_IRUGO, show_fan_value, NULL, 4);
  543. static SENSOR_DEVICE_ATTR(fan6_input, S_IRUGO, show_fan_value, NULL, 5);
  544. static SENSOR_DEVICE_ATTR(fan7_input, S_IRUGO, show_fan_value, NULL, 6);
  545. static SENSOR_DEVICE_ATTR(fan8_input, S_IRUGO, show_fan_value, NULL, 7);
  546. static SENSOR_DEVICE_ATTR(fan1_source, S_IRUGO, show_fan_source, NULL, 0);
  547. static SENSOR_DEVICE_ATTR(fan2_source, S_IRUGO, show_fan_source, NULL, 1);
  548. static SENSOR_DEVICE_ATTR(fan3_source, S_IRUGO, show_fan_source, NULL, 2);
  549. static SENSOR_DEVICE_ATTR(fan4_source, S_IRUGO, show_fan_source, NULL, 3);
  550. static SENSOR_DEVICE_ATTR(fan5_source, S_IRUGO, show_fan_source, NULL, 4);
  551. static SENSOR_DEVICE_ATTR(fan6_source, S_IRUGO, show_fan_source, NULL, 5);
  552. static SENSOR_DEVICE_ATTR(fan7_source, S_IRUGO, show_fan_source, NULL, 6);
  553. static SENSOR_DEVICE_ATTR(fan8_source, S_IRUGO, show_fan_source, NULL, 7);
  554. static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO | S_IWUSR,
  555. show_fan_alarm, clear_fan_alarm, 0);
  556. static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO | S_IWUSR,
  557. show_fan_alarm, clear_fan_alarm, 1);
  558. static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO | S_IWUSR,
  559. show_fan_alarm, clear_fan_alarm, 2);
  560. static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO | S_IWUSR,
  561. show_fan_alarm, clear_fan_alarm, 3);
  562. static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO | S_IWUSR,
  563. show_fan_alarm, clear_fan_alarm, 4);
  564. static SENSOR_DEVICE_ATTR(fan6_alarm, S_IRUGO | S_IWUSR,
  565. show_fan_alarm, clear_fan_alarm, 5);
  566. static SENSOR_DEVICE_ATTR(fan7_alarm, S_IRUGO | S_IWUSR,
  567. show_fan_alarm, clear_fan_alarm, 6);
  568. static SENSOR_DEVICE_ATTR(fan8_alarm, S_IRUGO | S_IWUSR,
  569. show_fan_alarm, clear_fan_alarm, 7);
  570. static struct attribute *fts_fan_attrs[] = {
  571. &sensor_dev_attr_fan1_input.dev_attr.attr,
  572. &sensor_dev_attr_fan2_input.dev_attr.attr,
  573. &sensor_dev_attr_fan3_input.dev_attr.attr,
  574. &sensor_dev_attr_fan4_input.dev_attr.attr,
  575. &sensor_dev_attr_fan5_input.dev_attr.attr,
  576. &sensor_dev_attr_fan6_input.dev_attr.attr,
  577. &sensor_dev_attr_fan7_input.dev_attr.attr,
  578. &sensor_dev_attr_fan8_input.dev_attr.attr,
  579. &sensor_dev_attr_fan1_source.dev_attr.attr,
  580. &sensor_dev_attr_fan2_source.dev_attr.attr,
  581. &sensor_dev_attr_fan3_source.dev_attr.attr,
  582. &sensor_dev_attr_fan4_source.dev_attr.attr,
  583. &sensor_dev_attr_fan5_source.dev_attr.attr,
  584. &sensor_dev_attr_fan6_source.dev_attr.attr,
  585. &sensor_dev_attr_fan7_source.dev_attr.attr,
  586. &sensor_dev_attr_fan8_source.dev_attr.attr,
  587. &sensor_dev_attr_fan1_alarm.dev_attr.attr,
  588. &sensor_dev_attr_fan2_alarm.dev_attr.attr,
  589. &sensor_dev_attr_fan3_alarm.dev_attr.attr,
  590. &sensor_dev_attr_fan4_alarm.dev_attr.attr,
  591. &sensor_dev_attr_fan5_alarm.dev_attr.attr,
  592. &sensor_dev_attr_fan6_alarm.dev_attr.attr,
  593. &sensor_dev_attr_fan7_alarm.dev_attr.attr,
  594. &sensor_dev_attr_fan8_alarm.dev_attr.attr,
  595. NULL
  596. };
  597. /* Voltages */
  598. static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_in_value, NULL, 0);
  599. static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_in_value, NULL, 1);
  600. static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_in_value, NULL, 2);
  601. static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, show_in_value, NULL, 3);
  602. static struct attribute *fts_voltage_attrs[] = {
  603. &sensor_dev_attr_in1_input.dev_attr.attr,
  604. &sensor_dev_attr_in2_input.dev_attr.attr,
  605. &sensor_dev_attr_in3_input.dev_attr.attr,
  606. &sensor_dev_attr_in4_input.dev_attr.attr,
  607. NULL
  608. };
  609. static const struct attribute_group fts_voltage_attr_group = {
  610. .attrs = fts_voltage_attrs
  611. };
  612. static const struct attribute_group fts_temp_attr_group = {
  613. .attrs = fts_temp_attrs
  614. };
  615. static const struct attribute_group fts_fan_attr_group = {
  616. .attrs = fts_fan_attrs
  617. };
  618. static const struct attribute_group *fts_attr_groups[] = {
  619. &fts_voltage_attr_group,
  620. &fts_temp_attr_group,
  621. &fts_fan_attr_group,
  622. NULL
  623. };
  624. /*****************************************************************************/
  625. /* Module initialization / remove functions */
  626. /*****************************************************************************/
  627. static int fts_detect(struct i2c_client *client,
  628. struct i2c_board_info *info)
  629. {
  630. int val;
  631. /* detection works with revsion greater or equal to 0x2b */
  632. val = i2c_smbus_read_byte_data(client, FTS_DEVICE_REVISION_REG);
  633. if (val < 0x2b)
  634. return -ENODEV;
  635. /* Device Detect Regs must have 0x17 0x34 and 0x54 */
  636. val = i2c_smbus_read_byte_data(client, FTS_DEVICE_DETECT_REG_1);
  637. if (val != 0x17)
  638. return -ENODEV;
  639. val = i2c_smbus_read_byte_data(client, FTS_DEVICE_DETECT_REG_2);
  640. if (val != 0x34)
  641. return -ENODEV;
  642. val = i2c_smbus_read_byte_data(client, FTS_DEVICE_DETECT_REG_3);
  643. if (val != 0x54)
  644. return -ENODEV;
  645. /*
  646. * 0x10 == Baseboard Management Controller, 0x01 == Teutates
  647. * Device ID Reg needs to be 0x11
  648. */
  649. val = i2c_smbus_read_byte_data(client, FTS_DEVICE_ID_REG);
  650. if (val != 0x11)
  651. return -ENODEV;
  652. strlcpy(info->type, fts_id[0].name, I2C_NAME_SIZE);
  653. info->flags = 0;
  654. return 0;
  655. }
  656. static int fts_remove(struct i2c_client *client)
  657. {
  658. struct fts_data *data = dev_get_drvdata(&client->dev);
  659. watchdog_unregister_device(&data->wdd);
  660. return 0;
  661. }
  662. static int fts_probe(struct i2c_client *client, const struct i2c_device_id *id)
  663. {
  664. u8 revision;
  665. struct fts_data *data;
  666. int err;
  667. s8 deviceid;
  668. struct device *hwmon_dev;
  669. if (client->addr != 0x73)
  670. return -ENODEV;
  671. /* Baseboard Management Controller check */
  672. deviceid = i2c_smbus_read_byte_data(client, FTS_DEVICE_ID_REG);
  673. if (deviceid > 0 && (deviceid & 0xF0) == 0x10) {
  674. switch (deviceid & 0x0F) {
  675. case 0x01:
  676. break;
  677. default:
  678. dev_dbg(&client->dev,
  679. "No Baseboard Management Controller\n");
  680. return -ENODEV;
  681. }
  682. } else {
  683. dev_dbg(&client->dev, "No fujitsu board\n");
  684. return -ENODEV;
  685. }
  686. data = devm_kzalloc(&client->dev, sizeof(struct fts_data),
  687. GFP_KERNEL);
  688. if (!data)
  689. return -ENOMEM;
  690. mutex_init(&data->update_lock);
  691. mutex_init(&data->access_lock);
  692. data->client = client;
  693. dev_set_drvdata(&client->dev, data);
  694. err = i2c_smbus_read_byte_data(client, FTS_DEVICE_REVISION_REG);
  695. if (err < 0)
  696. return err;
  697. revision = err;
  698. hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev,
  699. "ftsteutates",
  700. data,
  701. fts_attr_groups);
  702. if (IS_ERR(hwmon_dev))
  703. return PTR_ERR(hwmon_dev);
  704. err = fts_watchdog_init(data);
  705. if (err)
  706. return err;
  707. dev_info(&client->dev, "Detected FTS Teutates chip, revision: %d.%d\n",
  708. (revision & 0xF0) >> 4, revision & 0x0F);
  709. return 0;
  710. }
  711. /*****************************************************************************/
  712. /* Module Details */
  713. /*****************************************************************************/
  714. static struct i2c_driver fts_driver = {
  715. .class = I2C_CLASS_HWMON,
  716. .driver = {
  717. .name = "ftsteutates",
  718. },
  719. .id_table = fts_id,
  720. .probe = fts_probe,
  721. .remove = fts_remove,
  722. .detect = fts_detect,
  723. .address_list = normal_i2c,
  724. };
  725. module_i2c_driver(fts_driver);
  726. MODULE_AUTHOR("Thilo Cestonaro <[email protected]>");
  727. MODULE_DESCRIPTION("FTS Teutates driver");
  728. MODULE_LICENSE("GPL");