lineage-pem.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. /*
  2. * Driver for Lineage Compact Power Line series of power entry modules.
  3. *
  4. * Copyright (C) 2010, 2011 Ericsson AB.
  5. *
  6. * Documentation:
  7. * http://www.lineagepower.com/oem/pdf/CPLI2C.pdf
  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>
  24. #include <linux/module.h>
  25. #include <linux/init.h>
  26. #include <linux/err.h>
  27. #include <linux/slab.h>
  28. #include <linux/i2c.h>
  29. #include <linux/hwmon.h>
  30. #include <linux/hwmon-sysfs.h>
  31. #include <linux/jiffies.h>
  32. /*
  33. * This driver supports various Lineage Compact Power Line DC/DC and AC/DC
  34. * converters such as CP1800, CP2000AC, CP2000DC, CP2100DC, and others.
  35. *
  36. * The devices are nominally PMBus compliant. However, most standard PMBus
  37. * commands are not supported. Specifically, all hardware monitoring and
  38. * status reporting commands are non-standard. For this reason, a standard
  39. * PMBus driver can not be used.
  40. *
  41. * All Lineage CPL devices have a built-in I2C bus master selector (PCA9541).
  42. * To ensure device access, this driver should only be used as client driver
  43. * to the pca9541 I2C master selector driver.
  44. */
  45. /* Command codes */
  46. #define PEM_OPERATION 0x01
  47. #define PEM_CLEAR_INFO_FLAGS 0x03
  48. #define PEM_VOUT_COMMAND 0x21
  49. #define PEM_VOUT_OV_FAULT_LIMIT 0x40
  50. #define PEM_READ_DATA_STRING 0xd0
  51. #define PEM_READ_INPUT_STRING 0xdc
  52. #define PEM_READ_FIRMWARE_REV 0xdd
  53. #define PEM_READ_RUN_TIMER 0xde
  54. #define PEM_FAN_HI_SPEED 0xdf
  55. #define PEM_FAN_NORMAL_SPEED 0xe0
  56. #define PEM_READ_FAN_SPEED 0xe1
  57. /* offsets in data string */
  58. #define PEM_DATA_STATUS_2 0
  59. #define PEM_DATA_STATUS_1 1
  60. #define PEM_DATA_ALARM_2 2
  61. #define PEM_DATA_ALARM_1 3
  62. #define PEM_DATA_VOUT_LSB 4
  63. #define PEM_DATA_VOUT_MSB 5
  64. #define PEM_DATA_CURRENT 6
  65. #define PEM_DATA_TEMP 7
  66. /* Virtual entries, to report constants */
  67. #define PEM_DATA_TEMP_MAX 10
  68. #define PEM_DATA_TEMP_CRIT 11
  69. /* offsets in input string */
  70. #define PEM_INPUT_VOLTAGE 0
  71. #define PEM_INPUT_POWER_LSB 1
  72. #define PEM_INPUT_POWER_MSB 2
  73. /* offsets in fan data */
  74. #define PEM_FAN_ADJUSTMENT 0
  75. #define PEM_FAN_FAN1 1
  76. #define PEM_FAN_FAN2 2
  77. #define PEM_FAN_FAN3 3
  78. /* Status register bits */
  79. #define STS1_OUTPUT_ON (1 << 0)
  80. #define STS1_LEDS_FLASHING (1 << 1)
  81. #define STS1_EXT_FAULT (1 << 2)
  82. #define STS1_SERVICE_LED_ON (1 << 3)
  83. #define STS1_SHUTDOWN_OCCURRED (1 << 4)
  84. #define STS1_INT_FAULT (1 << 5)
  85. #define STS1_ISOLATION_TEST_OK (1 << 6)
  86. #define STS2_ENABLE_PIN_HI (1 << 0)
  87. #define STS2_DATA_OUT_RANGE (1 << 1)
  88. #define STS2_RESTARTED_OK (1 << 1)
  89. #define STS2_ISOLATION_TEST_FAIL (1 << 3)
  90. #define STS2_HIGH_POWER_CAP (1 << 4)
  91. #define STS2_INVALID_INSTR (1 << 5)
  92. #define STS2_WILL_RESTART (1 << 6)
  93. #define STS2_PEC_ERR (1 << 7)
  94. /* Alarm register bits */
  95. #define ALRM1_VIN_OUT_LIMIT (1 << 0)
  96. #define ALRM1_VOUT_OUT_LIMIT (1 << 1)
  97. #define ALRM1_OV_VOLT_SHUTDOWN (1 << 2)
  98. #define ALRM1_VIN_OVERCURRENT (1 << 3)
  99. #define ALRM1_TEMP_WARNING (1 << 4)
  100. #define ALRM1_TEMP_SHUTDOWN (1 << 5)
  101. #define ALRM1_PRIMARY_FAULT (1 << 6)
  102. #define ALRM1_POWER_LIMIT (1 << 7)
  103. #define ALRM2_5V_OUT_LIMIT (1 << 1)
  104. #define ALRM2_TEMP_FAULT (1 << 2)
  105. #define ALRM2_OV_LOW (1 << 3)
  106. #define ALRM2_DCDC_TEMP_HIGH (1 << 4)
  107. #define ALRM2_PRI_TEMP_HIGH (1 << 5)
  108. #define ALRM2_NO_PRIMARY (1 << 6)
  109. #define ALRM2_FAN_FAULT (1 << 7)
  110. #define FIRMWARE_REV_LEN 4
  111. #define DATA_STRING_LEN 9
  112. #define INPUT_STRING_LEN 5 /* 4 for most devices */
  113. #define FAN_SPEED_LEN 5
  114. struct pem_data {
  115. struct i2c_client *client;
  116. const struct attribute_group *groups[4];
  117. struct mutex update_lock;
  118. bool valid;
  119. bool fans_supported;
  120. int input_length;
  121. unsigned long last_updated; /* in jiffies */
  122. u8 firmware_rev[FIRMWARE_REV_LEN];
  123. u8 data_string[DATA_STRING_LEN];
  124. u8 input_string[INPUT_STRING_LEN];
  125. u8 fan_speed[FAN_SPEED_LEN];
  126. };
  127. static int pem_read_block(struct i2c_client *client, u8 command, u8 *data,
  128. int data_len)
  129. {
  130. u8 block_buffer[I2C_SMBUS_BLOCK_MAX];
  131. int result;
  132. result = i2c_smbus_read_block_data(client, command, block_buffer);
  133. if (unlikely(result < 0))
  134. goto abort;
  135. if (unlikely(result == 0xff || result != data_len)) {
  136. result = -EIO;
  137. goto abort;
  138. }
  139. memcpy(data, block_buffer, data_len);
  140. result = 0;
  141. abort:
  142. return result;
  143. }
  144. static struct pem_data *pem_update_device(struct device *dev)
  145. {
  146. struct pem_data *data = dev_get_drvdata(dev);
  147. struct i2c_client *client = data->client;
  148. struct pem_data *ret = data;
  149. mutex_lock(&data->update_lock);
  150. if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
  151. int result;
  152. /* Read data string */
  153. result = pem_read_block(client, PEM_READ_DATA_STRING,
  154. data->data_string,
  155. sizeof(data->data_string));
  156. if (unlikely(result < 0)) {
  157. ret = ERR_PTR(result);
  158. goto abort;
  159. }
  160. /* Read input string */
  161. if (data->input_length) {
  162. result = pem_read_block(client, PEM_READ_INPUT_STRING,
  163. data->input_string,
  164. data->input_length);
  165. if (unlikely(result < 0)) {
  166. ret = ERR_PTR(result);
  167. goto abort;
  168. }
  169. }
  170. /* Read fan speeds */
  171. if (data->fans_supported) {
  172. result = pem_read_block(client, PEM_READ_FAN_SPEED,
  173. data->fan_speed,
  174. sizeof(data->fan_speed));
  175. if (unlikely(result < 0)) {
  176. ret = ERR_PTR(result);
  177. goto abort;
  178. }
  179. }
  180. i2c_smbus_write_byte(client, PEM_CLEAR_INFO_FLAGS);
  181. data->last_updated = jiffies;
  182. data->valid = 1;
  183. }
  184. abort:
  185. mutex_unlock(&data->update_lock);
  186. return ret;
  187. }
  188. static long pem_get_data(u8 *data, int len, int index)
  189. {
  190. long val;
  191. switch (index) {
  192. case PEM_DATA_VOUT_LSB:
  193. val = (data[index] + (data[index+1] << 8)) * 5 / 2;
  194. break;
  195. case PEM_DATA_CURRENT:
  196. val = data[index] * 200;
  197. break;
  198. case PEM_DATA_TEMP:
  199. val = data[index] * 1000;
  200. break;
  201. case PEM_DATA_TEMP_MAX:
  202. val = 97 * 1000; /* 97 degrees C per datasheet */
  203. break;
  204. case PEM_DATA_TEMP_CRIT:
  205. val = 107 * 1000; /* 107 degrees C per datasheet */
  206. break;
  207. default:
  208. WARN_ON_ONCE(1);
  209. val = 0;
  210. }
  211. return val;
  212. }
  213. static long pem_get_input(u8 *data, int len, int index)
  214. {
  215. long val;
  216. switch (index) {
  217. case PEM_INPUT_VOLTAGE:
  218. if (len == INPUT_STRING_LEN)
  219. val = (data[index] + (data[index+1] << 8) - 75) * 1000;
  220. else
  221. val = (data[index] - 75) * 1000;
  222. break;
  223. case PEM_INPUT_POWER_LSB:
  224. if (len == INPUT_STRING_LEN)
  225. index++;
  226. val = (data[index] + (data[index+1] << 8)) * 1000000L;
  227. break;
  228. default:
  229. WARN_ON_ONCE(1);
  230. val = 0;
  231. }
  232. return val;
  233. }
  234. static long pem_get_fan(u8 *data, int len, int index)
  235. {
  236. long val;
  237. switch (index) {
  238. case PEM_FAN_FAN1:
  239. case PEM_FAN_FAN2:
  240. case PEM_FAN_FAN3:
  241. val = data[index] * 100;
  242. break;
  243. default:
  244. WARN_ON_ONCE(1);
  245. val = 0;
  246. }
  247. return val;
  248. }
  249. /*
  250. * Show boolean, either a fault or an alarm.
  251. * .nr points to the register, .index is the bit mask to check
  252. */
  253. static ssize_t pem_show_bool(struct device *dev,
  254. struct device_attribute *da, char *buf)
  255. {
  256. struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(da);
  257. struct pem_data *data = pem_update_device(dev);
  258. u8 status;
  259. if (IS_ERR(data))
  260. return PTR_ERR(data);
  261. status = data->data_string[attr->nr] & attr->index;
  262. return snprintf(buf, PAGE_SIZE, "%d\n", !!status);
  263. }
  264. static ssize_t pem_show_data(struct device *dev, struct device_attribute *da,
  265. char *buf)
  266. {
  267. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  268. struct pem_data *data = pem_update_device(dev);
  269. long value;
  270. if (IS_ERR(data))
  271. return PTR_ERR(data);
  272. value = pem_get_data(data->data_string, sizeof(data->data_string),
  273. attr->index);
  274. return snprintf(buf, PAGE_SIZE, "%ld\n", value);
  275. }
  276. static ssize_t pem_show_input(struct device *dev, struct device_attribute *da,
  277. char *buf)
  278. {
  279. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  280. struct pem_data *data = pem_update_device(dev);
  281. long value;
  282. if (IS_ERR(data))
  283. return PTR_ERR(data);
  284. value = pem_get_input(data->input_string, sizeof(data->input_string),
  285. attr->index);
  286. return snprintf(buf, PAGE_SIZE, "%ld\n", value);
  287. }
  288. static ssize_t pem_show_fan(struct device *dev, struct device_attribute *da,
  289. char *buf)
  290. {
  291. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  292. struct pem_data *data = pem_update_device(dev);
  293. long value;
  294. if (IS_ERR(data))
  295. return PTR_ERR(data);
  296. value = pem_get_fan(data->fan_speed, sizeof(data->fan_speed),
  297. attr->index);
  298. return snprintf(buf, PAGE_SIZE, "%ld\n", value);
  299. }
  300. /* Voltages */
  301. static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, pem_show_data, NULL,
  302. PEM_DATA_VOUT_LSB);
  303. static SENSOR_DEVICE_ATTR_2(in1_alarm, S_IRUGO, pem_show_bool, NULL,
  304. PEM_DATA_ALARM_1, ALRM1_VOUT_OUT_LIMIT);
  305. static SENSOR_DEVICE_ATTR_2(in1_crit_alarm, S_IRUGO, pem_show_bool, NULL,
  306. PEM_DATA_ALARM_1, ALRM1_OV_VOLT_SHUTDOWN);
  307. static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, pem_show_input, NULL,
  308. PEM_INPUT_VOLTAGE);
  309. static SENSOR_DEVICE_ATTR_2(in2_alarm, S_IRUGO, pem_show_bool, NULL,
  310. PEM_DATA_ALARM_1,
  311. ALRM1_VIN_OUT_LIMIT | ALRM1_PRIMARY_FAULT);
  312. /* Currents */
  313. static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, pem_show_data, NULL,
  314. PEM_DATA_CURRENT);
  315. static SENSOR_DEVICE_ATTR_2(curr1_alarm, S_IRUGO, pem_show_bool, NULL,
  316. PEM_DATA_ALARM_1, ALRM1_VIN_OVERCURRENT);
  317. /* Power */
  318. static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, pem_show_input, NULL,
  319. PEM_INPUT_POWER_LSB);
  320. static SENSOR_DEVICE_ATTR_2(power1_alarm, S_IRUGO, pem_show_bool, NULL,
  321. PEM_DATA_ALARM_1, ALRM1_POWER_LIMIT);
  322. /* Fans */
  323. static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, pem_show_fan, NULL,
  324. PEM_FAN_FAN1);
  325. static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, pem_show_fan, NULL,
  326. PEM_FAN_FAN2);
  327. static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, pem_show_fan, NULL,
  328. PEM_FAN_FAN3);
  329. static SENSOR_DEVICE_ATTR_2(fan1_alarm, S_IRUGO, pem_show_bool, NULL,
  330. PEM_DATA_ALARM_2, ALRM2_FAN_FAULT);
  331. /* Temperatures */
  332. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, pem_show_data, NULL,
  333. PEM_DATA_TEMP);
  334. static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, pem_show_data, NULL,
  335. PEM_DATA_TEMP_MAX);
  336. static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, pem_show_data, NULL,
  337. PEM_DATA_TEMP_CRIT);
  338. static SENSOR_DEVICE_ATTR_2(temp1_alarm, S_IRUGO, pem_show_bool, NULL,
  339. PEM_DATA_ALARM_1, ALRM1_TEMP_WARNING);
  340. static SENSOR_DEVICE_ATTR_2(temp1_crit_alarm, S_IRUGO, pem_show_bool, NULL,
  341. PEM_DATA_ALARM_1, ALRM1_TEMP_SHUTDOWN);
  342. static SENSOR_DEVICE_ATTR_2(temp1_fault, S_IRUGO, pem_show_bool, NULL,
  343. PEM_DATA_ALARM_2, ALRM2_TEMP_FAULT);
  344. static struct attribute *pem_attributes[] = {
  345. &sensor_dev_attr_in1_input.dev_attr.attr,
  346. &sensor_dev_attr_in1_alarm.dev_attr.attr,
  347. &sensor_dev_attr_in1_crit_alarm.dev_attr.attr,
  348. &sensor_dev_attr_in2_alarm.dev_attr.attr,
  349. &sensor_dev_attr_curr1_alarm.dev_attr.attr,
  350. &sensor_dev_attr_power1_alarm.dev_attr.attr,
  351. &sensor_dev_attr_fan1_alarm.dev_attr.attr,
  352. &sensor_dev_attr_temp1_input.dev_attr.attr,
  353. &sensor_dev_attr_temp1_max.dev_attr.attr,
  354. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  355. &sensor_dev_attr_temp1_alarm.dev_attr.attr,
  356. &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
  357. &sensor_dev_attr_temp1_fault.dev_attr.attr,
  358. NULL,
  359. };
  360. static const struct attribute_group pem_group = {
  361. .attrs = pem_attributes,
  362. };
  363. static struct attribute *pem_input_attributes[] = {
  364. &sensor_dev_attr_in2_input.dev_attr.attr,
  365. &sensor_dev_attr_curr1_input.dev_attr.attr,
  366. &sensor_dev_attr_power1_input.dev_attr.attr,
  367. NULL
  368. };
  369. static const struct attribute_group pem_input_group = {
  370. .attrs = pem_input_attributes,
  371. };
  372. static struct attribute *pem_fan_attributes[] = {
  373. &sensor_dev_attr_fan1_input.dev_attr.attr,
  374. &sensor_dev_attr_fan2_input.dev_attr.attr,
  375. &sensor_dev_attr_fan3_input.dev_attr.attr,
  376. NULL
  377. };
  378. static const struct attribute_group pem_fan_group = {
  379. .attrs = pem_fan_attributes,
  380. };
  381. static int pem_probe(struct i2c_client *client,
  382. const struct i2c_device_id *id)
  383. {
  384. struct i2c_adapter *adapter = client->adapter;
  385. struct device *dev = &client->dev;
  386. struct device *hwmon_dev;
  387. struct pem_data *data;
  388. int ret, idx = 0;
  389. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BLOCK_DATA
  390. | I2C_FUNC_SMBUS_WRITE_BYTE))
  391. return -ENODEV;
  392. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  393. if (!data)
  394. return -ENOMEM;
  395. data->client = client;
  396. mutex_init(&data->update_lock);
  397. /*
  398. * We use the next two commands to determine if the device is really
  399. * there.
  400. */
  401. ret = pem_read_block(client, PEM_READ_FIRMWARE_REV,
  402. data->firmware_rev, sizeof(data->firmware_rev));
  403. if (ret < 0)
  404. return ret;
  405. ret = i2c_smbus_write_byte(client, PEM_CLEAR_INFO_FLAGS);
  406. if (ret < 0)
  407. return ret;
  408. dev_info(dev, "Firmware revision %d.%d.%d\n",
  409. data->firmware_rev[0], data->firmware_rev[1],
  410. data->firmware_rev[2]);
  411. /* sysfs hooks */
  412. data->groups[idx++] = &pem_group;
  413. /*
  414. * Check if input readings are supported.
  415. * This is the case if we can read input data,
  416. * and if the returned data is not all zeros.
  417. * Note that input alarms are always supported.
  418. */
  419. ret = pem_read_block(client, PEM_READ_INPUT_STRING,
  420. data->input_string,
  421. sizeof(data->input_string) - 1);
  422. if (!ret && (data->input_string[0] || data->input_string[1] ||
  423. data->input_string[2]))
  424. data->input_length = sizeof(data->input_string) - 1;
  425. else if (ret < 0) {
  426. /* Input string is one byte longer for some devices */
  427. ret = pem_read_block(client, PEM_READ_INPUT_STRING,
  428. data->input_string,
  429. sizeof(data->input_string));
  430. if (!ret && (data->input_string[0] || data->input_string[1] ||
  431. data->input_string[2] || data->input_string[3]))
  432. data->input_length = sizeof(data->input_string);
  433. }
  434. if (data->input_length)
  435. data->groups[idx++] = &pem_input_group;
  436. /*
  437. * Check if fan speed readings are supported.
  438. * This is the case if we can read fan speed data,
  439. * and if the returned data is not all zeros.
  440. * Note that the fan alarm is always supported.
  441. */
  442. ret = pem_read_block(client, PEM_READ_FAN_SPEED,
  443. data->fan_speed,
  444. sizeof(data->fan_speed));
  445. if (!ret && (data->fan_speed[0] || data->fan_speed[1] ||
  446. data->fan_speed[2] || data->fan_speed[3])) {
  447. data->fans_supported = true;
  448. data->groups[idx++] = &pem_fan_group;
  449. }
  450. hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
  451. data, data->groups);
  452. return PTR_ERR_OR_ZERO(hwmon_dev);
  453. }
  454. static const struct i2c_device_id pem_id[] = {
  455. {"lineage_pem", 0},
  456. {}
  457. };
  458. MODULE_DEVICE_TABLE(i2c, pem_id);
  459. static struct i2c_driver pem_driver = {
  460. .driver = {
  461. .name = "lineage_pem",
  462. },
  463. .probe = pem_probe,
  464. .id_table = pem_id,
  465. };
  466. module_i2c_driver(pem_driver);
  467. MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
  468. MODULE_DESCRIPTION("Lineage CPL PEM hardware monitoring driver");
  469. MODULE_LICENSE("GPL");