arm_big_little.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. /*
  2. * ARM big.LITTLE Platforms CPUFreq support
  3. *
  4. * Copyright (C) 2013 ARM Ltd.
  5. * Sudeep KarkadaNagesha <sudeep.karkadanagesha@arm.com>
  6. *
  7. * Copyright (C) 2013 Linaro.
  8. * Viresh Kumar <viresh.kumar@linaro.org>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. *
  14. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  15. * kind, whether express or implied; without even the implied warranty
  16. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. */
  19. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  20. #include <linux/clk.h>
  21. #include <linux/cpu.h>
  22. #include <linux/cpufreq.h>
  23. #include <linux/cpumask.h>
  24. #include <linux/cpu_cooling.h>
  25. #include <linux/export.h>
  26. #include <linux/module.h>
  27. #include <linux/mutex.h>
  28. #include <linux/of_platform.h>
  29. #include <linux/pm_opp.h>
  30. #include <linux/slab.h>
  31. #include <linux/topology.h>
  32. #include <linux/types.h>
  33. #include "arm_big_little.h"
  34. /* Currently we support only two clusters */
  35. #define A15_CLUSTER 0
  36. #define A7_CLUSTER 1
  37. #define MAX_CLUSTERS 2
  38. #ifdef CONFIG_BL_SWITCHER
  39. #include <asm/bL_switcher.h>
  40. static bool bL_switching_enabled;
  41. #define is_bL_switching_enabled() bL_switching_enabled
  42. #define set_switching_enabled(x) (bL_switching_enabled = (x))
  43. #else
  44. #define is_bL_switching_enabled() false
  45. #define set_switching_enabled(x) do { } while (0)
  46. #define bL_switch_request(...) do { } while (0)
  47. #define bL_switcher_put_enabled() do { } while (0)
  48. #define bL_switcher_get_enabled() do { } while (0)
  49. #endif
  50. #define ACTUAL_FREQ(cluster, freq) ((cluster == A7_CLUSTER) ? freq << 1 : freq)
  51. #define VIRT_FREQ(cluster, freq) ((cluster == A7_CLUSTER) ? freq >> 1 : freq)
  52. static struct thermal_cooling_device *cdev[MAX_CLUSTERS];
  53. static struct cpufreq_arm_bL_ops *arm_bL_ops;
  54. static struct clk *clk[MAX_CLUSTERS];
  55. static struct cpufreq_frequency_table *freq_table[MAX_CLUSTERS + 1];
  56. static atomic_t cluster_usage[MAX_CLUSTERS + 1];
  57. static unsigned int clk_big_min; /* (Big) clock frequencies */
  58. static unsigned int clk_little_max; /* Maximum clock frequency (Little) */
  59. static DEFINE_PER_CPU(unsigned int, physical_cluster);
  60. static DEFINE_PER_CPU(unsigned int, cpu_last_req_freq);
  61. static struct mutex cluster_lock[MAX_CLUSTERS];
  62. static inline int raw_cpu_to_cluster(int cpu)
  63. {
  64. return topology_physical_package_id(cpu);
  65. }
  66. static inline int cpu_to_cluster(int cpu)
  67. {
  68. return is_bL_switching_enabled() ?
  69. MAX_CLUSTERS : raw_cpu_to_cluster(cpu);
  70. }
  71. static unsigned int find_cluster_maxfreq(int cluster)
  72. {
  73. int j;
  74. u32 max_freq = 0, cpu_freq;
  75. for_each_online_cpu(j) {
  76. cpu_freq = per_cpu(cpu_last_req_freq, j);
  77. if ((cluster == per_cpu(physical_cluster, j)) &&
  78. (max_freq < cpu_freq))
  79. max_freq = cpu_freq;
  80. }
  81. pr_debug("%s: cluster: %d, max freq: %d\n", __func__, cluster,
  82. max_freq);
  83. return max_freq;
  84. }
  85. static unsigned int clk_get_cpu_rate(unsigned int cpu)
  86. {
  87. u32 cur_cluster = per_cpu(physical_cluster, cpu);
  88. u32 rate = clk_get_rate(clk[cur_cluster]) / 1000;
  89. /* For switcher we use virtual A7 clock rates */
  90. if (is_bL_switching_enabled())
  91. rate = VIRT_FREQ(cur_cluster, rate);
  92. pr_debug("%s: cpu: %d, cluster: %d, freq: %u\n", __func__, cpu,
  93. cur_cluster, rate);
  94. return rate;
  95. }
  96. static unsigned int bL_cpufreq_get_rate(unsigned int cpu)
  97. {
  98. if (is_bL_switching_enabled()) {
  99. pr_debug("%s: freq: %d\n", __func__, per_cpu(cpu_last_req_freq,
  100. cpu));
  101. return per_cpu(cpu_last_req_freq, cpu);
  102. } else {
  103. return clk_get_cpu_rate(cpu);
  104. }
  105. }
  106. static unsigned int
  107. bL_cpufreq_set_rate(u32 cpu, u32 old_cluster, u32 new_cluster, u32 rate)
  108. {
  109. u32 new_rate, prev_rate;
  110. int ret;
  111. bool bLs = is_bL_switching_enabled();
  112. mutex_lock(&cluster_lock[new_cluster]);
  113. if (bLs) {
  114. prev_rate = per_cpu(cpu_last_req_freq, cpu);
  115. per_cpu(cpu_last_req_freq, cpu) = rate;
  116. per_cpu(physical_cluster, cpu) = new_cluster;
  117. new_rate = find_cluster_maxfreq(new_cluster);
  118. new_rate = ACTUAL_FREQ(new_cluster, new_rate);
  119. } else {
  120. new_rate = rate;
  121. }
  122. pr_debug("%s: cpu: %d, old cluster: %d, new cluster: %d, freq: %d\n",
  123. __func__, cpu, old_cluster, new_cluster, new_rate);
  124. ret = clk_set_rate(clk[new_cluster], new_rate * 1000);
  125. if (!ret) {
  126. /*
  127. * FIXME: clk_set_rate hasn't returned an error here however it
  128. * may be that clk_change_rate failed due to hardware or
  129. * firmware issues and wasn't able to report that due to the
  130. * current design of the clk core layer. To work around this
  131. * problem we will read back the clock rate and check it is
  132. * correct. This needs to be removed once clk core is fixed.
  133. */
  134. if (clk_get_rate(clk[new_cluster]) != new_rate * 1000)
  135. ret = -EIO;
  136. }
  137. if (WARN_ON(ret)) {
  138. pr_err("clk_set_rate failed: %d, new cluster: %d\n", ret,
  139. new_cluster);
  140. if (bLs) {
  141. per_cpu(cpu_last_req_freq, cpu) = prev_rate;
  142. per_cpu(physical_cluster, cpu) = old_cluster;
  143. }
  144. mutex_unlock(&cluster_lock[new_cluster]);
  145. return ret;
  146. }
  147. mutex_unlock(&cluster_lock[new_cluster]);
  148. /* Recalc freq for old cluster when switching clusters */
  149. if (old_cluster != new_cluster) {
  150. pr_debug("%s: cpu: %d, old cluster: %d, new cluster: %d\n",
  151. __func__, cpu, old_cluster, new_cluster);
  152. /* Switch cluster */
  153. bL_switch_request(cpu, new_cluster);
  154. mutex_lock(&cluster_lock[old_cluster]);
  155. /* Set freq of old cluster if there are cpus left on it */
  156. new_rate = find_cluster_maxfreq(old_cluster);
  157. new_rate = ACTUAL_FREQ(old_cluster, new_rate);
  158. if (new_rate) {
  159. pr_debug("%s: Updating rate of old cluster: %d, to freq: %d\n",
  160. __func__, old_cluster, new_rate);
  161. if (clk_set_rate(clk[old_cluster], new_rate * 1000))
  162. pr_err("%s: clk_set_rate failed: %d, old cluster: %d\n",
  163. __func__, ret, old_cluster);
  164. }
  165. mutex_unlock(&cluster_lock[old_cluster]);
  166. }
  167. return 0;
  168. }
  169. /* Set clock frequency */
  170. static int bL_cpufreq_set_target(struct cpufreq_policy *policy,
  171. unsigned int index)
  172. {
  173. u32 cpu = policy->cpu, cur_cluster, new_cluster, actual_cluster;
  174. unsigned int freqs_new;
  175. cur_cluster = cpu_to_cluster(cpu);
  176. new_cluster = actual_cluster = per_cpu(physical_cluster, cpu);
  177. freqs_new = freq_table[cur_cluster][index].frequency;
  178. if (is_bL_switching_enabled()) {
  179. if ((actual_cluster == A15_CLUSTER) &&
  180. (freqs_new < clk_big_min)) {
  181. new_cluster = A7_CLUSTER;
  182. } else if ((actual_cluster == A7_CLUSTER) &&
  183. (freqs_new > clk_little_max)) {
  184. new_cluster = A15_CLUSTER;
  185. }
  186. }
  187. return bL_cpufreq_set_rate(cpu, actual_cluster, new_cluster, freqs_new);
  188. }
  189. static inline u32 get_table_count(struct cpufreq_frequency_table *table)
  190. {
  191. int count;
  192. for (count = 0; table[count].frequency != CPUFREQ_TABLE_END; count++)
  193. ;
  194. return count;
  195. }
  196. /* get the minimum frequency in the cpufreq_frequency_table */
  197. static inline u32 get_table_min(struct cpufreq_frequency_table *table)
  198. {
  199. struct cpufreq_frequency_table *pos;
  200. uint32_t min_freq = ~0;
  201. cpufreq_for_each_entry(pos, table)
  202. if (pos->frequency < min_freq)
  203. min_freq = pos->frequency;
  204. return min_freq;
  205. }
  206. /* get the maximum frequency in the cpufreq_frequency_table */
  207. static inline u32 get_table_max(struct cpufreq_frequency_table *table)
  208. {
  209. struct cpufreq_frequency_table *pos;
  210. uint32_t max_freq = 0;
  211. cpufreq_for_each_entry(pos, table)
  212. if (pos->frequency > max_freq)
  213. max_freq = pos->frequency;
  214. return max_freq;
  215. }
  216. static int merge_cluster_tables(void)
  217. {
  218. int i, j, k = 0, count = 1;
  219. struct cpufreq_frequency_table *table;
  220. for (i = 0; i < MAX_CLUSTERS; i++)
  221. count += get_table_count(freq_table[i]);
  222. table = kzalloc(sizeof(*table) * count, GFP_KERNEL);
  223. if (!table)
  224. return -ENOMEM;
  225. freq_table[MAX_CLUSTERS] = table;
  226. /* Add in reverse order to get freqs in increasing order */
  227. for (i = MAX_CLUSTERS - 1; i >= 0; i--) {
  228. for (j = 0; freq_table[i][j].frequency != CPUFREQ_TABLE_END;
  229. j++) {
  230. table[k].frequency = VIRT_FREQ(i,
  231. freq_table[i][j].frequency);
  232. pr_debug("%s: index: %d, freq: %d\n", __func__, k,
  233. table[k].frequency);
  234. k++;
  235. }
  236. }
  237. table[k].driver_data = k;
  238. table[k].frequency = CPUFREQ_TABLE_END;
  239. pr_debug("%s: End, table: %p, count: %d\n", __func__, table, k);
  240. return 0;
  241. }
  242. static void _put_cluster_clk_and_freq_table(struct device *cpu_dev,
  243. const struct cpumask *cpumask)
  244. {
  245. u32 cluster = raw_cpu_to_cluster(cpu_dev->id);
  246. if (!freq_table[cluster])
  247. return;
  248. clk_put(clk[cluster]);
  249. dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table[cluster]);
  250. if (arm_bL_ops->free_opp_table)
  251. arm_bL_ops->free_opp_table(cpumask);
  252. dev_dbg(cpu_dev, "%s: cluster: %d\n", __func__, cluster);
  253. }
  254. static void put_cluster_clk_and_freq_table(struct device *cpu_dev,
  255. const struct cpumask *cpumask)
  256. {
  257. u32 cluster = cpu_to_cluster(cpu_dev->id);
  258. int i;
  259. if (atomic_dec_return(&cluster_usage[cluster]))
  260. return;
  261. if (cluster < MAX_CLUSTERS)
  262. return _put_cluster_clk_and_freq_table(cpu_dev, cpumask);
  263. for_each_present_cpu(i) {
  264. struct device *cdev = get_cpu_device(i);
  265. if (!cdev) {
  266. pr_err("%s: failed to get cpu%d device\n", __func__, i);
  267. return;
  268. }
  269. _put_cluster_clk_and_freq_table(cdev, cpumask);
  270. }
  271. /* free virtual table */
  272. kfree(freq_table[cluster]);
  273. }
  274. static int _get_cluster_clk_and_freq_table(struct device *cpu_dev,
  275. const struct cpumask *cpumask)
  276. {
  277. u32 cluster = raw_cpu_to_cluster(cpu_dev->id);
  278. int ret;
  279. if (freq_table[cluster])
  280. return 0;
  281. ret = arm_bL_ops->init_opp_table(cpumask);
  282. if (ret) {
  283. dev_err(cpu_dev, "%s: init_opp_table failed, cpu: %d, err: %d\n",
  284. __func__, cpu_dev->id, ret);
  285. goto out;
  286. }
  287. ret = dev_pm_opp_init_cpufreq_table(cpu_dev, &freq_table[cluster]);
  288. if (ret) {
  289. dev_err(cpu_dev, "%s: failed to init cpufreq table, cpu: %d, err: %d\n",
  290. __func__, cpu_dev->id, ret);
  291. goto free_opp_table;
  292. }
  293. clk[cluster] = clk_get(cpu_dev, NULL);
  294. if (!IS_ERR(clk[cluster])) {
  295. dev_dbg(cpu_dev, "%s: clk: %p & freq table: %p, cluster: %d\n",
  296. __func__, clk[cluster], freq_table[cluster],
  297. cluster);
  298. return 0;
  299. }
  300. dev_err(cpu_dev, "%s: Failed to get clk for cpu: %d, cluster: %d\n",
  301. __func__, cpu_dev->id, cluster);
  302. ret = PTR_ERR(clk[cluster]);
  303. dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table[cluster]);
  304. free_opp_table:
  305. if (arm_bL_ops->free_opp_table)
  306. arm_bL_ops->free_opp_table(cpumask);
  307. out:
  308. dev_err(cpu_dev, "%s: Failed to get data for cluster: %d\n", __func__,
  309. cluster);
  310. return ret;
  311. }
  312. static int get_cluster_clk_and_freq_table(struct device *cpu_dev,
  313. const struct cpumask *cpumask)
  314. {
  315. u32 cluster = cpu_to_cluster(cpu_dev->id);
  316. int i, ret;
  317. if (atomic_inc_return(&cluster_usage[cluster]) != 1)
  318. return 0;
  319. if (cluster < MAX_CLUSTERS) {
  320. ret = _get_cluster_clk_and_freq_table(cpu_dev, cpumask);
  321. if (ret)
  322. atomic_dec(&cluster_usage[cluster]);
  323. return ret;
  324. }
  325. /*
  326. * Get data for all clusters and fill virtual cluster with a merge of
  327. * both
  328. */
  329. for_each_present_cpu(i) {
  330. struct device *cdev = get_cpu_device(i);
  331. if (!cdev) {
  332. pr_err("%s: failed to get cpu%d device\n", __func__, i);
  333. return -ENODEV;
  334. }
  335. ret = _get_cluster_clk_and_freq_table(cdev, cpumask);
  336. if (ret)
  337. goto put_clusters;
  338. }
  339. ret = merge_cluster_tables();
  340. if (ret)
  341. goto put_clusters;
  342. /* Assuming 2 cluster, set clk_big_min and clk_little_max */
  343. clk_big_min = get_table_min(freq_table[0]);
  344. clk_little_max = VIRT_FREQ(1, get_table_max(freq_table[1]));
  345. pr_debug("%s: cluster: %d, clk_big_min: %d, clk_little_max: %d\n",
  346. __func__, cluster, clk_big_min, clk_little_max);
  347. return 0;
  348. put_clusters:
  349. for_each_present_cpu(i) {
  350. struct device *cdev = get_cpu_device(i);
  351. if (!cdev) {
  352. pr_err("%s: failed to get cpu%d device\n", __func__, i);
  353. return -ENODEV;
  354. }
  355. _put_cluster_clk_and_freq_table(cdev, cpumask);
  356. }
  357. atomic_dec(&cluster_usage[cluster]);
  358. return ret;
  359. }
  360. /* Per-CPU initialization */
  361. static int bL_cpufreq_init(struct cpufreq_policy *policy)
  362. {
  363. u32 cur_cluster = cpu_to_cluster(policy->cpu);
  364. struct device *cpu_dev;
  365. int ret;
  366. cpu_dev = get_cpu_device(policy->cpu);
  367. if (!cpu_dev) {
  368. pr_err("%s: failed to get cpu%d device\n", __func__,
  369. policy->cpu);
  370. return -ENODEV;
  371. }
  372. if (cur_cluster < MAX_CLUSTERS) {
  373. int cpu;
  374. cpumask_copy(policy->cpus, topology_core_cpumask(policy->cpu));
  375. for_each_cpu(cpu, policy->cpus)
  376. per_cpu(physical_cluster, cpu) = cur_cluster;
  377. } else {
  378. /* Assumption: during init, we are always running on A15 */
  379. per_cpu(physical_cluster, policy->cpu) = A15_CLUSTER;
  380. }
  381. ret = get_cluster_clk_and_freq_table(cpu_dev, policy->cpus);
  382. if (ret)
  383. return ret;
  384. ret = cpufreq_table_validate_and_show(policy, freq_table[cur_cluster]);
  385. if (ret) {
  386. dev_err(cpu_dev, "CPU %d, cluster: %d invalid freq table\n",
  387. policy->cpu, cur_cluster);
  388. put_cluster_clk_and_freq_table(cpu_dev, policy->cpus);
  389. return ret;
  390. }
  391. if (arm_bL_ops->get_transition_latency)
  392. policy->cpuinfo.transition_latency =
  393. arm_bL_ops->get_transition_latency(cpu_dev);
  394. else
  395. policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
  396. if (is_bL_switching_enabled())
  397. per_cpu(cpu_last_req_freq, policy->cpu) = clk_get_cpu_rate(policy->cpu);
  398. dev_info(cpu_dev, "%s: CPU %d initialized\n", __func__, policy->cpu);
  399. return 0;
  400. }
  401. static int bL_cpufreq_exit(struct cpufreq_policy *policy)
  402. {
  403. struct device *cpu_dev;
  404. int cur_cluster = cpu_to_cluster(policy->cpu);
  405. if (cur_cluster < MAX_CLUSTERS) {
  406. cpufreq_cooling_unregister(cdev[cur_cluster]);
  407. cdev[cur_cluster] = NULL;
  408. }
  409. cpu_dev = get_cpu_device(policy->cpu);
  410. if (!cpu_dev) {
  411. pr_err("%s: failed to get cpu%d device\n", __func__,
  412. policy->cpu);
  413. return -ENODEV;
  414. }
  415. put_cluster_clk_and_freq_table(cpu_dev, policy->related_cpus);
  416. dev_dbg(cpu_dev, "%s: Exited, cpu: %d\n", __func__, policy->cpu);
  417. return 0;
  418. }
  419. static void bL_cpufreq_ready(struct cpufreq_policy *policy)
  420. {
  421. struct device *cpu_dev = get_cpu_device(policy->cpu);
  422. int cur_cluster = cpu_to_cluster(policy->cpu);
  423. struct device_node *np;
  424. /* Do not register a cpu_cooling device if we are in IKS mode */
  425. if (cur_cluster >= MAX_CLUSTERS)
  426. return;
  427. np = of_node_get(cpu_dev->of_node);
  428. if (WARN_ON(!np))
  429. return;
  430. if (of_find_property(np, "#cooling-cells", NULL)) {
  431. u32 power_coefficient = 0;
  432. of_property_read_u32(np, "dynamic-power-coefficient",
  433. &power_coefficient);
  434. cdev[cur_cluster] = of_cpufreq_power_cooling_register(np,
  435. policy->related_cpus, power_coefficient, NULL);
  436. if (IS_ERR(cdev[cur_cluster])) {
  437. dev_err(cpu_dev,
  438. "running cpufreq without cooling device: %ld\n",
  439. PTR_ERR(cdev[cur_cluster]));
  440. cdev[cur_cluster] = NULL;
  441. }
  442. }
  443. of_node_put(np);
  444. }
  445. static struct cpufreq_driver bL_cpufreq_driver = {
  446. .name = "arm-big-little",
  447. .flags = CPUFREQ_STICKY |
  448. CPUFREQ_HAVE_GOVERNOR_PER_POLICY |
  449. CPUFREQ_NEED_INITIAL_FREQ_CHECK,
  450. .verify = cpufreq_generic_frequency_table_verify,
  451. .target_index = bL_cpufreq_set_target,
  452. .get = bL_cpufreq_get_rate,
  453. .init = bL_cpufreq_init,
  454. .exit = bL_cpufreq_exit,
  455. .ready = bL_cpufreq_ready,
  456. .attr = cpufreq_generic_attr,
  457. };
  458. #ifdef CONFIG_BL_SWITCHER
  459. static int bL_cpufreq_switcher_notifier(struct notifier_block *nfb,
  460. unsigned long action, void *_arg)
  461. {
  462. pr_debug("%s: action: %ld\n", __func__, action);
  463. switch (action) {
  464. case BL_NOTIFY_PRE_ENABLE:
  465. case BL_NOTIFY_PRE_DISABLE:
  466. cpufreq_unregister_driver(&bL_cpufreq_driver);
  467. break;
  468. case BL_NOTIFY_POST_ENABLE:
  469. set_switching_enabled(true);
  470. cpufreq_register_driver(&bL_cpufreq_driver);
  471. break;
  472. case BL_NOTIFY_POST_DISABLE:
  473. set_switching_enabled(false);
  474. cpufreq_register_driver(&bL_cpufreq_driver);
  475. break;
  476. default:
  477. return NOTIFY_DONE;
  478. }
  479. return NOTIFY_OK;
  480. }
  481. static struct notifier_block bL_switcher_notifier = {
  482. .notifier_call = bL_cpufreq_switcher_notifier,
  483. };
  484. static int __bLs_register_notifier(void)
  485. {
  486. return bL_switcher_register_notifier(&bL_switcher_notifier);
  487. }
  488. static int __bLs_unregister_notifier(void)
  489. {
  490. return bL_switcher_unregister_notifier(&bL_switcher_notifier);
  491. }
  492. #else
  493. static int __bLs_register_notifier(void) { return 0; }
  494. static int __bLs_unregister_notifier(void) { return 0; }
  495. #endif
  496. int bL_cpufreq_register(struct cpufreq_arm_bL_ops *ops)
  497. {
  498. int ret, i;
  499. if (arm_bL_ops) {
  500. pr_debug("%s: Already registered: %s, exiting\n", __func__,
  501. arm_bL_ops->name);
  502. return -EBUSY;
  503. }
  504. if (!ops || !strlen(ops->name) || !ops->init_opp_table) {
  505. pr_err("%s: Invalid arm_bL_ops, exiting\n", __func__);
  506. return -ENODEV;
  507. }
  508. arm_bL_ops = ops;
  509. set_switching_enabled(bL_switcher_get_enabled());
  510. for (i = 0; i < MAX_CLUSTERS; i++)
  511. mutex_init(&cluster_lock[i]);
  512. ret = cpufreq_register_driver(&bL_cpufreq_driver);
  513. if (ret) {
  514. pr_info("%s: Failed registering platform driver: %s, err: %d\n",
  515. __func__, ops->name, ret);
  516. arm_bL_ops = NULL;
  517. } else {
  518. ret = __bLs_register_notifier();
  519. if (ret) {
  520. cpufreq_unregister_driver(&bL_cpufreq_driver);
  521. arm_bL_ops = NULL;
  522. } else {
  523. pr_info("%s: Registered platform driver: %s\n",
  524. __func__, ops->name);
  525. }
  526. }
  527. bL_switcher_put_enabled();
  528. return ret;
  529. }
  530. EXPORT_SYMBOL_GPL(bL_cpufreq_register);
  531. void bL_cpufreq_unregister(struct cpufreq_arm_bL_ops *ops)
  532. {
  533. if (arm_bL_ops != ops) {
  534. pr_err("%s: Registered with: %s, can't unregister, exiting\n",
  535. __func__, arm_bL_ops->name);
  536. return;
  537. }
  538. bL_switcher_get_enabled();
  539. __bLs_unregister_notifier();
  540. cpufreq_unregister_driver(&bL_cpufreq_driver);
  541. bL_switcher_put_enabled();
  542. pr_info("%s: Un-registered platform driver: %s\n", __func__,
  543. arm_bL_ops->name);
  544. arm_bL_ops = NULL;
  545. }
  546. EXPORT_SYMBOL_GPL(bL_cpufreq_unregister);
  547. MODULE_AUTHOR("Viresh Kumar <viresh.kumar@linaro.org>");
  548. MODULE_DESCRIPTION("Generic ARM big LITTLE cpufreq driver");
  549. MODULE_LICENSE("GPL v2");