xgene-hwmon.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * APM X-Gene SoC Hardware Monitoring Driver
  4. *
  5. * Copyright (c) 2016, Applied Micro Circuits Corporation
  6. * Author: Loc Ho <lho@apm.com>
  7. * Hoan Tran <hotran@apm.com>
  8. *
  9. * This driver provides the following features:
  10. * - Retrieve CPU total power (uW)
  11. * - Retrieve IO total power (uW)
  12. * - Retrieve SoC temperature (milli-degree C) and alarm
  13. */
  14. #include <linux/acpi.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/hwmon.h>
  17. #include <linux/hwmon-sysfs.h>
  18. #include <linux/io.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/kfifo.h>
  21. #include <linux/mailbox_controller.h>
  22. #include <linux/mailbox_client.h>
  23. #include <linux/module.h>
  24. #include <linux/of.h>
  25. #include <linux/platform_device.h>
  26. #include <acpi/pcc.h>
  27. /* SLIMpro message defines */
  28. #define MSG_TYPE_DBG 0
  29. #define MSG_TYPE_ERR 7
  30. #define MSG_TYPE_PWRMGMT 9
  31. #define MSG_TYPE(v) (((v) & 0xF0000000) >> 28)
  32. #define MSG_TYPE_SET(v) (((v) << 28) & 0xF0000000)
  33. #define MSG_SUBTYPE(v) (((v) & 0x0F000000) >> 24)
  34. #define MSG_SUBTYPE_SET(v) (((v) << 24) & 0x0F000000)
  35. #define DBG_SUBTYPE_SENSOR_READ 4
  36. #define SENSOR_RD_MSG 0x04FFE902
  37. #define SENSOR_RD_EN_ADDR(a) ((a) & 0x000FFFFF)
  38. #define PMD_PWR_REG 0x20
  39. #define PMD_PWR_MW_REG 0x26
  40. #define SOC_PWR_REG 0x21
  41. #define SOC_PWR_MW_REG 0x27
  42. #define SOC_TEMP_REG 0x10
  43. #define TEMP_NEGATIVE_BIT 8
  44. #define SENSOR_INVALID_DATA BIT(15)
  45. #define PWRMGMT_SUBTYPE_TPC 1
  46. #define TPC_ALARM 2
  47. #define TPC_GET_ALARM 3
  48. #define TPC_CMD(v) (((v) & 0x00FF0000) >> 16)
  49. #define TPC_CMD_SET(v) (((v) << 16) & 0x00FF0000)
  50. #define TPC_EN_MSG(hndl, cmd, type) \
  51. (MSG_TYPE_SET(MSG_TYPE_PWRMGMT) | \
  52. MSG_SUBTYPE_SET(hndl) | TPC_CMD_SET(cmd) | type)
  53. /*
  54. * Arbitrary retries in case the remote processor is slow to respond
  55. * to PCC commands
  56. */
  57. #define PCC_NUM_RETRIES 500
  58. #define ASYNC_MSG_FIFO_SIZE 16
  59. #define MBOX_OP_TIMEOUTMS 1000
  60. #define WATT_TO_mWATT(x) ((x) * 1000)
  61. #define mWATT_TO_uWATT(x) ((x) * 1000)
  62. #define CELSIUS_TO_mCELSIUS(x) ((x) * 1000)
  63. #define to_xgene_hwmon_dev(cl) \
  64. container_of(cl, struct xgene_hwmon_dev, mbox_client)
  65. enum xgene_hwmon_version {
  66. XGENE_HWMON_V1 = 0,
  67. XGENE_HWMON_V2 = 1,
  68. };
  69. struct slimpro_resp_msg {
  70. u32 msg;
  71. u32 param1;
  72. u32 param2;
  73. } __packed;
  74. struct xgene_hwmon_dev {
  75. struct device *dev;
  76. struct mbox_chan *mbox_chan;
  77. struct pcc_mbox_chan *pcc_chan;
  78. struct mbox_client mbox_client;
  79. int mbox_idx;
  80. spinlock_t kfifo_lock;
  81. struct mutex rd_mutex;
  82. struct completion rd_complete;
  83. int resp_pending;
  84. struct slimpro_resp_msg sync_msg;
  85. struct work_struct workq;
  86. struct kfifo_rec_ptr_1 async_msg_fifo;
  87. struct device *hwmon_dev;
  88. bool temp_critical_alarm;
  89. unsigned int usecs_lat;
  90. };
  91. /*
  92. * This function tests and clears a bitmask then returns its old value
  93. */
  94. static u16 xgene_word_tst_and_clr(u16 *addr, u16 mask)
  95. {
  96. u16 ret, val;
  97. val = le16_to_cpu(READ_ONCE(*addr));
  98. ret = val & mask;
  99. val &= ~mask;
  100. WRITE_ONCE(*addr, cpu_to_le16(val));
  101. return ret;
  102. }
  103. static int xgene_hwmon_pcc_rd(struct xgene_hwmon_dev *ctx, u32 *msg)
  104. {
  105. struct acpi_pcct_shared_memory __iomem *generic_comm_base =
  106. ctx->pcc_chan->shmem;
  107. u32 *ptr = (void *)(generic_comm_base + 1);
  108. int rc, i;
  109. u16 val;
  110. mutex_lock(&ctx->rd_mutex);
  111. init_completion(&ctx->rd_complete);
  112. ctx->resp_pending = true;
  113. /* Write signature for subspace */
  114. WRITE_ONCE(generic_comm_base->signature,
  115. cpu_to_le32(PCC_SIGNATURE | ctx->mbox_idx));
  116. /* Write to the shared command region */
  117. WRITE_ONCE(generic_comm_base->command,
  118. cpu_to_le16(MSG_TYPE(msg[0]) | PCC_CMD_GENERATE_DB_INTR));
  119. /* Flip CMD COMPLETE bit */
  120. val = le16_to_cpu(READ_ONCE(generic_comm_base->status));
  121. val &= ~PCC_STATUS_CMD_COMPLETE;
  122. WRITE_ONCE(generic_comm_base->status, cpu_to_le16(val));
  123. /* Copy the message to the PCC comm space */
  124. for (i = 0; i < sizeof(struct slimpro_resp_msg) / 4; i++)
  125. WRITE_ONCE(ptr[i], cpu_to_le32(msg[i]));
  126. /* Ring the doorbell */
  127. rc = mbox_send_message(ctx->mbox_chan, msg);
  128. if (rc < 0) {
  129. dev_err(ctx->dev, "Mailbox send error %d\n", rc);
  130. goto err;
  131. }
  132. if (!wait_for_completion_timeout(&ctx->rd_complete,
  133. usecs_to_jiffies(ctx->usecs_lat))) {
  134. dev_err(ctx->dev, "Mailbox operation timed out\n");
  135. rc = -ETIMEDOUT;
  136. goto err;
  137. }
  138. /* Check for error message */
  139. if (MSG_TYPE(ctx->sync_msg.msg) == MSG_TYPE_ERR) {
  140. rc = -EINVAL;
  141. goto err;
  142. }
  143. msg[0] = ctx->sync_msg.msg;
  144. msg[1] = ctx->sync_msg.param1;
  145. msg[2] = ctx->sync_msg.param2;
  146. err:
  147. mbox_chan_txdone(ctx->mbox_chan, 0);
  148. ctx->resp_pending = false;
  149. mutex_unlock(&ctx->rd_mutex);
  150. return rc;
  151. }
  152. static int xgene_hwmon_rd(struct xgene_hwmon_dev *ctx, u32 *msg)
  153. {
  154. int rc;
  155. mutex_lock(&ctx->rd_mutex);
  156. init_completion(&ctx->rd_complete);
  157. ctx->resp_pending = true;
  158. rc = mbox_send_message(ctx->mbox_chan, msg);
  159. if (rc < 0) {
  160. dev_err(ctx->dev, "Mailbox send error %d\n", rc);
  161. goto err;
  162. }
  163. if (!wait_for_completion_timeout(&ctx->rd_complete,
  164. msecs_to_jiffies(MBOX_OP_TIMEOUTMS))) {
  165. dev_err(ctx->dev, "Mailbox operation timed out\n");
  166. rc = -ETIMEDOUT;
  167. goto err;
  168. }
  169. /* Check for error message */
  170. if (MSG_TYPE(ctx->sync_msg.msg) == MSG_TYPE_ERR) {
  171. rc = -EINVAL;
  172. goto err;
  173. }
  174. msg[0] = ctx->sync_msg.msg;
  175. msg[1] = ctx->sync_msg.param1;
  176. msg[2] = ctx->sync_msg.param2;
  177. err:
  178. ctx->resp_pending = false;
  179. mutex_unlock(&ctx->rd_mutex);
  180. return rc;
  181. }
  182. static int xgene_hwmon_reg_map_rd(struct xgene_hwmon_dev *ctx, u32 addr,
  183. u32 *data)
  184. {
  185. u32 msg[3];
  186. int rc;
  187. msg[0] = SENSOR_RD_MSG;
  188. msg[1] = SENSOR_RD_EN_ADDR(addr);
  189. msg[2] = 0;
  190. if (acpi_disabled)
  191. rc = xgene_hwmon_rd(ctx, msg);
  192. else
  193. rc = xgene_hwmon_pcc_rd(ctx, msg);
  194. if (rc < 0)
  195. return rc;
  196. /*
  197. * Check if sensor data is valid.
  198. */
  199. if (msg[1] & SENSOR_INVALID_DATA)
  200. return -ENODATA;
  201. *data = msg[1];
  202. return rc;
  203. }
  204. static int xgene_hwmon_get_notification_msg(struct xgene_hwmon_dev *ctx,
  205. u32 *amsg)
  206. {
  207. u32 msg[3];
  208. int rc;
  209. msg[0] = TPC_EN_MSG(PWRMGMT_SUBTYPE_TPC, TPC_GET_ALARM, 0);
  210. msg[1] = 0;
  211. msg[2] = 0;
  212. rc = xgene_hwmon_pcc_rd(ctx, msg);
  213. if (rc < 0)
  214. return rc;
  215. amsg[0] = msg[0];
  216. amsg[1] = msg[1];
  217. amsg[2] = msg[2];
  218. return rc;
  219. }
  220. static int xgene_hwmon_get_cpu_pwr(struct xgene_hwmon_dev *ctx, u32 *val)
  221. {
  222. u32 watt, mwatt;
  223. int rc;
  224. rc = xgene_hwmon_reg_map_rd(ctx, PMD_PWR_REG, &watt);
  225. if (rc < 0)
  226. return rc;
  227. rc = xgene_hwmon_reg_map_rd(ctx, PMD_PWR_MW_REG, &mwatt);
  228. if (rc < 0)
  229. return rc;
  230. *val = WATT_TO_mWATT(watt) + mwatt;
  231. return 0;
  232. }
  233. static int xgene_hwmon_get_io_pwr(struct xgene_hwmon_dev *ctx, u32 *val)
  234. {
  235. u32 watt, mwatt;
  236. int rc;
  237. rc = xgene_hwmon_reg_map_rd(ctx, SOC_PWR_REG, &watt);
  238. if (rc < 0)
  239. return rc;
  240. rc = xgene_hwmon_reg_map_rd(ctx, SOC_PWR_MW_REG, &mwatt);
  241. if (rc < 0)
  242. return rc;
  243. *val = WATT_TO_mWATT(watt) + mwatt;
  244. return 0;
  245. }
  246. static int xgene_hwmon_get_temp(struct xgene_hwmon_dev *ctx, u32 *val)
  247. {
  248. return xgene_hwmon_reg_map_rd(ctx, SOC_TEMP_REG, val);
  249. }
  250. /*
  251. * Sensor temperature/power functions
  252. */
  253. static ssize_t temp1_input_show(struct device *dev,
  254. struct device_attribute *attr,
  255. char *buf)
  256. {
  257. struct xgene_hwmon_dev *ctx = dev_get_drvdata(dev);
  258. int rc, temp;
  259. u32 val;
  260. rc = xgene_hwmon_get_temp(ctx, &val);
  261. if (rc < 0)
  262. return rc;
  263. temp = sign_extend32(val, TEMP_NEGATIVE_BIT);
  264. return sysfs_emit(buf, "%d\n", CELSIUS_TO_mCELSIUS(temp));
  265. }
  266. static ssize_t temp1_label_show(struct device *dev,
  267. struct device_attribute *attr,
  268. char *buf)
  269. {
  270. return sysfs_emit(buf, "SoC Temperature\n");
  271. }
  272. static ssize_t temp1_critical_alarm_show(struct device *dev,
  273. struct device_attribute *devattr,
  274. char *buf)
  275. {
  276. struct xgene_hwmon_dev *ctx = dev_get_drvdata(dev);
  277. return sysfs_emit(buf, "%d\n", ctx->temp_critical_alarm);
  278. }
  279. static ssize_t power1_label_show(struct device *dev,
  280. struct device_attribute *attr,
  281. char *buf)
  282. {
  283. return sysfs_emit(buf, "CPU power\n");
  284. }
  285. static ssize_t power2_label_show(struct device *dev,
  286. struct device_attribute *attr,
  287. char *buf)
  288. {
  289. return sysfs_emit(buf, "IO power\n");
  290. }
  291. static ssize_t power1_input_show(struct device *dev,
  292. struct device_attribute *attr,
  293. char *buf)
  294. {
  295. struct xgene_hwmon_dev *ctx = dev_get_drvdata(dev);
  296. u32 val;
  297. int rc;
  298. rc = xgene_hwmon_get_cpu_pwr(ctx, &val);
  299. if (rc < 0)
  300. return rc;
  301. return sysfs_emit(buf, "%u\n", mWATT_TO_uWATT(val));
  302. }
  303. static ssize_t power2_input_show(struct device *dev,
  304. struct device_attribute *attr,
  305. char *buf)
  306. {
  307. struct xgene_hwmon_dev *ctx = dev_get_drvdata(dev);
  308. u32 val;
  309. int rc;
  310. rc = xgene_hwmon_get_io_pwr(ctx, &val);
  311. if (rc < 0)
  312. return rc;
  313. return sysfs_emit(buf, "%u\n", mWATT_TO_uWATT(val));
  314. }
  315. static DEVICE_ATTR_RO(temp1_label);
  316. static DEVICE_ATTR_RO(temp1_input);
  317. static DEVICE_ATTR_RO(temp1_critical_alarm);
  318. static DEVICE_ATTR_RO(power1_label);
  319. static DEVICE_ATTR_RO(power1_input);
  320. static DEVICE_ATTR_RO(power2_label);
  321. static DEVICE_ATTR_RO(power2_input);
  322. static struct attribute *xgene_hwmon_attrs[] = {
  323. &dev_attr_temp1_label.attr,
  324. &dev_attr_temp1_input.attr,
  325. &dev_attr_temp1_critical_alarm.attr,
  326. &dev_attr_power1_label.attr,
  327. &dev_attr_power1_input.attr,
  328. &dev_attr_power2_label.attr,
  329. &dev_attr_power2_input.attr,
  330. NULL,
  331. };
  332. ATTRIBUTE_GROUPS(xgene_hwmon);
  333. static int xgene_hwmon_tpc_alarm(struct xgene_hwmon_dev *ctx,
  334. struct slimpro_resp_msg *amsg)
  335. {
  336. ctx->temp_critical_alarm = !!amsg->param2;
  337. sysfs_notify(&ctx->dev->kobj, NULL, "temp1_critical_alarm");
  338. return 0;
  339. }
  340. static void xgene_hwmon_process_pwrmsg(struct xgene_hwmon_dev *ctx,
  341. struct slimpro_resp_msg *amsg)
  342. {
  343. if ((MSG_SUBTYPE(amsg->msg) == PWRMGMT_SUBTYPE_TPC) &&
  344. (TPC_CMD(amsg->msg) == TPC_ALARM))
  345. xgene_hwmon_tpc_alarm(ctx, amsg);
  346. }
  347. /*
  348. * This function is called to process async work queue
  349. */
  350. static void xgene_hwmon_evt_work(struct work_struct *work)
  351. {
  352. struct slimpro_resp_msg amsg;
  353. struct xgene_hwmon_dev *ctx;
  354. int ret;
  355. ctx = container_of(work, struct xgene_hwmon_dev, workq);
  356. while (kfifo_out_spinlocked(&ctx->async_msg_fifo, &amsg,
  357. sizeof(struct slimpro_resp_msg),
  358. &ctx->kfifo_lock)) {
  359. /*
  360. * If PCC, send a consumer command to Platform to get info
  361. * If Slimpro Mailbox, get message from specific FIFO
  362. */
  363. if (!acpi_disabled) {
  364. ret = xgene_hwmon_get_notification_msg(ctx,
  365. (u32 *)&amsg);
  366. if (ret < 0)
  367. continue;
  368. }
  369. if (MSG_TYPE(amsg.msg) == MSG_TYPE_PWRMGMT)
  370. xgene_hwmon_process_pwrmsg(ctx, &amsg);
  371. }
  372. }
  373. static int xgene_hwmon_rx_ready(struct xgene_hwmon_dev *ctx, void *msg)
  374. {
  375. if (IS_ERR_OR_NULL(ctx->hwmon_dev) && !ctx->resp_pending) {
  376. /* Enqueue to the FIFO */
  377. kfifo_in_spinlocked(&ctx->async_msg_fifo, msg,
  378. sizeof(struct slimpro_resp_msg),
  379. &ctx->kfifo_lock);
  380. return -ENODEV;
  381. }
  382. return 0;
  383. }
  384. /*
  385. * This function is called when the SLIMpro Mailbox received a message
  386. */
  387. static void xgene_hwmon_rx_cb(struct mbox_client *cl, void *msg)
  388. {
  389. struct xgene_hwmon_dev *ctx = to_xgene_hwmon_dev(cl);
  390. /*
  391. * While the driver registers with the mailbox framework, an interrupt
  392. * can be pending before the probe function completes its
  393. * initialization. If such condition occurs, just queue up the message
  394. * as the driver is not ready for servicing the callback.
  395. */
  396. if (xgene_hwmon_rx_ready(ctx, msg) < 0)
  397. return;
  398. /*
  399. * Response message format:
  400. * msg[0] is the return code of the operation
  401. * msg[1] is the first parameter word
  402. * msg[2] is the second parameter word
  403. *
  404. * As message only supports dword size, just assign it.
  405. */
  406. /* Check for sync query */
  407. if (ctx->resp_pending &&
  408. ((MSG_TYPE(((u32 *)msg)[0]) == MSG_TYPE_ERR) ||
  409. (MSG_TYPE(((u32 *)msg)[0]) == MSG_TYPE_DBG &&
  410. MSG_SUBTYPE(((u32 *)msg)[0]) == DBG_SUBTYPE_SENSOR_READ) ||
  411. (MSG_TYPE(((u32 *)msg)[0]) == MSG_TYPE_PWRMGMT &&
  412. MSG_SUBTYPE(((u32 *)msg)[0]) == PWRMGMT_SUBTYPE_TPC &&
  413. TPC_CMD(((u32 *)msg)[0]) == TPC_ALARM))) {
  414. ctx->sync_msg.msg = ((u32 *)msg)[0];
  415. ctx->sync_msg.param1 = ((u32 *)msg)[1];
  416. ctx->sync_msg.param2 = ((u32 *)msg)[2];
  417. /* Operation waiting for response */
  418. complete(&ctx->rd_complete);
  419. return;
  420. }
  421. /* Enqueue to the FIFO */
  422. kfifo_in_spinlocked(&ctx->async_msg_fifo, msg,
  423. sizeof(struct slimpro_resp_msg), &ctx->kfifo_lock);
  424. /* Schedule the bottom handler */
  425. schedule_work(&ctx->workq);
  426. }
  427. /*
  428. * This function is called when the PCC Mailbox received a message
  429. */
  430. static void xgene_hwmon_pcc_rx_cb(struct mbox_client *cl, void *msg)
  431. {
  432. struct xgene_hwmon_dev *ctx = to_xgene_hwmon_dev(cl);
  433. struct acpi_pcct_shared_memory __iomem *generic_comm_base =
  434. ctx->pcc_chan->shmem;
  435. struct slimpro_resp_msg amsg;
  436. /*
  437. * While the driver registers with the mailbox framework, an interrupt
  438. * can be pending before the probe function completes its
  439. * initialization. If such condition occurs, just queue up the message
  440. * as the driver is not ready for servicing the callback.
  441. */
  442. if (xgene_hwmon_rx_ready(ctx, &amsg) < 0)
  443. return;
  444. msg = generic_comm_base + 1;
  445. /* Check if platform sends interrupt */
  446. if (!xgene_word_tst_and_clr(&generic_comm_base->status,
  447. PCC_STATUS_SCI_DOORBELL))
  448. return;
  449. /*
  450. * Response message format:
  451. * msg[0] is the return code of the operation
  452. * msg[1] is the first parameter word
  453. * msg[2] is the second parameter word
  454. *
  455. * As message only supports dword size, just assign it.
  456. */
  457. /* Check for sync query */
  458. if (ctx->resp_pending &&
  459. ((MSG_TYPE(((u32 *)msg)[0]) == MSG_TYPE_ERR) ||
  460. (MSG_TYPE(((u32 *)msg)[0]) == MSG_TYPE_DBG &&
  461. MSG_SUBTYPE(((u32 *)msg)[0]) == DBG_SUBTYPE_SENSOR_READ) ||
  462. (MSG_TYPE(((u32 *)msg)[0]) == MSG_TYPE_PWRMGMT &&
  463. MSG_SUBTYPE(((u32 *)msg)[0]) == PWRMGMT_SUBTYPE_TPC &&
  464. TPC_CMD(((u32 *)msg)[0]) == TPC_ALARM))) {
  465. /* Check if platform completes command */
  466. if (xgene_word_tst_and_clr(&generic_comm_base->status,
  467. PCC_STATUS_CMD_COMPLETE)) {
  468. ctx->sync_msg.msg = ((u32 *)msg)[0];
  469. ctx->sync_msg.param1 = ((u32 *)msg)[1];
  470. ctx->sync_msg.param2 = ((u32 *)msg)[2];
  471. /* Operation waiting for response */
  472. complete(&ctx->rd_complete);
  473. return;
  474. }
  475. }
  476. /*
  477. * Platform notifies interrupt to OSPM.
  478. * OPSM schedules a consumer command to get this information
  479. * in a workqueue. Platform must wait until OSPM has issued
  480. * a consumer command that serves this notification.
  481. */
  482. /* Enqueue to the FIFO */
  483. kfifo_in_spinlocked(&ctx->async_msg_fifo, &amsg,
  484. sizeof(struct slimpro_resp_msg), &ctx->kfifo_lock);
  485. /* Schedule the bottom handler */
  486. schedule_work(&ctx->workq);
  487. }
  488. static void xgene_hwmon_tx_done(struct mbox_client *cl, void *msg, int ret)
  489. {
  490. if (ret) {
  491. dev_dbg(cl->dev, "TX did not complete: CMD sent:%x, ret:%d\n",
  492. *(u16 *)msg, ret);
  493. } else {
  494. dev_dbg(cl->dev, "TX completed. CMD sent:%x, ret:%d\n",
  495. *(u16 *)msg, ret);
  496. }
  497. }
  498. #ifdef CONFIG_ACPI
  499. static const struct acpi_device_id xgene_hwmon_acpi_match[] = {
  500. {"APMC0D29", XGENE_HWMON_V1},
  501. {"APMC0D8A", XGENE_HWMON_V2},
  502. {},
  503. };
  504. MODULE_DEVICE_TABLE(acpi, xgene_hwmon_acpi_match);
  505. #endif
  506. static int xgene_hwmon_probe(struct platform_device *pdev)
  507. {
  508. struct xgene_hwmon_dev *ctx;
  509. struct mbox_client *cl;
  510. int rc;
  511. ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
  512. if (!ctx)
  513. return -ENOMEM;
  514. ctx->dev = &pdev->dev;
  515. platform_set_drvdata(pdev, ctx);
  516. cl = &ctx->mbox_client;
  517. spin_lock_init(&ctx->kfifo_lock);
  518. mutex_init(&ctx->rd_mutex);
  519. rc = kfifo_alloc(&ctx->async_msg_fifo,
  520. sizeof(struct slimpro_resp_msg) * ASYNC_MSG_FIFO_SIZE,
  521. GFP_KERNEL);
  522. if (rc)
  523. return -ENOMEM;
  524. INIT_WORK(&ctx->workq, xgene_hwmon_evt_work);
  525. /* Request mailbox channel */
  526. cl->dev = &pdev->dev;
  527. cl->tx_done = xgene_hwmon_tx_done;
  528. cl->tx_block = false;
  529. cl->tx_tout = MBOX_OP_TIMEOUTMS;
  530. cl->knows_txdone = false;
  531. if (acpi_disabled) {
  532. cl->rx_callback = xgene_hwmon_rx_cb;
  533. ctx->mbox_chan = mbox_request_channel(cl, 0);
  534. if (IS_ERR(ctx->mbox_chan)) {
  535. dev_err(&pdev->dev,
  536. "SLIMpro mailbox channel request failed\n");
  537. rc = -ENODEV;
  538. goto out_mbox_free;
  539. }
  540. } else {
  541. struct pcc_mbox_chan *pcc_chan;
  542. const struct acpi_device_id *acpi_id;
  543. acpi_id = acpi_match_device(pdev->dev.driver->acpi_match_table,
  544. &pdev->dev);
  545. if (!acpi_id) {
  546. rc = -EINVAL;
  547. goto out_mbox_free;
  548. }
  549. if (device_property_read_u32(&pdev->dev, "pcc-channel",
  550. &ctx->mbox_idx)) {
  551. dev_err(&pdev->dev, "no pcc-channel property\n");
  552. rc = -ENODEV;
  553. goto out_mbox_free;
  554. }
  555. cl->rx_callback = xgene_hwmon_pcc_rx_cb;
  556. pcc_chan = pcc_mbox_request_channel(cl, ctx->mbox_idx);
  557. if (IS_ERR(pcc_chan)) {
  558. dev_err(&pdev->dev,
  559. "PPC channel request failed\n");
  560. rc = -ENODEV;
  561. goto out_mbox_free;
  562. }
  563. ctx->pcc_chan = pcc_chan;
  564. ctx->mbox_chan = pcc_chan->mchan;
  565. if (!ctx->mbox_chan->mbox->txdone_irq) {
  566. dev_err(&pdev->dev, "PCC IRQ not supported\n");
  567. rc = -ENODEV;
  568. goto out;
  569. }
  570. /*
  571. * pcc_chan->latency is just a Nominal value. In reality
  572. * the remote processor could be much slower to reply.
  573. * So add an arbitrary amount of wait on top of Nominal.
  574. */
  575. ctx->usecs_lat = PCC_NUM_RETRIES * pcc_chan->latency;
  576. }
  577. ctx->hwmon_dev = hwmon_device_register_with_groups(ctx->dev,
  578. "apm_xgene",
  579. ctx,
  580. xgene_hwmon_groups);
  581. if (IS_ERR(ctx->hwmon_dev)) {
  582. dev_err(&pdev->dev, "Failed to register HW monitor device\n");
  583. rc = PTR_ERR(ctx->hwmon_dev);
  584. goto out;
  585. }
  586. /*
  587. * Schedule the bottom handler if there is a pending message.
  588. */
  589. schedule_work(&ctx->workq);
  590. dev_info(&pdev->dev, "APM X-Gene SoC HW monitor driver registered\n");
  591. return 0;
  592. out:
  593. if (acpi_disabled)
  594. mbox_free_channel(ctx->mbox_chan);
  595. else
  596. pcc_mbox_free_channel(ctx->pcc_chan);
  597. out_mbox_free:
  598. kfifo_free(&ctx->async_msg_fifo);
  599. return rc;
  600. }
  601. static void xgene_hwmon_remove(struct platform_device *pdev)
  602. {
  603. struct xgene_hwmon_dev *ctx = platform_get_drvdata(pdev);
  604. cancel_work_sync(&ctx->workq);
  605. hwmon_device_unregister(ctx->hwmon_dev);
  606. kfifo_free(&ctx->async_msg_fifo);
  607. if (acpi_disabled)
  608. mbox_free_channel(ctx->mbox_chan);
  609. else
  610. pcc_mbox_free_channel(ctx->pcc_chan);
  611. }
  612. static const struct of_device_id xgene_hwmon_of_match[] = {
  613. {.compatible = "apm,xgene-slimpro-hwmon"},
  614. {}
  615. };
  616. MODULE_DEVICE_TABLE(of, xgene_hwmon_of_match);
  617. static struct platform_driver xgene_hwmon_driver = {
  618. .probe = xgene_hwmon_probe,
  619. .remove = xgene_hwmon_remove,
  620. .driver = {
  621. .name = "xgene-slimpro-hwmon",
  622. .of_match_table = xgene_hwmon_of_match,
  623. .acpi_match_table = ACPI_PTR(xgene_hwmon_acpi_match),
  624. },
  625. };
  626. module_platform_driver(xgene_hwmon_driver);
  627. MODULE_DESCRIPTION("APM X-Gene SoC hardware monitor");
  628. MODULE_LICENSE("GPL");