bcm-voter.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2020-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2023, Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <asm/div64.h>
  7. #include <linux/interconnect-provider.h>
  8. #include <linux/list_sort.h>
  9. #include <linux/module.h>
  10. #include <linux/of.h>
  11. #include <linux/platform_device.h>
  12. #include <soc/qcom/rpmh.h>
  13. #include <soc/qcom/tcs.h>
  14. #include "bcm-voter.h"
  15. #include "icc-rpmh.h"
  16. #define CREATE_TRACE_POINTS
  17. #include "trace.h"
  18. static LIST_HEAD(bcm_voters);
  19. static DEFINE_MUTEX(bcm_voter_lock);
  20. /**
  21. * struct bcm_voter - Bus Clock Manager voter
  22. * @dev: reference to the device that communicates with the BCM
  23. * @np: reference to the device node to match bcm voters
  24. * @lock: mutex to protect commit and wake/sleep lists in the voter
  25. * @commit_list: list containing bcms to be committed to hardware
  26. * @ws_list: list containing bcms that have different wake/sleep votes
  27. * @voter_node: list of bcm voters
  28. * @tcs_wait: mask for which buckets require TCS completion
  29. * @has_amc: flag to determine if this voter supports AMC
  30. * @init: flag to determine when init has completed.
  31. */
  32. struct bcm_voter {
  33. struct device *dev;
  34. struct device_node *np;
  35. struct qcom_icc_crm_voter *crm;
  36. struct mutex lock;
  37. struct list_head commit_list;
  38. struct list_head ws_list;
  39. struct list_head voter_node;
  40. u32 tcs_wait;
  41. bool has_amc;
  42. bool init;
  43. };
  44. static int cmp_vcd(void *priv, const struct list_head *a, const struct list_head *b)
  45. {
  46. const struct qcom_icc_bcm *bcm_a = list_entry(a, struct qcom_icc_bcm, list);
  47. const struct qcom_icc_bcm *bcm_b = list_entry(b, struct qcom_icc_bcm, list);
  48. return bcm_a->aux_data.vcd - bcm_b->aux_data.vcd;
  49. }
  50. static u64 bcm_div(u64 num, u32 base)
  51. {
  52. /* Ensure that small votes aren't lost. */
  53. if (num && num < base)
  54. return 1;
  55. do_div(num, base);
  56. return num;
  57. }
  58. /* BCMs with enable_mask use one-hot-encoding for on/off signaling */
  59. static void bcm_aggregate_mask(struct qcom_icc_bcm *bcm)
  60. {
  61. struct qcom_icc_node *node;
  62. int bucket, i;
  63. for (bucket = 0; bucket < QCOM_ICC_NUM_BUCKETS; bucket++) {
  64. bcm->vote_x[bucket] = 0;
  65. bcm->vote_y[bucket] = 0;
  66. for (i = 0; i < bcm->num_nodes; i++) {
  67. node = bcm->nodes[i];
  68. /* If any vote in this bucket exists, keep the BCM enabled */
  69. if (node->sum_avg[bucket] || node->max_peak[bucket])
  70. bcm->vote_y[bucket] |= bcm->enable_mask;
  71. if (node->perf_mode[bucket])
  72. bcm->vote_y[bucket] |= bcm->perf_mode_mask;
  73. }
  74. }
  75. if (bcm->keepalive) {
  76. bcm->vote_y[QCOM_ICC_BUCKET_AMC] |= bcm->enable_mask;
  77. bcm->vote_y[QCOM_ICC_BUCKET_WAKE] |= bcm->enable_mask;
  78. }
  79. }
  80. static void bcm_aggregate(struct qcom_icc_bcm *bcm, bool init)
  81. {
  82. struct qcom_icc_node *node;
  83. size_t i, bucket;
  84. u64 agg_avg[QCOM_ICC_NUM_BUCKETS] = {0};
  85. u64 agg_peak[QCOM_ICC_NUM_BUCKETS] = {0};
  86. u64 temp;
  87. for (bucket = 0; bucket < QCOM_ICC_NUM_BUCKETS; bucket++) {
  88. for (i = 0; i < bcm->num_nodes; i++) {
  89. node = bcm->nodes[i];
  90. temp = bcm_div(node->sum_avg[bucket] * bcm->aux_data.width,
  91. node->buswidth * node->channels);
  92. agg_avg[bucket] = max(agg_avg[bucket], temp);
  93. temp = bcm_div(node->max_peak[bucket] * bcm->aux_data.width,
  94. node->buswidth);
  95. agg_peak[bucket] = max(agg_peak[bucket], temp);
  96. }
  97. temp = agg_avg[bucket] * bcm->vote_scale;
  98. bcm->vote_x[bucket] = bcm_div(temp, bcm->aux_data.unit);
  99. temp = agg_peak[bucket] * bcm->vote_scale;
  100. bcm->vote_y[bucket] = bcm_div(temp, bcm->aux_data.unit);
  101. }
  102. if (bcm->keepalive || bcm->keepalive_early) {
  103. /*
  104. * Keepalive should normally only be enforced for AMC/WAKE so
  105. * that BCMs are only kept alive when HLOS is active. But early
  106. * during init all clients haven't had a chance to vot yet, and
  107. * some have use cases that persist when HLOS is asleep. So
  108. * during init vote to all sets, including SLEEP.
  109. */
  110. if (init) {
  111. bcm->vote_x[QCOM_ICC_BUCKET_AMC] = 16000;
  112. bcm->vote_x[QCOM_ICC_BUCKET_WAKE] = 16000;
  113. bcm->vote_x[QCOM_ICC_BUCKET_SLEEP] = 16000;
  114. bcm->vote_y[QCOM_ICC_BUCKET_AMC] = 16000;
  115. bcm->vote_y[QCOM_ICC_BUCKET_WAKE] = 16000;
  116. bcm->vote_y[QCOM_ICC_BUCKET_SLEEP] = 16000;
  117. } else if (bcm->vote_x[QCOM_ICC_BUCKET_AMC] == 0 &&
  118. bcm->vote_y[QCOM_ICC_BUCKET_AMC] == 0) {
  119. bcm->vote_x[QCOM_ICC_BUCKET_AMC] = 1;
  120. bcm->vote_x[QCOM_ICC_BUCKET_WAKE] = 1;
  121. bcm->vote_y[QCOM_ICC_BUCKET_AMC] = 1;
  122. bcm->vote_y[QCOM_ICC_BUCKET_WAKE] = 1;
  123. }
  124. }
  125. }
  126. static inline void tcs_cmd_gen(struct tcs_cmd *cmd, u64 vote_x, u64 vote_y,
  127. u32 addr, bool commit, bool wait)
  128. {
  129. bool valid = true;
  130. if (!cmd)
  131. return;
  132. memset(cmd, 0, sizeof(*cmd));
  133. if (vote_x == 0 && vote_y == 0)
  134. valid = false;
  135. if (vote_x > BCM_TCS_CMD_VOTE_MASK)
  136. vote_x = BCM_TCS_CMD_VOTE_MASK;
  137. if (vote_y > BCM_TCS_CMD_VOTE_MASK)
  138. vote_y = BCM_TCS_CMD_VOTE_MASK;
  139. cmd->addr = addr;
  140. cmd->data = BCM_TCS_CMD(commit, valid, vote_x, vote_y);
  141. /*
  142. * Set the wait for completion flag on command that need to be completed
  143. * before the next command.
  144. */
  145. cmd->wait = wait;
  146. }
  147. static void tcs_list_gen(struct bcm_voter *voter, int bucket,
  148. struct tcs_cmd tcs_list[MAX_VCD],
  149. int n[MAX_VCD + 1])
  150. {
  151. struct list_head *bcm_list = &voter->commit_list;
  152. struct qcom_icc_bcm *bcm;
  153. bool commit, wait;
  154. size_t idx = 0, batch = 0, cur_vcd_size = 0;
  155. memset(n, 0, sizeof(int) * (MAX_VCD + 1));
  156. list_for_each_entry(bcm, bcm_list, list) {
  157. commit = false;
  158. cur_vcd_size++;
  159. if ((list_is_last(&bcm->list, bcm_list)) ||
  160. bcm->aux_data.vcd != list_next_entry(bcm, list)->aux_data.vcd) {
  161. commit = true;
  162. cur_vcd_size = 0;
  163. }
  164. wait = commit && (voter->tcs_wait & BIT(bucket));
  165. tcs_cmd_gen(&tcs_list[idx], bcm->vote_x[bucket],
  166. bcm->vote_y[bucket], bcm->addr, commit, wait);
  167. idx++;
  168. n[batch]++;
  169. /*
  170. * Batch the BCMs in such a way that we do not split them in
  171. * multiple payloads when they are under the same VCD. This is
  172. * to ensure that every BCM is committed since we only set the
  173. * commit bit on the last BCM request of every VCD.
  174. */
  175. if (n[batch] >= MAX_RPMH_PAYLOAD) {
  176. if (!commit) {
  177. n[batch] -= cur_vcd_size;
  178. n[batch + 1] = cur_vcd_size;
  179. }
  180. batch++;
  181. }
  182. }
  183. }
  184. /**
  185. * of_bcm_voter_get - gets a bcm voter handle from DT node
  186. * @dev: device pointer for the consumer device
  187. * @name: name for the bcm voter device
  188. *
  189. * This function will match a device_node pointer for the phandle
  190. * specified in the device DT and return a bcm_voter handle on success.
  191. *
  192. * Returns bcm_voter pointer or ERR_PTR() on error. EPROBE_DEFER is returned
  193. * when matching bcm voter is yet to be found.
  194. */
  195. struct bcm_voter *of_bcm_voter_get(struct device *dev, const char *name)
  196. {
  197. struct bcm_voter *voter = ERR_PTR(-EPROBE_DEFER);
  198. struct bcm_voter *temp;
  199. struct device_node *np, *node;
  200. int idx = 0;
  201. if (!dev || !dev->of_node)
  202. return ERR_PTR(-ENODEV);
  203. np = dev->of_node;
  204. if (name) {
  205. idx = of_property_match_string(np, "qcom,bcm-voter-names", name);
  206. if (idx < 0)
  207. return ERR_PTR(idx);
  208. }
  209. node = of_parse_phandle(np, "qcom,bcm-voters", idx);
  210. mutex_lock(&bcm_voter_lock);
  211. list_for_each_entry(temp, &bcm_voters, voter_node) {
  212. if (temp->np == node) {
  213. voter = temp;
  214. break;
  215. }
  216. }
  217. mutex_unlock(&bcm_voter_lock);
  218. of_node_put(node);
  219. return voter;
  220. }
  221. EXPORT_SYMBOL_GPL(of_bcm_voter_get);
  222. /**
  223. * qcom_icc_bcm_voter_exist - checks if the bcm voter exists
  224. * @voter: voter that needs to checked against available bcm voters
  225. *
  226. * Returns true incase bcm_voter exists else false
  227. */
  228. static bool qcom_icc_bcm_voter_exist(struct bcm_voter *voter)
  229. {
  230. bool exists = false;
  231. struct bcm_voter *temp;
  232. if (voter) {
  233. mutex_lock(&bcm_voter_lock);
  234. list_for_each_entry(temp, &bcm_voters, voter_node) {
  235. if (temp == voter) {
  236. exists = true;
  237. break;
  238. }
  239. }
  240. mutex_unlock(&bcm_voter_lock);
  241. }
  242. return exists;
  243. }
  244. /**
  245. * qcom_icc_bcm_voter_add - queues up the bcm nodes that require updates
  246. * @voter: voter that the bcms are being added to
  247. * @bcm: bcm to add to the commit and wake sleep list
  248. */
  249. void qcom_icc_bcm_voter_add(struct bcm_voter *voter, struct qcom_icc_bcm *bcm)
  250. {
  251. if (!voter)
  252. return;
  253. if (!qcom_icc_bcm_voter_exist(voter))
  254. return;
  255. mutex_lock(&voter->lock);
  256. if (list_empty(&bcm->list))
  257. list_add_tail(&bcm->list, &voter->commit_list);
  258. if (list_empty(&bcm->ws_list))
  259. list_add_tail(&bcm->ws_list, &voter->ws_list);
  260. mutex_unlock(&voter->lock);
  261. }
  262. EXPORT_SYMBOL_GPL(qcom_icc_bcm_voter_add);
  263. static void qcom_icc_bcm_log(struct bcm_voter *voter, enum rpmh_state state,
  264. const struct tcs_cmd *cmd, const u32 *commit_idx)
  265. {
  266. static const char * const rpmh_state[] = {
  267. "RPMH_SLEEP_STATE",
  268. "RPMH_WAKE_ONLY_STATE",
  269. "RPMH_ACTIVE_ONLY_STATE"
  270. };
  271. int i, count = 0;
  272. if (!cmd || !commit_idx)
  273. return;
  274. while (commit_idx[count] > 0)
  275. count++;
  276. if (!count)
  277. return;
  278. for (i = 0; i < count; i++)
  279. trace_bcm_voter_commit(rpmh_state[state], cmd);
  280. }
  281. static int commit_rpmh(struct bcm_voter *voter)
  282. {
  283. struct qcom_icc_bcm *bcm;
  284. struct qcom_icc_bcm *bcm_tmp;
  285. int commit_idx[MAX_VCD + 1];
  286. struct tcs_cmd cmds[MAX_BCMS];
  287. int ret = 0;
  288. if (voter->has_amc) {
  289. /*
  290. * Pre sort the BCMs based on VCD for ease of generating a command list
  291. * that groups the BCMs with the same VCD together. VCDs are numbered
  292. * with lowest being the most expensive time wise, ensuring that
  293. * those commands are being sent the earliest in the queue. This needs
  294. * to be sorted every commit since we can't guarantee the order in which
  295. * the BCMs are added to the list.
  296. */
  297. list_sort(NULL, &voter->commit_list, cmp_vcd);
  298. /*
  299. * Construct the command list based on a pre ordered list of BCMs
  300. * based on VCD.
  301. */
  302. tcs_list_gen(voter, QCOM_ICC_BUCKET_AMC, cmds, commit_idx);
  303. if (!commit_idx[0])
  304. goto out;
  305. qcom_icc_bcm_log(voter, RPMH_ACTIVE_ONLY_STATE, cmds, commit_idx);
  306. ret = rpmh_write_batch(voter->dev, RPMH_ACTIVE_ONLY_STATE,
  307. cmds, commit_idx);
  308. /*
  309. * Ignore -EBUSY for AMC requests, since this can only happen for AMC
  310. * requests when the RSC is in solver mode. We can only be in solver
  311. * mode at the time of request for secondary RSCs (e.g. Display RSC),
  312. * since the primary Apps RSC is only in solver mode while
  313. * entering/exiting power collapse when SW isn't running. The -EBUSY
  314. * response is expected in solver and is a non-issue, since we just
  315. * want the request to apply to the WAKE set in that case instead.
  316. * Interconnect doesn't know when the RSC is in solver, so just always
  317. * send AMC and ignore the harmless error response.
  318. */
  319. if (ret && ret != -EBUSY) {
  320. pr_err("Error sending AMC RPMH requests (%d)\n", ret);
  321. goto out;
  322. }
  323. }
  324. rpmh_invalidate(voter->dev);
  325. list_for_each_entry_safe(bcm, bcm_tmp, &voter->commit_list, list)
  326. list_del_init(&bcm->list);
  327. list_for_each_entry_safe(bcm, bcm_tmp, &voter->ws_list, ws_list) {
  328. /*
  329. * Only generate WAKE and SLEEP commands if a resource's
  330. * requirements change as the execution environment transitions
  331. * between different power states.
  332. */
  333. if (!voter->has_amc ||
  334. bcm->vote_x[QCOM_ICC_BUCKET_WAKE] != bcm->vote_x[QCOM_ICC_BUCKET_SLEEP] ||
  335. bcm->vote_y[QCOM_ICC_BUCKET_WAKE] != bcm->vote_y[QCOM_ICC_BUCKET_SLEEP])
  336. list_add_tail(&bcm->list, &voter->commit_list);
  337. else
  338. list_del_init(&bcm->ws_list);
  339. }
  340. if (list_empty(&voter->commit_list))
  341. goto out;
  342. list_sort(NULL, &voter->commit_list, cmp_vcd);
  343. tcs_list_gen(voter, QCOM_ICC_BUCKET_WAKE, cmds, commit_idx);
  344. qcom_icc_bcm_log(voter, RPMH_WAKE_ONLY_STATE, cmds, commit_idx);
  345. ret = rpmh_write_batch(voter->dev, RPMH_WAKE_ONLY_STATE, cmds, commit_idx);
  346. if (ret) {
  347. pr_err("Error sending WAKE RPMH requests (%d)\n", ret);
  348. goto out;
  349. }
  350. tcs_list_gen(voter, QCOM_ICC_BUCKET_SLEEP, cmds, commit_idx);
  351. qcom_icc_bcm_log(voter, RPMH_SLEEP_STATE, cmds, commit_idx);
  352. ret = rpmh_write_batch(voter->dev, RPMH_SLEEP_STATE, cmds, commit_idx);
  353. if (ret) {
  354. pr_err("Error sending SLEEP RPMH requests (%d)\n", ret);
  355. goto out;
  356. }
  357. out:
  358. return ret;
  359. }
  360. static int map_crm_pwr_state(enum crm_drv_type client_type, u32 bucket)
  361. {
  362. if (client_type == CRM_HW_DRV)
  363. return bucket;
  364. switch (bucket) {
  365. case QCOM_ICC_BUCKET_AMC: return CRM_ACTIVE_STATE;
  366. case QCOM_ICC_BUCKET_WAKE: return CRM_WAKE_STATE;
  367. case QCOM_ICC_BUCKET_SLEEP: return CRM_SLEEP_STATE;
  368. }
  369. return -EINVAL;
  370. }
  371. static int crm_cmd_gen(struct crm_cmd *cmd, enum crm_drv_type client_type,
  372. u32 bucket, u32 node, u64 vote_x, u64 vote_y)
  373. {
  374. int pwr_state;
  375. if (!cmd)
  376. return -EINVAL;
  377. memset(cmd, 0, sizeof(*cmd));
  378. if (vote_x > BCM_TCS_CMD_VOTE_MASK)
  379. vote_x = BCM_TCS_CMD_VOTE_MASK;
  380. if (vote_y > BCM_TCS_CMD_VOTE_MASK)
  381. vote_y = BCM_TCS_CMD_VOTE_MASK;
  382. pwr_state = map_crm_pwr_state(client_type, bucket);
  383. if (pwr_state < 0)
  384. return pwr_state;
  385. cmd->pwr_state.hw = pwr_state;
  386. cmd->resource_idx = node;
  387. cmd->data = BCM_TCS_CMD(true, true, vote_x, vote_y);
  388. cmd->wait = true;
  389. return 0;
  390. }
  391. static int commit_crm(struct bcm_voter *voter)
  392. {
  393. struct list_head *bcm_list = &voter->commit_list;
  394. struct qcom_icc_crm_voter *crm = voter->crm;
  395. struct qcom_icc_bcm *bcm;
  396. struct crm_cmd crm_cmd;
  397. int ret, i;
  398. list_for_each_entry(bcm, bcm_list, list) {
  399. for (i = 0; i < crm->pwr_states; i++) {
  400. ret = crm_cmd_gen(&crm_cmd, crm->client_type, i, bcm->crm_node,
  401. bcm->vote_x[i], bcm->vote_y[i]);
  402. if (ret) {
  403. pr_err("Error generating crm_cmd: ret=%d\n", ret);
  404. return ret;
  405. }
  406. ret = crm_write_bw_vote(crm->dev, crm->client_type,
  407. crm->client_idx, &crm_cmd);
  408. if (ret) {
  409. pr_err("Error writing crm bw: ret=%d\n", ret);
  410. return ret;
  411. }
  412. }
  413. }
  414. return 0;
  415. }
  416. /**
  417. * qcom_icc_bcm_voter_commit - generates and commits tcs cmds based on bcms
  418. * @voter: voter that needs flushing
  419. *
  420. * This function generates a set of AMC commands and flushes to the BCM device
  421. * associated with the voter. It conditionally generate WAKE and SLEEP commands
  422. * based on deltas between WAKE/SLEEP requirements. The ws_list persists
  423. * through multiple commit requests and bcm nodes are removed only when the
  424. * requirements for WAKE matches SLEEP.
  425. *
  426. * Returns 0 on success, or an appropriate error code otherwise.
  427. */
  428. int qcom_icc_bcm_voter_commit(struct bcm_voter *voter)
  429. {
  430. struct qcom_icc_bcm *bcm;
  431. struct qcom_icc_bcm *bcm_tmp;
  432. int ret;
  433. if (!voter)
  434. return 0;
  435. if (!qcom_icc_bcm_voter_exist(voter))
  436. return -ENODEV;
  437. mutex_lock(&voter->lock);
  438. list_for_each_entry(bcm, &voter->commit_list, list) {
  439. if (bcm->enable_mask)
  440. bcm_aggregate_mask(bcm);
  441. else
  442. bcm_aggregate(bcm, voter->init);
  443. }
  444. if (voter->crm)
  445. ret = commit_crm(voter);
  446. else
  447. ret = commit_rpmh(voter);
  448. list_for_each_entry_safe(bcm, bcm_tmp, &voter->commit_list, list)
  449. list_del_init(&bcm->list);
  450. mutex_unlock(&voter->lock);
  451. return ret;
  452. }
  453. EXPORT_SYMBOL_GPL(qcom_icc_bcm_voter_commit);
  454. /**
  455. * qcom_icc_bcm_voter_clear_init - clear init flag used during boot up
  456. * @voter: voter that we need to clear the init flag for
  457. */
  458. void qcom_icc_bcm_voter_clear_init(struct bcm_voter *voter)
  459. {
  460. if (!voter)
  461. return;
  462. if (!qcom_icc_bcm_voter_exist(voter))
  463. return;
  464. mutex_lock(&voter->lock);
  465. voter->init = false;
  466. mutex_unlock(&voter->lock);
  467. }
  468. EXPORT_SYMBOL(qcom_icc_bcm_voter_clear_init);
  469. static int qcom_icc_bcm_voter_probe(struct platform_device *pdev)
  470. {
  471. struct device_node *np = pdev->dev.of_node;
  472. struct qcom_icc_crm_voter *crm;
  473. struct bcm_voter *voter;
  474. const char *crm_name;
  475. int ret;
  476. voter = devm_kzalloc(&pdev->dev, sizeof(*voter), GFP_KERNEL);
  477. if (!voter)
  478. return -ENOMEM;
  479. voter->dev = &pdev->dev;
  480. voter->np = np;
  481. voter->init = true;
  482. voter->has_amc = !of_property_read_bool(np, "qcom,no-amc");
  483. if (of_property_read_u32(np, "qcom,tcs-wait", &voter->tcs_wait))
  484. voter->tcs_wait = QCOM_ICC_TAG_ACTIVE_ONLY;
  485. mutex_init(&voter->lock);
  486. INIT_LIST_HEAD(&voter->commit_list);
  487. INIT_LIST_HEAD(&voter->ws_list);
  488. ret = of_property_read_string(np, "qcom,crm-name", &crm_name);
  489. if (!ret) {
  490. crm = devm_kzalloc(&pdev->dev, sizeof(*crm), GFP_KERNEL);
  491. if (!crm)
  492. return -ENOMEM;
  493. crm->dev = crm_get_device(crm_name);
  494. if (IS_ERR(crm->dev)) {
  495. if (PTR_ERR(crm->dev) == -ENODEV) {
  496. dev_err(&pdev->dev, "crm_name=%s unavailable\n", crm_name);
  497. return -EPROBE_DEFER;
  498. }
  499. return PTR_ERR(crm->dev);
  500. }
  501. crm->client_type = CRM_HW_DRV;
  502. ret = of_property_read_u32(np, "qcom,crm-client-idx", &crm->client_idx);
  503. if (ret) {
  504. dev_err(&pdev->dev, "Error getting crm-client-idx, ret=%d\n", ret);
  505. return ret;
  506. }
  507. ret = of_property_read_u32(np, "qcom,crm-pwr-states", &crm->pwr_states);
  508. if (ret) {
  509. dev_err(&pdev->dev, "Error getting crm-pwr-states, ret=%d\n", ret);
  510. return ret;
  511. }
  512. voter->crm = crm;
  513. }
  514. mutex_lock(&bcm_voter_lock);
  515. list_add_tail(&voter->voter_node, &bcm_voters);
  516. mutex_unlock(&bcm_voter_lock);
  517. return 0;
  518. }
  519. static int qcom_icc_bcm_voter_remove(struct platform_device *pdev)
  520. {
  521. struct device_node *np = pdev->dev.of_node;
  522. struct bcm_voter *voter, *temp;
  523. mutex_lock(&bcm_voter_lock);
  524. list_for_each_entry_safe(voter, temp, &bcm_voters, voter_node) {
  525. if (voter->np == np) {
  526. list_del(&voter->voter_node);
  527. break;
  528. }
  529. }
  530. mutex_unlock(&bcm_voter_lock);
  531. return 0;
  532. }
  533. static const struct of_device_id bcm_voter_of_match[] = {
  534. { .compatible = "qcom,bcm-voter" },
  535. { }
  536. };
  537. MODULE_DEVICE_TABLE(of, bcm_voter_of_match);
  538. static struct platform_driver qcom_icc_bcm_voter_driver = {
  539. .probe = qcom_icc_bcm_voter_probe,
  540. .remove = qcom_icc_bcm_voter_remove,
  541. .driver = {
  542. .name = "bcm_voter",
  543. .of_match_table = bcm_voter_of_match,
  544. },
  545. };
  546. static int __init qcom_icc_bcm_voter_driver_init(void)
  547. {
  548. return platform_driver_register(&qcom_icc_bcm_voter_driver);
  549. }
  550. module_init(qcom_icc_bcm_voter_driver_init);
  551. MODULE_AUTHOR("David Dai <[email protected]>");
  552. MODULE_DESCRIPTION("Qualcomm BCM Voter interconnect driver");
  553. MODULE_LICENSE("GPL v2");