tegra-devfreq.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. /*
  2. * A devfreq driver for NVIDIA Tegra SoCs
  3. *
  4. * Copyright (c) 2014 NVIDIA CORPORATION. All rights reserved.
  5. * Copyright (C) 2014 Google, Inc
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms and conditions of the GNU General Public License,
  9. * version 2, as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. #include <linux/clk.h>
  21. #include <linux/cpufreq.h>
  22. #include <linux/devfreq.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/io.h>
  25. #include <linux/module.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/pm_opp.h>
  28. #include <linux/reset.h>
  29. #include "governor.h"
  30. #define ACTMON_GLB_STATUS 0x0
  31. #define ACTMON_GLB_PERIOD_CTRL 0x4
  32. #define ACTMON_DEV_CTRL 0x0
  33. #define ACTMON_DEV_CTRL_K_VAL_SHIFT 10
  34. #define ACTMON_DEV_CTRL_ENB_PERIODIC BIT(18)
  35. #define ACTMON_DEV_CTRL_AVG_BELOW_WMARK_EN BIT(20)
  36. #define ACTMON_DEV_CTRL_AVG_ABOVE_WMARK_EN BIT(21)
  37. #define ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_NUM_SHIFT 23
  38. #define ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_NUM_SHIFT 26
  39. #define ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_EN BIT(29)
  40. #define ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_EN BIT(30)
  41. #define ACTMON_DEV_CTRL_ENB BIT(31)
  42. #define ACTMON_DEV_UPPER_WMARK 0x4
  43. #define ACTMON_DEV_LOWER_WMARK 0x8
  44. #define ACTMON_DEV_INIT_AVG 0xc
  45. #define ACTMON_DEV_AVG_UPPER_WMARK 0x10
  46. #define ACTMON_DEV_AVG_LOWER_WMARK 0x14
  47. #define ACTMON_DEV_COUNT_WEIGHT 0x18
  48. #define ACTMON_DEV_AVG_COUNT 0x20
  49. #define ACTMON_DEV_INTR_STATUS 0x24
  50. #define ACTMON_INTR_STATUS_CLEAR 0xffffffff
  51. #define ACTMON_DEV_INTR_CONSECUTIVE_UPPER BIT(31)
  52. #define ACTMON_DEV_INTR_CONSECUTIVE_LOWER BIT(30)
  53. #define ACTMON_ABOVE_WMARK_WINDOW 1
  54. #define ACTMON_BELOW_WMARK_WINDOW 3
  55. #define ACTMON_BOOST_FREQ_STEP 16000
  56. /* activity counter is incremented every 256 memory transactions, and each
  57. * transaction takes 4 EMC clocks for Tegra124; So the COUNT_WEIGHT is
  58. * 4 * 256 = 1024.
  59. */
  60. #define ACTMON_COUNT_WEIGHT 0x400
  61. /*
  62. * ACTMON_AVERAGE_WINDOW_LOG2: default value for @DEV_CTRL_K_VAL, which
  63. * translates to 2 ^ (K_VAL + 1). ex: 2 ^ (6 + 1) = 128
  64. */
  65. #define ACTMON_AVERAGE_WINDOW_LOG2 6
  66. #define ACTMON_SAMPLING_PERIOD 12 /* ms */
  67. #define ACTMON_DEFAULT_AVG_BAND 6 /* 1/10 of % */
  68. #define KHZ 1000
  69. /* Assume that the bus is saturated if the utilization is 25% */
  70. #define BUS_SATURATION_RATIO 25
  71. /**
  72. * struct tegra_devfreq_device_config - configuration specific to an ACTMON
  73. * device
  74. *
  75. * Coefficients and thresholds are in %
  76. */
  77. struct tegra_devfreq_device_config {
  78. u32 offset;
  79. u32 irq_mask;
  80. unsigned int boost_up_coeff;
  81. unsigned int boost_down_coeff;
  82. unsigned int boost_up_threshold;
  83. unsigned int boost_down_threshold;
  84. u32 avg_dependency_threshold;
  85. };
  86. enum tegra_actmon_device {
  87. MCALL = 0,
  88. MCCPU,
  89. };
  90. static struct tegra_devfreq_device_config actmon_device_configs[] = {
  91. {
  92. /* MCALL */
  93. .offset = 0x1c0,
  94. .irq_mask = 1 << 26,
  95. .boost_up_coeff = 200,
  96. .boost_down_coeff = 50,
  97. .boost_up_threshold = 60,
  98. .boost_down_threshold = 40,
  99. },
  100. {
  101. /* MCCPU */
  102. .offset = 0x200,
  103. .irq_mask = 1 << 25,
  104. .boost_up_coeff = 800,
  105. .boost_down_coeff = 90,
  106. .boost_up_threshold = 27,
  107. .boost_down_threshold = 10,
  108. .avg_dependency_threshold = 50000,
  109. },
  110. };
  111. /**
  112. * struct tegra_devfreq_device - state specific to an ACTMON device
  113. *
  114. * Frequencies are in kHz.
  115. */
  116. struct tegra_devfreq_device {
  117. const struct tegra_devfreq_device_config *config;
  118. void __iomem *regs;
  119. u32 avg_band_freq;
  120. u32 avg_count;
  121. unsigned long target_freq;
  122. unsigned long boost_freq;
  123. };
  124. struct tegra_devfreq {
  125. struct devfreq *devfreq;
  126. struct platform_device *pdev;
  127. struct reset_control *reset;
  128. struct clk *clock;
  129. void __iomem *regs;
  130. spinlock_t lock;
  131. struct clk *emc_clock;
  132. unsigned long max_freq;
  133. unsigned long cur_freq;
  134. struct notifier_block rate_change_nb;
  135. struct tegra_devfreq_device devices[ARRAY_SIZE(actmon_device_configs)];
  136. };
  137. struct tegra_actmon_emc_ratio {
  138. unsigned long cpu_freq;
  139. unsigned long emc_freq;
  140. };
  141. static struct tegra_actmon_emc_ratio actmon_emc_ratios[] = {
  142. { 1400000, ULONG_MAX },
  143. { 1200000, 750000 },
  144. { 1100000, 600000 },
  145. { 1000000, 500000 },
  146. { 800000, 375000 },
  147. { 500000, 200000 },
  148. { 250000, 100000 },
  149. };
  150. static unsigned long do_percent(unsigned long val, unsigned int pct)
  151. {
  152. return val * pct / 100;
  153. }
  154. static void tegra_devfreq_update_avg_wmark(struct tegra_devfreq_device *dev)
  155. {
  156. u32 avg = dev->avg_count;
  157. u32 band = dev->avg_band_freq * ACTMON_SAMPLING_PERIOD;
  158. writel(avg + band, dev->regs + ACTMON_DEV_AVG_UPPER_WMARK);
  159. avg = max(avg, band);
  160. writel(avg - band, dev->regs + ACTMON_DEV_AVG_LOWER_WMARK);
  161. }
  162. static void tegra_devfreq_update_wmark(struct tegra_devfreq *tegra,
  163. struct tegra_devfreq_device *dev)
  164. {
  165. u32 val = tegra->cur_freq * ACTMON_SAMPLING_PERIOD;
  166. writel(do_percent(val, dev->config->boost_up_threshold),
  167. dev->regs + ACTMON_DEV_UPPER_WMARK);
  168. writel(do_percent(val, dev->config->boost_down_threshold),
  169. dev->regs + ACTMON_DEV_LOWER_WMARK);
  170. }
  171. static void actmon_write_barrier(struct tegra_devfreq *tegra)
  172. {
  173. /* ensure the update has reached the ACTMON */
  174. wmb();
  175. readl(tegra->regs + ACTMON_GLB_STATUS);
  176. }
  177. static irqreturn_t actmon_isr(int irq, void *data)
  178. {
  179. struct tegra_devfreq *tegra = data;
  180. struct tegra_devfreq_device *dev = NULL;
  181. unsigned long flags;
  182. u32 val;
  183. unsigned int i;
  184. val = readl(tegra->regs + ACTMON_GLB_STATUS);
  185. for (i = 0; i < ARRAY_SIZE(tegra->devices); i++) {
  186. if (val & tegra->devices[i].config->irq_mask) {
  187. dev = tegra->devices + i;
  188. break;
  189. }
  190. }
  191. if (!dev)
  192. return IRQ_NONE;
  193. spin_lock_irqsave(&tegra->lock, flags);
  194. dev->avg_count = readl(dev->regs + ACTMON_DEV_AVG_COUNT);
  195. tegra_devfreq_update_avg_wmark(dev);
  196. val = readl(dev->regs + ACTMON_DEV_INTR_STATUS);
  197. if (val & ACTMON_DEV_INTR_CONSECUTIVE_UPPER) {
  198. val = readl(dev->regs + ACTMON_DEV_CTRL) |
  199. ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_EN |
  200. ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_EN;
  201. /*
  202. * new_boost = min(old_boost * up_coef + step, max_freq)
  203. */
  204. dev->boost_freq = do_percent(dev->boost_freq,
  205. dev->config->boost_up_coeff);
  206. dev->boost_freq += ACTMON_BOOST_FREQ_STEP;
  207. if (dev->boost_freq >= tegra->max_freq) {
  208. dev->boost_freq = tegra->max_freq;
  209. val &= ~ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_EN;
  210. }
  211. writel(val, dev->regs + ACTMON_DEV_CTRL);
  212. } else if (val & ACTMON_DEV_INTR_CONSECUTIVE_LOWER) {
  213. val = readl(dev->regs + ACTMON_DEV_CTRL) |
  214. ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_EN |
  215. ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_EN;
  216. /*
  217. * new_boost = old_boost * down_coef
  218. * or 0 if (old_boost * down_coef < step / 2)
  219. */
  220. dev->boost_freq = do_percent(dev->boost_freq,
  221. dev->config->boost_down_coeff);
  222. if (dev->boost_freq < (ACTMON_BOOST_FREQ_STEP >> 1)) {
  223. dev->boost_freq = 0;
  224. val &= ~ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_EN;
  225. }
  226. writel(val, dev->regs + ACTMON_DEV_CTRL);
  227. }
  228. if (dev->config->avg_dependency_threshold) {
  229. val = readl(dev->regs + ACTMON_DEV_CTRL);
  230. if (dev->avg_count >= dev->config->avg_dependency_threshold)
  231. val |= ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_EN;
  232. else if (dev->boost_freq == 0)
  233. val &= ~ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_EN;
  234. writel(val, dev->regs + ACTMON_DEV_CTRL);
  235. }
  236. writel(ACTMON_INTR_STATUS_CLEAR, dev->regs + ACTMON_DEV_INTR_STATUS);
  237. actmon_write_barrier(tegra);
  238. spin_unlock_irqrestore(&tegra->lock, flags);
  239. return IRQ_WAKE_THREAD;
  240. }
  241. static unsigned long actmon_cpu_to_emc_rate(struct tegra_devfreq *tegra,
  242. unsigned long cpu_freq)
  243. {
  244. unsigned int i;
  245. struct tegra_actmon_emc_ratio *ratio = actmon_emc_ratios;
  246. for (i = 0; i < ARRAY_SIZE(actmon_emc_ratios); i++, ratio++) {
  247. if (cpu_freq >= ratio->cpu_freq) {
  248. if (ratio->emc_freq >= tegra->max_freq)
  249. return tegra->max_freq;
  250. else
  251. return ratio->emc_freq;
  252. }
  253. }
  254. return 0;
  255. }
  256. static void actmon_update_target(struct tegra_devfreq *tegra,
  257. struct tegra_devfreq_device *dev)
  258. {
  259. unsigned long cpu_freq = 0;
  260. unsigned long static_cpu_emc_freq = 0;
  261. unsigned int avg_sustain_coef;
  262. unsigned long flags;
  263. if (dev->config->avg_dependency_threshold) {
  264. cpu_freq = cpufreq_get(0);
  265. static_cpu_emc_freq = actmon_cpu_to_emc_rate(tegra, cpu_freq);
  266. }
  267. spin_lock_irqsave(&tegra->lock, flags);
  268. dev->target_freq = dev->avg_count / ACTMON_SAMPLING_PERIOD;
  269. avg_sustain_coef = 100 * 100 / dev->config->boost_up_threshold;
  270. dev->target_freq = do_percent(dev->target_freq, avg_sustain_coef);
  271. dev->target_freq += dev->boost_freq;
  272. if (dev->avg_count >= dev->config->avg_dependency_threshold)
  273. dev->target_freq = max(dev->target_freq, static_cpu_emc_freq);
  274. spin_unlock_irqrestore(&tegra->lock, flags);
  275. }
  276. static irqreturn_t actmon_thread_isr(int irq, void *data)
  277. {
  278. struct tegra_devfreq *tegra = data;
  279. mutex_lock(&tegra->devfreq->lock);
  280. update_devfreq(tegra->devfreq);
  281. mutex_unlock(&tegra->devfreq->lock);
  282. return IRQ_HANDLED;
  283. }
  284. static int tegra_actmon_rate_notify_cb(struct notifier_block *nb,
  285. unsigned long action, void *ptr)
  286. {
  287. struct clk_notifier_data *data = ptr;
  288. struct tegra_devfreq *tegra = container_of(nb, struct tegra_devfreq,
  289. rate_change_nb);
  290. unsigned int i;
  291. unsigned long flags;
  292. spin_lock_irqsave(&tegra->lock, flags);
  293. switch (action) {
  294. case POST_RATE_CHANGE:
  295. tegra->cur_freq = data->new_rate / KHZ;
  296. for (i = 0; i < ARRAY_SIZE(tegra->devices); i++)
  297. tegra_devfreq_update_wmark(tegra, tegra->devices + i);
  298. actmon_write_barrier(tegra);
  299. break;
  300. case PRE_RATE_CHANGE:
  301. /* fall through */
  302. case ABORT_RATE_CHANGE:
  303. break;
  304. };
  305. spin_unlock_irqrestore(&tegra->lock, flags);
  306. return NOTIFY_OK;
  307. }
  308. static void tegra_actmon_configure_device(struct tegra_devfreq *tegra,
  309. struct tegra_devfreq_device *dev)
  310. {
  311. u32 val;
  312. dev->avg_band_freq = tegra->max_freq * ACTMON_DEFAULT_AVG_BAND / KHZ;
  313. dev->target_freq = tegra->cur_freq;
  314. dev->avg_count = tegra->cur_freq * ACTMON_SAMPLING_PERIOD;
  315. writel(dev->avg_count, dev->regs + ACTMON_DEV_INIT_AVG);
  316. tegra_devfreq_update_avg_wmark(dev);
  317. tegra_devfreq_update_wmark(tegra, dev);
  318. writel(ACTMON_COUNT_WEIGHT, dev->regs + ACTMON_DEV_COUNT_WEIGHT);
  319. writel(ACTMON_INTR_STATUS_CLEAR, dev->regs + ACTMON_DEV_INTR_STATUS);
  320. val = 0;
  321. val |= ACTMON_DEV_CTRL_ENB_PERIODIC |
  322. ACTMON_DEV_CTRL_AVG_ABOVE_WMARK_EN |
  323. ACTMON_DEV_CTRL_AVG_BELOW_WMARK_EN;
  324. val |= (ACTMON_AVERAGE_WINDOW_LOG2 - 1)
  325. << ACTMON_DEV_CTRL_K_VAL_SHIFT;
  326. val |= (ACTMON_BELOW_WMARK_WINDOW - 1)
  327. << ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_NUM_SHIFT;
  328. val |= (ACTMON_ABOVE_WMARK_WINDOW - 1)
  329. << ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_NUM_SHIFT;
  330. val |= ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_EN |
  331. ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_EN;
  332. writel(val, dev->regs + ACTMON_DEV_CTRL);
  333. actmon_write_barrier(tegra);
  334. val = readl(dev->regs + ACTMON_DEV_CTRL);
  335. val |= ACTMON_DEV_CTRL_ENB;
  336. writel(val, dev->regs + ACTMON_DEV_CTRL);
  337. actmon_write_barrier(tegra);
  338. }
  339. static int tegra_devfreq_suspend(struct device *dev)
  340. {
  341. struct platform_device *pdev;
  342. struct tegra_devfreq *tegra;
  343. struct tegra_devfreq_device *actmon_dev;
  344. unsigned int i;
  345. u32 val;
  346. pdev = container_of(dev, struct platform_device, dev);
  347. tegra = platform_get_drvdata(pdev);
  348. for (i = 0; i < ARRAY_SIZE(tegra->devices); i++) {
  349. actmon_dev = &tegra->devices[i];
  350. val = readl(actmon_dev->regs + ACTMON_DEV_CTRL);
  351. val &= ~ACTMON_DEV_CTRL_ENB;
  352. writel(val, actmon_dev->regs + ACTMON_DEV_CTRL);
  353. writel(ACTMON_INTR_STATUS_CLEAR,
  354. actmon_dev->regs + ACTMON_DEV_INTR_STATUS);
  355. actmon_write_barrier(tegra);
  356. }
  357. return 0;
  358. }
  359. static int tegra_devfreq_resume(struct device *dev)
  360. {
  361. struct platform_device *pdev;
  362. struct tegra_devfreq *tegra;
  363. struct tegra_devfreq_device *actmon_dev;
  364. unsigned int i;
  365. pdev = container_of(dev, struct platform_device, dev);
  366. tegra = platform_get_drvdata(pdev);
  367. for (i = 0; i < ARRAY_SIZE(tegra->devices); i++) {
  368. actmon_dev = &tegra->devices[i];
  369. tegra_actmon_configure_device(tegra, actmon_dev);
  370. }
  371. return 0;
  372. }
  373. static int tegra_devfreq_target(struct device *dev, unsigned long *freq,
  374. u32 flags)
  375. {
  376. struct platform_device *pdev;
  377. struct tegra_devfreq *tegra;
  378. struct dev_pm_opp *opp;
  379. unsigned long rate = *freq * KHZ;
  380. pdev = container_of(dev, struct platform_device, dev);
  381. tegra = platform_get_drvdata(pdev);
  382. rcu_read_lock();
  383. opp = devfreq_recommended_opp(dev, &rate, flags);
  384. if (IS_ERR(opp)) {
  385. rcu_read_unlock();
  386. dev_err(dev, "Failed to find opp for %lu KHz\n", *freq);
  387. return PTR_ERR(opp);
  388. }
  389. rate = dev_pm_opp_get_freq(opp);
  390. rcu_read_unlock();
  391. /* TODO: Once we have per-user clk constraints, set a floor */
  392. clk_set_rate(tegra->emc_clock, rate);
  393. /* TODO: Set voltage as well */
  394. return 0;
  395. }
  396. static int tegra_devfreq_get_dev_status(struct device *dev,
  397. struct devfreq_dev_status *stat)
  398. {
  399. struct platform_device *pdev;
  400. struct tegra_devfreq *tegra;
  401. struct tegra_devfreq_device *actmon_dev;
  402. pdev = container_of(dev, struct platform_device, dev);
  403. tegra = platform_get_drvdata(pdev);
  404. stat->current_frequency = tegra->cur_freq;
  405. /* To be used by the tegra governor */
  406. stat->private_data = tegra;
  407. /* The below are to be used by the other governors */
  408. actmon_dev = &tegra->devices[MCALL];
  409. /* Number of cycles spent on memory access */
  410. stat->busy_time = actmon_dev->avg_count;
  411. /* The bus can be considered to be saturated way before 100% */
  412. stat->busy_time *= 100 / BUS_SATURATION_RATIO;
  413. /* Number of cycles in a sampling period */
  414. stat->total_time = ACTMON_SAMPLING_PERIOD * tegra->cur_freq;
  415. return 0;
  416. }
  417. static int tegra_devfreq_get_target(struct devfreq *devfreq,
  418. unsigned long *freq)
  419. {
  420. struct devfreq_dev_status stat;
  421. struct tegra_devfreq *tegra;
  422. struct tegra_devfreq_device *dev;
  423. unsigned long target_freq = 0;
  424. unsigned int i;
  425. int err;
  426. err = devfreq->profile->get_dev_status(devfreq->dev.parent, &stat);
  427. if (err)
  428. return err;
  429. tegra = stat.private_data;
  430. for (i = 0; i < ARRAY_SIZE(tegra->devices); i++) {
  431. dev = &tegra->devices[i];
  432. actmon_update_target(tegra, dev);
  433. target_freq = max(target_freq, dev->target_freq);
  434. }
  435. *freq = target_freq;
  436. return 0;
  437. }
  438. static int tegra_devfreq_event_handler(struct devfreq *devfreq,
  439. unsigned int event, void *data)
  440. {
  441. return 0;
  442. }
  443. static struct devfreq_governor tegra_devfreq_governor = {
  444. .name = "tegra",
  445. .get_target_freq = tegra_devfreq_get_target,
  446. .event_handler = tegra_devfreq_event_handler,
  447. };
  448. static struct devfreq_dev_profile tegra_devfreq_profile = {
  449. .polling_ms = 0,
  450. .target = tegra_devfreq_target,
  451. .get_dev_status = tegra_devfreq_get_dev_status,
  452. };
  453. static int tegra_devfreq_probe(struct platform_device *pdev)
  454. {
  455. struct tegra_devfreq *tegra;
  456. struct tegra_devfreq_device *dev;
  457. struct resource *res;
  458. unsigned long max_freq;
  459. unsigned int i;
  460. int irq;
  461. int err;
  462. tegra = devm_kzalloc(&pdev->dev, sizeof(*tegra), GFP_KERNEL);
  463. if (!tegra)
  464. return -ENOMEM;
  465. spin_lock_init(&tegra->lock);
  466. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  467. if (!res) {
  468. dev_err(&pdev->dev, "Failed to get regs resource\n");
  469. return -ENODEV;
  470. }
  471. tegra->regs = devm_ioremap_resource(&pdev->dev, res);
  472. if (IS_ERR(tegra->regs)) {
  473. dev_err(&pdev->dev, "Failed to get IO memory\n");
  474. return PTR_ERR(tegra->regs);
  475. }
  476. tegra->reset = devm_reset_control_get(&pdev->dev, "actmon");
  477. if (IS_ERR(tegra->reset)) {
  478. dev_err(&pdev->dev, "Failed to get reset\n");
  479. return PTR_ERR(tegra->reset);
  480. }
  481. tegra->clock = devm_clk_get(&pdev->dev, "actmon");
  482. if (IS_ERR(tegra->clock)) {
  483. dev_err(&pdev->dev, "Failed to get actmon clock\n");
  484. return PTR_ERR(tegra->clock);
  485. }
  486. tegra->emc_clock = devm_clk_get(&pdev->dev, "emc");
  487. if (IS_ERR(tegra->emc_clock)) {
  488. dev_err(&pdev->dev, "Failed to get emc clock\n");
  489. return PTR_ERR(tegra->emc_clock);
  490. }
  491. err = of_init_opp_table(&pdev->dev);
  492. if (err) {
  493. dev_err(&pdev->dev, "Failed to init operating point table\n");
  494. return err;
  495. }
  496. tegra->rate_change_nb.notifier_call = tegra_actmon_rate_notify_cb;
  497. err = clk_notifier_register(tegra->emc_clock, &tegra->rate_change_nb);
  498. if (err) {
  499. dev_err(&pdev->dev,
  500. "Failed to register rate change notifier\n");
  501. return err;
  502. }
  503. reset_control_assert(tegra->reset);
  504. err = clk_prepare_enable(tegra->clock);
  505. if (err) {
  506. reset_control_deassert(tegra->reset);
  507. return err;
  508. }
  509. reset_control_deassert(tegra->reset);
  510. max_freq = clk_round_rate(tegra->emc_clock, ULONG_MAX);
  511. tegra->max_freq = max_freq / KHZ;
  512. clk_set_rate(tegra->emc_clock, max_freq);
  513. tegra->cur_freq = clk_get_rate(tegra->emc_clock) / KHZ;
  514. writel(ACTMON_SAMPLING_PERIOD - 1,
  515. tegra->regs + ACTMON_GLB_PERIOD_CTRL);
  516. for (i = 0; i < ARRAY_SIZE(actmon_device_configs); i++) {
  517. dev = tegra->devices + i;
  518. dev->config = actmon_device_configs + i;
  519. dev->regs = tegra->regs + dev->config->offset;
  520. tegra_actmon_configure_device(tegra, tegra->devices + i);
  521. }
  522. err = devfreq_add_governor(&tegra_devfreq_governor);
  523. if (err) {
  524. dev_err(&pdev->dev, "Failed to add governor\n");
  525. return err;
  526. }
  527. tegra_devfreq_profile.initial_freq = clk_get_rate(tegra->emc_clock);
  528. tegra->devfreq = devm_devfreq_add_device(&pdev->dev,
  529. &tegra_devfreq_profile,
  530. "tegra",
  531. NULL);
  532. irq = platform_get_irq(pdev, 0);
  533. err = devm_request_threaded_irq(&pdev->dev, irq, actmon_isr,
  534. actmon_thread_isr, IRQF_SHARED,
  535. "tegra-devfreq", tegra);
  536. if (err) {
  537. dev_err(&pdev->dev, "Interrupt request failed\n");
  538. return err;
  539. }
  540. platform_set_drvdata(pdev, tegra);
  541. return 0;
  542. }
  543. static int tegra_devfreq_remove(struct platform_device *pdev)
  544. {
  545. struct tegra_devfreq *tegra = platform_get_drvdata(pdev);
  546. clk_notifier_unregister(tegra->emc_clock, &tegra->rate_change_nb);
  547. clk_disable_unprepare(tegra->clock);
  548. return 0;
  549. }
  550. static SIMPLE_DEV_PM_OPS(tegra_devfreq_pm_ops,
  551. tegra_devfreq_suspend,
  552. tegra_devfreq_resume);
  553. static struct of_device_id tegra_devfreq_of_match[] = {
  554. { .compatible = "nvidia,tegra124-actmon" },
  555. { },
  556. };
  557. static struct platform_driver tegra_devfreq_driver = {
  558. .probe = tegra_devfreq_probe,
  559. .remove = tegra_devfreq_remove,
  560. .driver = {
  561. .name = "tegra-devfreq",
  562. .owner = THIS_MODULE,
  563. .of_match_table = tegra_devfreq_of_match,
  564. .pm = &tegra_devfreq_pm_ops,
  565. },
  566. };
  567. module_platform_driver(tegra_devfreq_driver);
  568. MODULE_LICENSE("GPL");
  569. MODULE_DESCRIPTION("Tegra devfreq driver");
  570. MODULE_AUTHOR("Tomeu Vizoso <tomeu.vizoso@collabora.com>");
  571. MODULE_DEVICE_TABLE(of, tegra_devfreq_of_match);