clk-dfll.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758
  1. /*
  2. * clk-dfll.c - Tegra DFLL clock source common code
  3. *
  4. * Copyright (C) 2012-2014 NVIDIA Corporation. All rights reserved.
  5. *
  6. * Aleksandr Frid <afrid@nvidia.com>
  7. * Paul Walmsley <pwalmsley@nvidia.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  16. * more details.
  17. *
  18. * This library is for the DVCO and DFLL IP blocks on the Tegra124
  19. * SoC. These IP blocks together are also known at NVIDIA as
  20. * "CL-DVFS". To try to avoid confusion, this code refers to them
  21. * collectively as the "DFLL."
  22. *
  23. * The DFLL is a root clocksource which tolerates some amount of
  24. * supply voltage noise. Tegra124 uses it to clock the fast CPU
  25. * complex when the target CPU speed is above a particular rate. The
  26. * DFLL can be operated in either open-loop mode or closed-loop mode.
  27. * In open-loop mode, the DFLL generates an output clock appropriate
  28. * to the supply voltage. In closed-loop mode, when configured with a
  29. * target frequency, the DFLL minimizes supply voltage while
  30. * delivering an average frequency equal to the target.
  31. *
  32. * Devices clocked by the DFLL must be able to tolerate frequency
  33. * variation. In the case of the CPU, it's important to note that the
  34. * CPU cycle time will vary. This has implications for
  35. * performance-measurement code and any code that relies on the CPU
  36. * cycle time to delay for a certain length of time.
  37. *
  38. */
  39. #include <linux/clk.h>
  40. #include <linux/clk-provider.h>
  41. #include <linux/debugfs.h>
  42. #include <linux/device.h>
  43. #include <linux/err.h>
  44. #include <linux/i2c.h>
  45. #include <linux/io.h>
  46. #include <linux/kernel.h>
  47. #include <linux/module.h>
  48. #include <linux/of.h>
  49. #include <linux/pm_opp.h>
  50. #include <linux/pm_runtime.h>
  51. #include <linux/regmap.h>
  52. #include <linux/regulator/consumer.h>
  53. #include <linux/reset.h>
  54. #include <linux/seq_file.h>
  55. #include "clk-dfll.h"
  56. #include "cvb.h"
  57. /*
  58. * DFLL control registers - access via dfll_{readl,writel}
  59. */
  60. /* DFLL_CTRL: DFLL control register */
  61. #define DFLL_CTRL 0x00
  62. #define DFLL_CTRL_MODE_MASK 0x03
  63. /* DFLL_CONFIG: DFLL sample rate control */
  64. #define DFLL_CONFIG 0x04
  65. #define DFLL_CONFIG_DIV_MASK 0xff
  66. #define DFLL_CONFIG_DIV_PRESCALE 32
  67. /* DFLL_PARAMS: tuning coefficients for closed loop integrator */
  68. #define DFLL_PARAMS 0x08
  69. #define DFLL_PARAMS_CG_SCALE (0x1 << 24)
  70. #define DFLL_PARAMS_FORCE_MODE_SHIFT 22
  71. #define DFLL_PARAMS_FORCE_MODE_MASK (0x3 << DFLL_PARAMS_FORCE_MODE_SHIFT)
  72. #define DFLL_PARAMS_CF_PARAM_SHIFT 16
  73. #define DFLL_PARAMS_CF_PARAM_MASK (0x3f << DFLL_PARAMS_CF_PARAM_SHIFT)
  74. #define DFLL_PARAMS_CI_PARAM_SHIFT 8
  75. #define DFLL_PARAMS_CI_PARAM_MASK (0x7 << DFLL_PARAMS_CI_PARAM_SHIFT)
  76. #define DFLL_PARAMS_CG_PARAM_SHIFT 0
  77. #define DFLL_PARAMS_CG_PARAM_MASK (0xff << DFLL_PARAMS_CG_PARAM_SHIFT)
  78. /* DFLL_TUNE0: delay line configuration register 0 */
  79. #define DFLL_TUNE0 0x0c
  80. /* DFLL_TUNE1: delay line configuration register 1 */
  81. #define DFLL_TUNE1 0x10
  82. /* DFLL_FREQ_REQ: target DFLL frequency control */
  83. #define DFLL_FREQ_REQ 0x14
  84. #define DFLL_FREQ_REQ_FORCE_ENABLE (0x1 << 28)
  85. #define DFLL_FREQ_REQ_FORCE_SHIFT 16
  86. #define DFLL_FREQ_REQ_FORCE_MASK (0xfff << DFLL_FREQ_REQ_FORCE_SHIFT)
  87. #define FORCE_MAX 2047
  88. #define FORCE_MIN -2048
  89. #define DFLL_FREQ_REQ_SCALE_SHIFT 8
  90. #define DFLL_FREQ_REQ_SCALE_MASK (0xff << DFLL_FREQ_REQ_SCALE_SHIFT)
  91. #define DFLL_FREQ_REQ_SCALE_MAX 256
  92. #define DFLL_FREQ_REQ_FREQ_VALID (0x1 << 7)
  93. #define DFLL_FREQ_REQ_MULT_SHIFT 0
  94. #define DFLL_FREQ_REG_MULT_MASK (0x7f << DFLL_FREQ_REQ_MULT_SHIFT)
  95. #define FREQ_MAX 127
  96. /* DFLL_DROOP_CTRL: droop prevention control */
  97. #define DFLL_DROOP_CTRL 0x1c
  98. /* DFLL_OUTPUT_CFG: closed loop mode control registers */
  99. /* NOTE: access via dfll_i2c_{readl,writel} */
  100. #define DFLL_OUTPUT_CFG 0x20
  101. #define DFLL_OUTPUT_CFG_I2C_ENABLE (0x1 << 30)
  102. #define OUT_MASK 0x3f
  103. #define DFLL_OUTPUT_CFG_SAFE_SHIFT 24
  104. #define DFLL_OUTPUT_CFG_SAFE_MASK \
  105. (OUT_MASK << DFLL_OUTPUT_CFG_SAFE_SHIFT)
  106. #define DFLL_OUTPUT_CFG_MAX_SHIFT 16
  107. #define DFLL_OUTPUT_CFG_MAX_MASK \
  108. (OUT_MASK << DFLL_OUTPUT_CFG_MAX_SHIFT)
  109. #define DFLL_OUTPUT_CFG_MIN_SHIFT 8
  110. #define DFLL_OUTPUT_CFG_MIN_MASK \
  111. (OUT_MASK << DFLL_OUTPUT_CFG_MIN_SHIFT)
  112. #define DFLL_OUTPUT_CFG_PWM_DELTA (0x1 << 7)
  113. #define DFLL_OUTPUT_CFG_PWM_ENABLE (0x1 << 6)
  114. #define DFLL_OUTPUT_CFG_PWM_DIV_SHIFT 0
  115. #define DFLL_OUTPUT_CFG_PWM_DIV_MASK \
  116. (OUT_MASK << DFLL_OUTPUT_CFG_PWM_DIV_SHIFT)
  117. /* DFLL_OUTPUT_FORCE: closed loop mode voltage forcing control */
  118. #define DFLL_OUTPUT_FORCE 0x24
  119. #define DFLL_OUTPUT_FORCE_ENABLE (0x1 << 6)
  120. #define DFLL_OUTPUT_FORCE_VALUE_SHIFT 0
  121. #define DFLL_OUTPUT_FORCE_VALUE_MASK \
  122. (OUT_MASK << DFLL_OUTPUT_FORCE_VALUE_SHIFT)
  123. /* DFLL_MONITOR_CTRL: internal monitor data source control */
  124. #define DFLL_MONITOR_CTRL 0x28
  125. #define DFLL_MONITOR_CTRL_FREQ 6
  126. /* DFLL_MONITOR_DATA: internal monitor data output */
  127. #define DFLL_MONITOR_DATA 0x2c
  128. #define DFLL_MONITOR_DATA_NEW_MASK (0x1 << 16)
  129. #define DFLL_MONITOR_DATA_VAL_SHIFT 0
  130. #define DFLL_MONITOR_DATA_VAL_MASK (0xFFFF << DFLL_MONITOR_DATA_VAL_SHIFT)
  131. /*
  132. * I2C output control registers - access via dfll_i2c_{readl,writel}
  133. */
  134. /* DFLL_I2C_CFG: I2C controller configuration register */
  135. #define DFLL_I2C_CFG 0x40
  136. #define DFLL_I2C_CFG_ARB_ENABLE (0x1 << 20)
  137. #define DFLL_I2C_CFG_HS_CODE_SHIFT 16
  138. #define DFLL_I2C_CFG_HS_CODE_MASK (0x7 << DFLL_I2C_CFG_HS_CODE_SHIFT)
  139. #define DFLL_I2C_CFG_PACKET_ENABLE (0x1 << 15)
  140. #define DFLL_I2C_CFG_SIZE_SHIFT 12
  141. #define DFLL_I2C_CFG_SIZE_MASK (0x7 << DFLL_I2C_CFG_SIZE_SHIFT)
  142. #define DFLL_I2C_CFG_SLAVE_ADDR_10 (0x1 << 10)
  143. #define DFLL_I2C_CFG_SLAVE_ADDR_SHIFT_7BIT 1
  144. #define DFLL_I2C_CFG_SLAVE_ADDR_SHIFT_10BIT 0
  145. /* DFLL_I2C_VDD_REG_ADDR: PMIC I2C address for closed loop mode */
  146. #define DFLL_I2C_VDD_REG_ADDR 0x44
  147. /* DFLL_I2C_STS: I2C controller status */
  148. #define DFLL_I2C_STS 0x48
  149. #define DFLL_I2C_STS_I2C_LAST_SHIFT 1
  150. #define DFLL_I2C_STS_I2C_REQ_PENDING 0x1
  151. /* DFLL_INTR_STS: DFLL interrupt status register */
  152. #define DFLL_INTR_STS 0x5c
  153. /* DFLL_INTR_EN: DFLL interrupt enable register */
  154. #define DFLL_INTR_EN 0x60
  155. #define DFLL_INTR_MIN_MASK 0x1
  156. #define DFLL_INTR_MAX_MASK 0x2
  157. /*
  158. * Integrated I2C controller registers - relative to td->i2c_controller_base
  159. */
  160. /* DFLL_I2C_CLK_DIVISOR: I2C controller clock divisor */
  161. #define DFLL_I2C_CLK_DIVISOR 0x6c
  162. #define DFLL_I2C_CLK_DIVISOR_MASK 0xffff
  163. #define DFLL_I2C_CLK_DIVISOR_FS_SHIFT 16
  164. #define DFLL_I2C_CLK_DIVISOR_HS_SHIFT 0
  165. #define DFLL_I2C_CLK_DIVISOR_PREDIV 8
  166. #define DFLL_I2C_CLK_DIVISOR_HSMODE_PREDIV 12
  167. /*
  168. * Other constants
  169. */
  170. /* MAX_DFLL_VOLTAGES: number of LUT entries in the DFLL IP block */
  171. #define MAX_DFLL_VOLTAGES 33
  172. /*
  173. * REF_CLK_CYC_PER_DVCO_SAMPLE: the number of ref_clk cycles that the hardware
  174. * integrates the DVCO counter over - used for debug rate monitoring and
  175. * droop control
  176. */
  177. #define REF_CLK_CYC_PER_DVCO_SAMPLE 4
  178. /*
  179. * REF_CLOCK_RATE: the DFLL reference clock rate currently supported by this
  180. * driver, in Hz
  181. */
  182. #define REF_CLOCK_RATE 51000000UL
  183. #define DVCO_RATE_TO_MULT(rate, ref_rate) ((rate) / ((ref_rate) / 2))
  184. #define MULT_TO_DVCO_RATE(mult, ref_rate) ((mult) * ((ref_rate) / 2))
  185. /**
  186. * enum dfll_ctrl_mode - DFLL hardware operating mode
  187. * @DFLL_UNINITIALIZED: (uninitialized state - not in hardware bitfield)
  188. * @DFLL_DISABLED: DFLL not generating an output clock
  189. * @DFLL_OPEN_LOOP: DVCO running, but DFLL not adjusting voltage
  190. * @DFLL_CLOSED_LOOP: DVCO running, and DFLL adjusting voltage to match
  191. * the requested rate
  192. *
  193. * The integer corresponding to the last two states, minus one, is
  194. * written to the DFLL hardware to change operating modes.
  195. */
  196. enum dfll_ctrl_mode {
  197. DFLL_UNINITIALIZED = 0,
  198. DFLL_DISABLED = 1,
  199. DFLL_OPEN_LOOP = 2,
  200. DFLL_CLOSED_LOOP = 3,
  201. };
  202. /**
  203. * enum dfll_tune_range - voltage range that the driver believes it's in
  204. * @DFLL_TUNE_UNINITIALIZED: DFLL tuning not yet programmed
  205. * @DFLL_TUNE_LOW: DFLL in the low-voltage range (or open-loop mode)
  206. *
  207. * Some DFLL tuning parameters may need to change depending on the
  208. * DVCO's voltage; these states represent the ranges that the driver
  209. * supports. These are software states; these values are never
  210. * written into registers.
  211. */
  212. enum dfll_tune_range {
  213. DFLL_TUNE_UNINITIALIZED = 0,
  214. DFLL_TUNE_LOW = 1,
  215. };
  216. /**
  217. * struct dfll_rate_req - target DFLL rate request data
  218. * @rate: target frequency, after the postscaling
  219. * @dvco_target_rate: target frequency, after the postscaling
  220. * @lut_index: LUT index at which voltage the dvco_target_rate will be reached
  221. * @mult_bits: value to program to the MULT bits of the DFLL_FREQ_REQ register
  222. * @scale_bits: value to program to the SCALE bits of the DFLL_FREQ_REQ register
  223. */
  224. struct dfll_rate_req {
  225. unsigned long rate;
  226. unsigned long dvco_target_rate;
  227. int lut_index;
  228. u8 mult_bits;
  229. u8 scale_bits;
  230. };
  231. struct tegra_dfll {
  232. struct device *dev;
  233. struct tegra_dfll_soc_data *soc;
  234. void __iomem *base;
  235. void __iomem *i2c_base;
  236. void __iomem *i2c_controller_base;
  237. void __iomem *lut_base;
  238. struct regulator *vdd_reg;
  239. struct clk *soc_clk;
  240. struct clk *ref_clk;
  241. struct clk *i2c_clk;
  242. struct clk *dfll_clk;
  243. struct reset_control *dvco_rst;
  244. unsigned long ref_rate;
  245. unsigned long i2c_clk_rate;
  246. unsigned long dvco_rate_min;
  247. enum dfll_ctrl_mode mode;
  248. enum dfll_tune_range tune_range;
  249. struct dentry *debugfs_dir;
  250. struct clk_hw dfll_clk_hw;
  251. const char *output_clock_name;
  252. struct dfll_rate_req last_req;
  253. unsigned long last_unrounded_rate;
  254. /* Parameters from DT */
  255. u32 droop_ctrl;
  256. u32 sample_rate;
  257. u32 force_mode;
  258. u32 cf;
  259. u32 ci;
  260. u32 cg;
  261. bool cg_scale;
  262. /* I2C interface parameters */
  263. u32 i2c_fs_rate;
  264. u32 i2c_reg;
  265. u32 i2c_slave_addr;
  266. /* i2c_lut array entries are regulator framework selectors */
  267. unsigned i2c_lut[MAX_DFLL_VOLTAGES];
  268. int i2c_lut_size;
  269. u8 lut_min, lut_max, lut_safe;
  270. };
  271. #define clk_hw_to_dfll(_hw) container_of(_hw, struct tegra_dfll, dfll_clk_hw)
  272. /* mode_name: map numeric DFLL modes to names for friendly console messages */
  273. static const char * const mode_name[] = {
  274. [DFLL_UNINITIALIZED] = "uninitialized",
  275. [DFLL_DISABLED] = "disabled",
  276. [DFLL_OPEN_LOOP] = "open_loop",
  277. [DFLL_CLOSED_LOOP] = "closed_loop",
  278. };
  279. /*
  280. * Register accessors
  281. */
  282. static inline u32 dfll_readl(struct tegra_dfll *td, u32 offs)
  283. {
  284. return __raw_readl(td->base + offs);
  285. }
  286. static inline void dfll_writel(struct tegra_dfll *td, u32 val, u32 offs)
  287. {
  288. WARN_ON(offs >= DFLL_I2C_CFG);
  289. __raw_writel(val, td->base + offs);
  290. }
  291. static inline void dfll_wmb(struct tegra_dfll *td)
  292. {
  293. dfll_readl(td, DFLL_CTRL);
  294. }
  295. /* I2C output control registers - for addresses above DFLL_I2C_CFG */
  296. static inline u32 dfll_i2c_readl(struct tegra_dfll *td, u32 offs)
  297. {
  298. return __raw_readl(td->i2c_base + offs);
  299. }
  300. static inline void dfll_i2c_writel(struct tegra_dfll *td, u32 val, u32 offs)
  301. {
  302. __raw_writel(val, td->i2c_base + offs);
  303. }
  304. static inline void dfll_i2c_wmb(struct tegra_dfll *td)
  305. {
  306. dfll_i2c_readl(td, DFLL_I2C_CFG);
  307. }
  308. /**
  309. * dfll_is_running - is the DFLL currently generating a clock?
  310. * @td: DFLL instance
  311. *
  312. * If the DFLL is currently generating an output clock signal, return
  313. * true; otherwise return false.
  314. */
  315. static bool dfll_is_running(struct tegra_dfll *td)
  316. {
  317. return td->mode >= DFLL_OPEN_LOOP;
  318. }
  319. /*
  320. * Runtime PM suspend/resume callbacks
  321. */
  322. /**
  323. * tegra_dfll_runtime_resume - enable all clocks needed by the DFLL
  324. * @dev: DFLL device *
  325. *
  326. * Enable all clocks needed by the DFLL. Assumes that clk_prepare()
  327. * has already been called on all the clocks.
  328. *
  329. * XXX Should also handle context restore when returning from off.
  330. */
  331. int tegra_dfll_runtime_resume(struct device *dev)
  332. {
  333. struct tegra_dfll *td = dev_get_drvdata(dev);
  334. int ret;
  335. ret = clk_enable(td->ref_clk);
  336. if (ret) {
  337. dev_err(dev, "could not enable ref clock: %d\n", ret);
  338. return ret;
  339. }
  340. ret = clk_enable(td->soc_clk);
  341. if (ret) {
  342. dev_err(dev, "could not enable register clock: %d\n", ret);
  343. clk_disable(td->ref_clk);
  344. return ret;
  345. }
  346. ret = clk_enable(td->i2c_clk);
  347. if (ret) {
  348. dev_err(dev, "could not enable i2c clock: %d\n", ret);
  349. clk_disable(td->soc_clk);
  350. clk_disable(td->ref_clk);
  351. return ret;
  352. }
  353. return 0;
  354. }
  355. EXPORT_SYMBOL(tegra_dfll_runtime_resume);
  356. /**
  357. * tegra_dfll_runtime_suspend - disable all clocks needed by the DFLL
  358. * @dev: DFLL device *
  359. *
  360. * Disable all clocks needed by the DFLL. Assumes that other code
  361. * will later call clk_unprepare().
  362. */
  363. int tegra_dfll_runtime_suspend(struct device *dev)
  364. {
  365. struct tegra_dfll *td = dev_get_drvdata(dev);
  366. clk_disable(td->ref_clk);
  367. clk_disable(td->soc_clk);
  368. clk_disable(td->i2c_clk);
  369. return 0;
  370. }
  371. EXPORT_SYMBOL(tegra_dfll_runtime_suspend);
  372. /*
  373. * DFLL tuning operations (per-voltage-range tuning settings)
  374. */
  375. /**
  376. * dfll_tune_low - tune to DFLL and CPU settings valid for any voltage
  377. * @td: DFLL instance
  378. *
  379. * Tune the DFLL oscillator parameters and the CPU clock shaper for
  380. * the low-voltage range. These settings are valid for any voltage,
  381. * but may not be optimal.
  382. */
  383. static void dfll_tune_low(struct tegra_dfll *td)
  384. {
  385. td->tune_range = DFLL_TUNE_LOW;
  386. dfll_writel(td, td->soc->cvb->cpu_dfll_data.tune0_low, DFLL_TUNE0);
  387. dfll_writel(td, td->soc->cvb->cpu_dfll_data.tune1, DFLL_TUNE1);
  388. dfll_wmb(td);
  389. if (td->soc->set_clock_trimmers_low)
  390. td->soc->set_clock_trimmers_low();
  391. }
  392. /*
  393. * Output clock scaler helpers
  394. */
  395. /**
  396. * dfll_scale_dvco_rate - calculate scaled rate from the DVCO rate
  397. * @scale_bits: clock scaler value (bits in the DFLL_FREQ_REQ_SCALE field)
  398. * @dvco_rate: the DVCO rate
  399. *
  400. * Apply the same scaling formula that the DFLL hardware uses to scale
  401. * the DVCO rate.
  402. */
  403. static unsigned long dfll_scale_dvco_rate(int scale_bits,
  404. unsigned long dvco_rate)
  405. {
  406. return (u64)dvco_rate * (scale_bits + 1) / DFLL_FREQ_REQ_SCALE_MAX;
  407. }
  408. /*
  409. * DFLL mode switching
  410. */
  411. /**
  412. * dfll_set_mode - change the DFLL control mode
  413. * @td: DFLL instance
  414. * @mode: DFLL control mode (see enum dfll_ctrl_mode)
  415. *
  416. * Change the DFLL's operating mode between disabled, open-loop mode,
  417. * and closed-loop mode, or vice versa.
  418. */
  419. static void dfll_set_mode(struct tegra_dfll *td,
  420. enum dfll_ctrl_mode mode)
  421. {
  422. td->mode = mode;
  423. dfll_writel(td, mode - 1, DFLL_CTRL);
  424. dfll_wmb(td);
  425. }
  426. /*
  427. * DFLL-to-I2C controller interface
  428. */
  429. /**
  430. * dfll_i2c_set_output_enabled - enable/disable I2C PMIC voltage requests
  431. * @td: DFLL instance
  432. * @enable: whether to enable or disable the I2C voltage requests
  433. *
  434. * Set the master enable control for I2C control value updates. If disabled,
  435. * then I2C control messages are inhibited, regardless of the DFLL mode.
  436. */
  437. static int dfll_i2c_set_output_enabled(struct tegra_dfll *td, bool enable)
  438. {
  439. u32 val;
  440. val = dfll_i2c_readl(td, DFLL_OUTPUT_CFG);
  441. if (enable)
  442. val |= DFLL_OUTPUT_CFG_I2C_ENABLE;
  443. else
  444. val &= ~DFLL_OUTPUT_CFG_I2C_ENABLE;
  445. dfll_i2c_writel(td, val, DFLL_OUTPUT_CFG);
  446. dfll_i2c_wmb(td);
  447. return 0;
  448. }
  449. /**
  450. * dfll_load_lut - load the voltage lookup table
  451. * @td: struct tegra_dfll *
  452. *
  453. * Load the voltage-to-PMIC register value lookup table into the DFLL
  454. * IP block memory. Look-up tables can be loaded at any time.
  455. */
  456. static void dfll_load_i2c_lut(struct tegra_dfll *td)
  457. {
  458. int i, lut_index;
  459. u32 val;
  460. for (i = 0; i < MAX_DFLL_VOLTAGES; i++) {
  461. if (i < td->lut_min)
  462. lut_index = td->lut_min;
  463. else if (i > td->lut_max)
  464. lut_index = td->lut_max;
  465. else
  466. lut_index = i;
  467. val = regulator_list_hardware_vsel(td->vdd_reg,
  468. td->i2c_lut[lut_index]);
  469. __raw_writel(val, td->lut_base + i * 4);
  470. }
  471. dfll_i2c_wmb(td);
  472. }
  473. /**
  474. * dfll_init_i2c_if - set up the DFLL's DFLL-I2C interface
  475. * @td: DFLL instance
  476. *
  477. * During DFLL driver initialization, program the DFLL-I2C interface
  478. * with the PMU slave address, vdd register offset, and transfer mode.
  479. * This data is used by the DFLL to automatically construct I2C
  480. * voltage-set commands, which are then passed to the DFLL's internal
  481. * I2C controller.
  482. */
  483. static void dfll_init_i2c_if(struct tegra_dfll *td)
  484. {
  485. u32 val;
  486. if (td->i2c_slave_addr > 0x7f) {
  487. val = td->i2c_slave_addr << DFLL_I2C_CFG_SLAVE_ADDR_SHIFT_10BIT;
  488. val |= DFLL_I2C_CFG_SLAVE_ADDR_10;
  489. } else {
  490. val = td->i2c_slave_addr << DFLL_I2C_CFG_SLAVE_ADDR_SHIFT_7BIT;
  491. }
  492. val |= DFLL_I2C_CFG_SIZE_MASK;
  493. val |= DFLL_I2C_CFG_ARB_ENABLE;
  494. dfll_i2c_writel(td, val, DFLL_I2C_CFG);
  495. dfll_i2c_writel(td, td->i2c_reg, DFLL_I2C_VDD_REG_ADDR);
  496. val = DIV_ROUND_UP(td->i2c_clk_rate, td->i2c_fs_rate * 8);
  497. BUG_ON(!val || (val > DFLL_I2C_CLK_DIVISOR_MASK));
  498. val = (val - 1) << DFLL_I2C_CLK_DIVISOR_FS_SHIFT;
  499. /* default hs divisor just in case */
  500. val |= 1 << DFLL_I2C_CLK_DIVISOR_HS_SHIFT;
  501. __raw_writel(val, td->i2c_controller_base + DFLL_I2C_CLK_DIVISOR);
  502. dfll_i2c_wmb(td);
  503. }
  504. /**
  505. * dfll_init_out_if - prepare DFLL-to-PMIC interface
  506. * @td: DFLL instance
  507. *
  508. * During DFLL driver initialization or resume from context loss,
  509. * disable the I2C command output to the PMIC, set safe voltage and
  510. * output limits, and disable and clear limit interrupts.
  511. */
  512. static void dfll_init_out_if(struct tegra_dfll *td)
  513. {
  514. u32 val;
  515. td->lut_min = 0;
  516. td->lut_max = td->i2c_lut_size - 1;
  517. td->lut_safe = td->lut_min + 1;
  518. dfll_i2c_writel(td, 0, DFLL_OUTPUT_CFG);
  519. val = (td->lut_safe << DFLL_OUTPUT_CFG_SAFE_SHIFT) |
  520. (td->lut_max << DFLL_OUTPUT_CFG_MAX_SHIFT) |
  521. (td->lut_min << DFLL_OUTPUT_CFG_MIN_SHIFT);
  522. dfll_i2c_writel(td, val, DFLL_OUTPUT_CFG);
  523. dfll_i2c_wmb(td);
  524. dfll_writel(td, 0, DFLL_OUTPUT_FORCE);
  525. dfll_i2c_writel(td, 0, DFLL_INTR_EN);
  526. dfll_i2c_writel(td, DFLL_INTR_MAX_MASK | DFLL_INTR_MIN_MASK,
  527. DFLL_INTR_STS);
  528. dfll_load_i2c_lut(td);
  529. dfll_init_i2c_if(td);
  530. }
  531. /*
  532. * Set/get the DFLL's targeted output clock rate
  533. */
  534. /**
  535. * find_lut_index_for_rate - determine I2C LUT index for given DFLL rate
  536. * @td: DFLL instance
  537. * @rate: clock rate
  538. *
  539. * Determines the index of a I2C LUT entry for a voltage that approximately
  540. * produces the given DFLL clock rate. This is used when forcing a value
  541. * to the integrator during rate changes. Returns -ENOENT if a suitable
  542. * LUT index is not found.
  543. */
  544. static int find_lut_index_for_rate(struct tegra_dfll *td, unsigned long rate)
  545. {
  546. struct dev_pm_opp *opp;
  547. int i, uv;
  548. opp = dev_pm_opp_find_freq_ceil(td->soc->dev, &rate);
  549. if (IS_ERR(opp))
  550. return PTR_ERR(opp);
  551. uv = dev_pm_opp_get_voltage(opp);
  552. dev_pm_opp_put(opp);
  553. for (i = 0; i < td->i2c_lut_size; i++) {
  554. if (regulator_list_voltage(td->vdd_reg, td->i2c_lut[i]) == uv)
  555. return i;
  556. }
  557. return -ENOENT;
  558. }
  559. /**
  560. * dfll_calculate_rate_request - calculate DFLL parameters for a given rate
  561. * @td: DFLL instance
  562. * @req: DFLL-rate-request structure
  563. * @rate: the desired DFLL rate
  564. *
  565. * Populate the DFLL-rate-request record @req fields with the scale_bits
  566. * and mult_bits fields, based on the target input rate. Returns 0 upon
  567. * success, or -EINVAL if the requested rate in req->rate is too high
  568. * or low for the DFLL to generate.
  569. */
  570. static int dfll_calculate_rate_request(struct tegra_dfll *td,
  571. struct dfll_rate_req *req,
  572. unsigned long rate)
  573. {
  574. u32 val;
  575. /*
  576. * If requested rate is below the minimum DVCO rate, active the scaler.
  577. * In the future the DVCO minimum voltage should be selected based on
  578. * chip temperature and the actual minimum rate should be calibrated
  579. * at runtime.
  580. */
  581. req->scale_bits = DFLL_FREQ_REQ_SCALE_MAX - 1;
  582. if (rate < td->dvco_rate_min) {
  583. int scale;
  584. scale = DIV_ROUND_CLOSEST(rate / 1000 * DFLL_FREQ_REQ_SCALE_MAX,
  585. td->dvco_rate_min / 1000);
  586. if (!scale) {
  587. dev_err(td->dev, "%s: Rate %lu is too low\n",
  588. __func__, rate);
  589. return -EINVAL;
  590. }
  591. req->scale_bits = scale - 1;
  592. rate = td->dvco_rate_min;
  593. }
  594. /* Convert requested rate into frequency request and scale settings */
  595. val = DVCO_RATE_TO_MULT(rate, td->ref_rate);
  596. if (val > FREQ_MAX) {
  597. dev_err(td->dev, "%s: Rate %lu is above dfll range\n",
  598. __func__, rate);
  599. return -EINVAL;
  600. }
  601. req->mult_bits = val;
  602. req->dvco_target_rate = MULT_TO_DVCO_RATE(req->mult_bits, td->ref_rate);
  603. req->rate = dfll_scale_dvco_rate(req->scale_bits,
  604. req->dvco_target_rate);
  605. req->lut_index = find_lut_index_for_rate(td, req->dvco_target_rate);
  606. if (req->lut_index < 0)
  607. return req->lut_index;
  608. return 0;
  609. }
  610. /**
  611. * dfll_set_frequency_request - start the frequency change operation
  612. * @td: DFLL instance
  613. * @req: rate request structure
  614. *
  615. * Tell the DFLL to try to change its output frequency to the
  616. * frequency represented by @req. DFLL must be in closed-loop mode.
  617. */
  618. static void dfll_set_frequency_request(struct tegra_dfll *td,
  619. struct dfll_rate_req *req)
  620. {
  621. u32 val = 0;
  622. int force_val;
  623. int coef = 128; /* FIXME: td->cg_scale? */;
  624. force_val = (req->lut_index - td->lut_safe) * coef / td->cg;
  625. force_val = clamp(force_val, FORCE_MIN, FORCE_MAX);
  626. val |= req->mult_bits << DFLL_FREQ_REQ_MULT_SHIFT;
  627. val |= req->scale_bits << DFLL_FREQ_REQ_SCALE_SHIFT;
  628. val |= ((u32)force_val << DFLL_FREQ_REQ_FORCE_SHIFT) &
  629. DFLL_FREQ_REQ_FORCE_MASK;
  630. val |= DFLL_FREQ_REQ_FREQ_VALID | DFLL_FREQ_REQ_FORCE_ENABLE;
  631. dfll_writel(td, val, DFLL_FREQ_REQ);
  632. dfll_wmb(td);
  633. }
  634. /**
  635. * tegra_dfll_request_rate - set the next rate for the DFLL to tune to
  636. * @td: DFLL instance
  637. * @rate: clock rate to target
  638. *
  639. * Convert the requested clock rate @rate into the DFLL control logic
  640. * settings. In closed-loop mode, update new settings immediately to
  641. * adjust DFLL output rate accordingly. Otherwise, just save them
  642. * until the next switch to closed loop. Returns 0 upon success,
  643. * -EPERM if the DFLL driver has not yet been initialized, or -EINVAL
  644. * if @rate is outside the DFLL's tunable range.
  645. */
  646. static int dfll_request_rate(struct tegra_dfll *td, unsigned long rate)
  647. {
  648. int ret;
  649. struct dfll_rate_req req;
  650. if (td->mode == DFLL_UNINITIALIZED) {
  651. dev_err(td->dev, "%s: Cannot set DFLL rate in %s mode\n",
  652. __func__, mode_name[td->mode]);
  653. return -EPERM;
  654. }
  655. ret = dfll_calculate_rate_request(td, &req, rate);
  656. if (ret)
  657. return ret;
  658. td->last_unrounded_rate = rate;
  659. td->last_req = req;
  660. if (td->mode == DFLL_CLOSED_LOOP)
  661. dfll_set_frequency_request(td, &td->last_req);
  662. return 0;
  663. }
  664. /*
  665. * DFLL enable/disable & open-loop <-> closed-loop transitions
  666. */
  667. /**
  668. * dfll_disable - switch from open-loop mode to disabled mode
  669. * @td: DFLL instance
  670. *
  671. * Switch from OPEN_LOOP state to DISABLED state. Returns 0 upon success
  672. * or -EPERM if the DFLL is not currently in open-loop mode.
  673. */
  674. static int dfll_disable(struct tegra_dfll *td)
  675. {
  676. if (td->mode != DFLL_OPEN_LOOP) {
  677. dev_err(td->dev, "cannot disable DFLL in %s mode\n",
  678. mode_name[td->mode]);
  679. return -EINVAL;
  680. }
  681. dfll_set_mode(td, DFLL_DISABLED);
  682. pm_runtime_put_sync(td->dev);
  683. return 0;
  684. }
  685. /**
  686. * dfll_enable - switch a disabled DFLL to open-loop mode
  687. * @td: DFLL instance
  688. *
  689. * Switch from DISABLED state to OPEN_LOOP state. Returns 0 upon success
  690. * or -EPERM if the DFLL is not currently disabled.
  691. */
  692. static int dfll_enable(struct tegra_dfll *td)
  693. {
  694. if (td->mode != DFLL_DISABLED) {
  695. dev_err(td->dev, "cannot enable DFLL in %s mode\n",
  696. mode_name[td->mode]);
  697. return -EPERM;
  698. }
  699. pm_runtime_get_sync(td->dev);
  700. dfll_set_mode(td, DFLL_OPEN_LOOP);
  701. return 0;
  702. }
  703. /**
  704. * dfll_set_open_loop_config - prepare to switch to open-loop mode
  705. * @td: DFLL instance
  706. *
  707. * Prepare to switch the DFLL to open-loop mode. This switches the
  708. * DFLL to the low-voltage tuning range, ensures that I2C output
  709. * forcing is disabled, and disables the output clock rate scaler.
  710. * The DFLL's low-voltage tuning range parameters must be
  711. * characterized to keep the downstream device stable at any DVCO
  712. * input voltage. No return value.
  713. */
  714. static void dfll_set_open_loop_config(struct tegra_dfll *td)
  715. {
  716. u32 val;
  717. /* always tune low (safe) in open loop */
  718. if (td->tune_range != DFLL_TUNE_LOW)
  719. dfll_tune_low(td);
  720. val = dfll_readl(td, DFLL_FREQ_REQ);
  721. val |= DFLL_FREQ_REQ_SCALE_MASK;
  722. val &= ~DFLL_FREQ_REQ_FORCE_ENABLE;
  723. dfll_writel(td, val, DFLL_FREQ_REQ);
  724. dfll_wmb(td);
  725. }
  726. /**
  727. * tegra_dfll_lock - switch from open-loop to closed-loop mode
  728. * @td: DFLL instance
  729. *
  730. * Switch from OPEN_LOOP state to CLOSED_LOOP state. Returns 0 upon success,
  731. * -EINVAL if the DFLL's target rate hasn't been set yet, or -EPERM if the
  732. * DFLL is not currently in open-loop mode.
  733. */
  734. static int dfll_lock(struct tegra_dfll *td)
  735. {
  736. struct dfll_rate_req *req = &td->last_req;
  737. switch (td->mode) {
  738. case DFLL_CLOSED_LOOP:
  739. return 0;
  740. case DFLL_OPEN_LOOP:
  741. if (req->rate == 0) {
  742. dev_err(td->dev, "%s: Cannot lock DFLL at rate 0\n",
  743. __func__);
  744. return -EINVAL;
  745. }
  746. dfll_i2c_set_output_enabled(td, true);
  747. dfll_set_mode(td, DFLL_CLOSED_LOOP);
  748. dfll_set_frequency_request(td, req);
  749. return 0;
  750. default:
  751. BUG_ON(td->mode > DFLL_CLOSED_LOOP);
  752. dev_err(td->dev, "%s: Cannot lock DFLL in %s mode\n",
  753. __func__, mode_name[td->mode]);
  754. return -EPERM;
  755. }
  756. }
  757. /**
  758. * tegra_dfll_unlock - switch from closed-loop to open-loop mode
  759. * @td: DFLL instance
  760. *
  761. * Switch from CLOSED_LOOP state to OPEN_LOOP state. Returns 0 upon success,
  762. * or -EPERM if the DFLL is not currently in open-loop mode.
  763. */
  764. static int dfll_unlock(struct tegra_dfll *td)
  765. {
  766. switch (td->mode) {
  767. case DFLL_CLOSED_LOOP:
  768. dfll_set_open_loop_config(td);
  769. dfll_set_mode(td, DFLL_OPEN_LOOP);
  770. dfll_i2c_set_output_enabled(td, false);
  771. return 0;
  772. case DFLL_OPEN_LOOP:
  773. return 0;
  774. default:
  775. BUG_ON(td->mode > DFLL_CLOSED_LOOP);
  776. dev_err(td->dev, "%s: Cannot unlock DFLL in %s mode\n",
  777. __func__, mode_name[td->mode]);
  778. return -EPERM;
  779. }
  780. }
  781. /*
  782. * Clock framework integration
  783. *
  784. * When the DFLL is being controlled by the CCF, always enter closed loop
  785. * mode when the clk is enabled. This requires that a DFLL rate request
  786. * has been set beforehand, which implies that a clk_set_rate() call is
  787. * always required before a clk_enable().
  788. */
  789. static int dfll_clk_is_enabled(struct clk_hw *hw)
  790. {
  791. struct tegra_dfll *td = clk_hw_to_dfll(hw);
  792. return dfll_is_running(td);
  793. }
  794. static int dfll_clk_enable(struct clk_hw *hw)
  795. {
  796. struct tegra_dfll *td = clk_hw_to_dfll(hw);
  797. int ret;
  798. ret = dfll_enable(td);
  799. if (ret)
  800. return ret;
  801. ret = dfll_lock(td);
  802. if (ret)
  803. dfll_disable(td);
  804. return ret;
  805. }
  806. static void dfll_clk_disable(struct clk_hw *hw)
  807. {
  808. struct tegra_dfll *td = clk_hw_to_dfll(hw);
  809. int ret;
  810. ret = dfll_unlock(td);
  811. if (!ret)
  812. dfll_disable(td);
  813. }
  814. static unsigned long dfll_clk_recalc_rate(struct clk_hw *hw,
  815. unsigned long parent_rate)
  816. {
  817. struct tegra_dfll *td = clk_hw_to_dfll(hw);
  818. return td->last_unrounded_rate;
  819. }
  820. /* Must use determine_rate since it allows for rates exceeding 2^31-1 */
  821. static int dfll_clk_determine_rate(struct clk_hw *hw,
  822. struct clk_rate_request *clk_req)
  823. {
  824. struct tegra_dfll *td = clk_hw_to_dfll(hw);
  825. struct dfll_rate_req req;
  826. int ret;
  827. ret = dfll_calculate_rate_request(td, &req, clk_req->rate);
  828. if (ret)
  829. return ret;
  830. /*
  831. * Don't set the rounded rate, since it doesn't really matter as
  832. * the output rate will be voltage controlled anyway, and cpufreq
  833. * freaks out if any rounding happens.
  834. */
  835. return 0;
  836. }
  837. static int dfll_clk_set_rate(struct clk_hw *hw, unsigned long rate,
  838. unsigned long parent_rate)
  839. {
  840. struct tegra_dfll *td = clk_hw_to_dfll(hw);
  841. return dfll_request_rate(td, rate);
  842. }
  843. static const struct clk_ops dfll_clk_ops = {
  844. .is_enabled = dfll_clk_is_enabled,
  845. .enable = dfll_clk_enable,
  846. .disable = dfll_clk_disable,
  847. .recalc_rate = dfll_clk_recalc_rate,
  848. .determine_rate = dfll_clk_determine_rate,
  849. .set_rate = dfll_clk_set_rate,
  850. };
  851. static struct clk_init_data dfll_clk_init_data = {
  852. .ops = &dfll_clk_ops,
  853. .num_parents = 0,
  854. };
  855. /**
  856. * dfll_register_clk - register the DFLL output clock with the clock framework
  857. * @td: DFLL instance
  858. *
  859. * Register the DFLL's output clock with the Linux clock framework and register
  860. * the DFLL driver as an OF clock provider. Returns 0 upon success or -EINVAL
  861. * or -ENOMEM upon failure.
  862. */
  863. static int dfll_register_clk(struct tegra_dfll *td)
  864. {
  865. int ret;
  866. dfll_clk_init_data.name = td->output_clock_name;
  867. td->dfll_clk_hw.init = &dfll_clk_init_data;
  868. td->dfll_clk = clk_register(td->dev, &td->dfll_clk_hw);
  869. if (IS_ERR(td->dfll_clk)) {
  870. dev_err(td->dev, "DFLL clock registration error\n");
  871. return -EINVAL;
  872. }
  873. ret = of_clk_add_provider(td->dev->of_node, of_clk_src_simple_get,
  874. td->dfll_clk);
  875. if (ret) {
  876. dev_err(td->dev, "of_clk_add_provider() failed\n");
  877. clk_unregister(td->dfll_clk);
  878. return ret;
  879. }
  880. return 0;
  881. }
  882. /**
  883. * dfll_unregister_clk - unregister the DFLL output clock
  884. * @td: DFLL instance
  885. *
  886. * Unregister the DFLL's output clock from the Linux clock framework
  887. * and from clkdev. No return value.
  888. */
  889. static void dfll_unregister_clk(struct tegra_dfll *td)
  890. {
  891. of_clk_del_provider(td->dev->of_node);
  892. clk_unregister(td->dfll_clk);
  893. td->dfll_clk = NULL;
  894. }
  895. /*
  896. * Debugfs interface
  897. */
  898. #ifdef CONFIG_DEBUG_FS
  899. /*
  900. * Monitor control
  901. */
  902. /**
  903. * dfll_calc_monitored_rate - convert DFLL_MONITOR_DATA_VAL rate into real freq
  904. * @monitor_data: value read from the DFLL_MONITOR_DATA_VAL bitfield
  905. * @ref_rate: DFLL reference clock rate
  906. *
  907. * Convert @monitor_data from DFLL_MONITOR_DATA_VAL units into cycles
  908. * per second. Returns the converted value.
  909. */
  910. static u64 dfll_calc_monitored_rate(u32 monitor_data,
  911. unsigned long ref_rate)
  912. {
  913. return monitor_data * (ref_rate / REF_CLK_CYC_PER_DVCO_SAMPLE);
  914. }
  915. /**
  916. * dfll_read_monitor_rate - return the DFLL's output rate from internal monitor
  917. * @td: DFLL instance
  918. *
  919. * If the DFLL is enabled, return the last rate reported by the DFLL's
  920. * internal monitoring hardware. This works in both open-loop and
  921. * closed-loop mode, and takes the output scaler setting into account.
  922. * Assumes that the monitor was programmed to monitor frequency before
  923. * the sample period started. If the driver believes that the DFLL is
  924. * currently uninitialized or disabled, it will return 0, since
  925. * otherwise the DFLL monitor data register will return the last
  926. * measured rate from when the DFLL was active.
  927. */
  928. static u64 dfll_read_monitor_rate(struct tegra_dfll *td)
  929. {
  930. u32 v, s;
  931. u64 pre_scaler_rate, post_scaler_rate;
  932. if (!dfll_is_running(td))
  933. return 0;
  934. v = dfll_readl(td, DFLL_MONITOR_DATA);
  935. v = (v & DFLL_MONITOR_DATA_VAL_MASK) >> DFLL_MONITOR_DATA_VAL_SHIFT;
  936. pre_scaler_rate = dfll_calc_monitored_rate(v, td->ref_rate);
  937. s = dfll_readl(td, DFLL_FREQ_REQ);
  938. s = (s & DFLL_FREQ_REQ_SCALE_MASK) >> DFLL_FREQ_REQ_SCALE_SHIFT;
  939. post_scaler_rate = dfll_scale_dvco_rate(s, pre_scaler_rate);
  940. return post_scaler_rate;
  941. }
  942. static int attr_enable_get(void *data, u64 *val)
  943. {
  944. struct tegra_dfll *td = data;
  945. *val = dfll_is_running(td);
  946. return 0;
  947. }
  948. static int attr_enable_set(void *data, u64 val)
  949. {
  950. struct tegra_dfll *td = data;
  951. return val ? dfll_enable(td) : dfll_disable(td);
  952. }
  953. DEFINE_SIMPLE_ATTRIBUTE(enable_fops, attr_enable_get, attr_enable_set,
  954. "%llu\n");
  955. static int attr_lock_get(void *data, u64 *val)
  956. {
  957. struct tegra_dfll *td = data;
  958. *val = (td->mode == DFLL_CLOSED_LOOP);
  959. return 0;
  960. }
  961. static int attr_lock_set(void *data, u64 val)
  962. {
  963. struct tegra_dfll *td = data;
  964. return val ? dfll_lock(td) : dfll_unlock(td);
  965. }
  966. DEFINE_SIMPLE_ATTRIBUTE(lock_fops, attr_lock_get, attr_lock_set,
  967. "%llu\n");
  968. static int attr_rate_get(void *data, u64 *val)
  969. {
  970. struct tegra_dfll *td = data;
  971. *val = dfll_read_monitor_rate(td);
  972. return 0;
  973. }
  974. static int attr_rate_set(void *data, u64 val)
  975. {
  976. struct tegra_dfll *td = data;
  977. return dfll_request_rate(td, val);
  978. }
  979. DEFINE_SIMPLE_ATTRIBUTE(rate_fops, attr_rate_get, attr_rate_set, "%llu\n");
  980. static int attr_registers_show(struct seq_file *s, void *data)
  981. {
  982. u32 val, offs;
  983. struct tegra_dfll *td = s->private;
  984. seq_puts(s, "CONTROL REGISTERS:\n");
  985. for (offs = 0; offs <= DFLL_MONITOR_DATA; offs += 4) {
  986. if (offs == DFLL_OUTPUT_CFG)
  987. val = dfll_i2c_readl(td, offs);
  988. else
  989. val = dfll_readl(td, offs);
  990. seq_printf(s, "[0x%02x] = 0x%08x\n", offs, val);
  991. }
  992. seq_puts(s, "\nI2C and INTR REGISTERS:\n");
  993. for (offs = DFLL_I2C_CFG; offs <= DFLL_I2C_STS; offs += 4)
  994. seq_printf(s, "[0x%02x] = 0x%08x\n", offs,
  995. dfll_i2c_readl(td, offs));
  996. for (offs = DFLL_INTR_STS; offs <= DFLL_INTR_EN; offs += 4)
  997. seq_printf(s, "[0x%02x] = 0x%08x\n", offs,
  998. dfll_i2c_readl(td, offs));
  999. seq_puts(s, "\nINTEGRATED I2C CONTROLLER REGISTERS:\n");
  1000. offs = DFLL_I2C_CLK_DIVISOR;
  1001. seq_printf(s, "[0x%02x] = 0x%08x\n", offs,
  1002. __raw_readl(td->i2c_controller_base + offs));
  1003. seq_puts(s, "\nLUT:\n");
  1004. for (offs = 0; offs < 4 * MAX_DFLL_VOLTAGES; offs += 4)
  1005. seq_printf(s, "[0x%02x] = 0x%08x\n", offs,
  1006. __raw_readl(td->lut_base + offs));
  1007. return 0;
  1008. }
  1009. static int attr_registers_open(struct inode *inode, struct file *file)
  1010. {
  1011. return single_open(file, attr_registers_show, inode->i_private);
  1012. }
  1013. static const struct file_operations attr_registers_fops = {
  1014. .open = attr_registers_open,
  1015. .read = seq_read,
  1016. .llseek = seq_lseek,
  1017. .release = single_release,
  1018. };
  1019. static int dfll_debug_init(struct tegra_dfll *td)
  1020. {
  1021. int ret;
  1022. if (!td || (td->mode == DFLL_UNINITIALIZED))
  1023. return 0;
  1024. td->debugfs_dir = debugfs_create_dir("tegra_dfll_fcpu", NULL);
  1025. if (!td->debugfs_dir)
  1026. return -ENOMEM;
  1027. ret = -ENOMEM;
  1028. if (!debugfs_create_file("enable", S_IRUGO | S_IWUSR,
  1029. td->debugfs_dir, td, &enable_fops))
  1030. goto err_out;
  1031. if (!debugfs_create_file("lock", S_IRUGO,
  1032. td->debugfs_dir, td, &lock_fops))
  1033. goto err_out;
  1034. if (!debugfs_create_file("rate", S_IRUGO,
  1035. td->debugfs_dir, td, &rate_fops))
  1036. goto err_out;
  1037. if (!debugfs_create_file("registers", S_IRUGO,
  1038. td->debugfs_dir, td, &attr_registers_fops))
  1039. goto err_out;
  1040. return 0;
  1041. err_out:
  1042. debugfs_remove_recursive(td->debugfs_dir);
  1043. return ret;
  1044. }
  1045. #endif /* CONFIG_DEBUG_FS */
  1046. /*
  1047. * DFLL initialization
  1048. */
  1049. /**
  1050. * dfll_set_default_params - program non-output related DFLL parameters
  1051. * @td: DFLL instance
  1052. *
  1053. * During DFLL driver initialization or resume from context loss,
  1054. * program parameters for the closed loop integrator, DVCO tuning,
  1055. * voltage droop control and monitor control.
  1056. */
  1057. static void dfll_set_default_params(struct tegra_dfll *td)
  1058. {
  1059. u32 val;
  1060. val = DIV_ROUND_UP(td->ref_rate, td->sample_rate * 32);
  1061. BUG_ON(val > DFLL_CONFIG_DIV_MASK);
  1062. dfll_writel(td, val, DFLL_CONFIG);
  1063. val = (td->force_mode << DFLL_PARAMS_FORCE_MODE_SHIFT) |
  1064. (td->cf << DFLL_PARAMS_CF_PARAM_SHIFT) |
  1065. (td->ci << DFLL_PARAMS_CI_PARAM_SHIFT) |
  1066. (td->cg << DFLL_PARAMS_CG_PARAM_SHIFT) |
  1067. (td->cg_scale ? DFLL_PARAMS_CG_SCALE : 0);
  1068. dfll_writel(td, val, DFLL_PARAMS);
  1069. dfll_tune_low(td);
  1070. dfll_writel(td, td->droop_ctrl, DFLL_DROOP_CTRL);
  1071. dfll_writel(td, DFLL_MONITOR_CTRL_FREQ, DFLL_MONITOR_CTRL);
  1072. }
  1073. /**
  1074. * dfll_init_clks - clk_get() the DFLL source clocks
  1075. * @td: DFLL instance
  1076. *
  1077. * Call clk_get() on the DFLL source clocks and save the pointers for later
  1078. * use. Returns 0 upon success or error (see devm_clk_get) if one or more
  1079. * of the clocks couldn't be looked up.
  1080. */
  1081. static int dfll_init_clks(struct tegra_dfll *td)
  1082. {
  1083. td->ref_clk = devm_clk_get(td->dev, "ref");
  1084. if (IS_ERR(td->ref_clk)) {
  1085. dev_err(td->dev, "missing ref clock\n");
  1086. return PTR_ERR(td->ref_clk);
  1087. }
  1088. td->soc_clk = devm_clk_get(td->dev, "soc");
  1089. if (IS_ERR(td->soc_clk)) {
  1090. dev_err(td->dev, "missing soc clock\n");
  1091. return PTR_ERR(td->soc_clk);
  1092. }
  1093. td->i2c_clk = devm_clk_get(td->dev, "i2c");
  1094. if (IS_ERR(td->i2c_clk)) {
  1095. dev_err(td->dev, "missing i2c clock\n");
  1096. return PTR_ERR(td->i2c_clk);
  1097. }
  1098. td->i2c_clk_rate = clk_get_rate(td->i2c_clk);
  1099. return 0;
  1100. }
  1101. /**
  1102. * dfll_init - Prepare the DFLL IP block for use
  1103. * @td: DFLL instance
  1104. *
  1105. * Do everything necessary to prepare the DFLL IP block for use. The
  1106. * DFLL will be left in DISABLED state. Called by dfll_probe().
  1107. * Returns 0 upon success, or passes along the error from whatever
  1108. * function returned it.
  1109. */
  1110. static int dfll_init(struct tegra_dfll *td)
  1111. {
  1112. int ret;
  1113. td->ref_rate = clk_get_rate(td->ref_clk);
  1114. if (td->ref_rate != REF_CLOCK_RATE) {
  1115. dev_err(td->dev, "unexpected ref clk rate %lu, expecting %lu",
  1116. td->ref_rate, REF_CLOCK_RATE);
  1117. return -EINVAL;
  1118. }
  1119. reset_control_deassert(td->dvco_rst);
  1120. ret = clk_prepare(td->ref_clk);
  1121. if (ret) {
  1122. dev_err(td->dev, "failed to prepare ref_clk\n");
  1123. return ret;
  1124. }
  1125. ret = clk_prepare(td->soc_clk);
  1126. if (ret) {
  1127. dev_err(td->dev, "failed to prepare soc_clk\n");
  1128. goto di_err1;
  1129. }
  1130. ret = clk_prepare(td->i2c_clk);
  1131. if (ret) {
  1132. dev_err(td->dev, "failed to prepare i2c_clk\n");
  1133. goto di_err2;
  1134. }
  1135. td->last_unrounded_rate = 0;
  1136. pm_runtime_enable(td->dev);
  1137. pm_runtime_get_sync(td->dev);
  1138. dfll_set_mode(td, DFLL_DISABLED);
  1139. dfll_set_default_params(td);
  1140. if (td->soc->init_clock_trimmers)
  1141. td->soc->init_clock_trimmers();
  1142. dfll_set_open_loop_config(td);
  1143. dfll_init_out_if(td);
  1144. pm_runtime_put_sync(td->dev);
  1145. return 0;
  1146. di_err2:
  1147. clk_unprepare(td->soc_clk);
  1148. di_err1:
  1149. clk_unprepare(td->ref_clk);
  1150. reset_control_assert(td->dvco_rst);
  1151. return ret;
  1152. }
  1153. /*
  1154. * DT data fetch
  1155. */
  1156. /*
  1157. * Find a PMIC voltage register-to-voltage mapping for the given voltage.
  1158. * An exact voltage match is required.
  1159. */
  1160. static int find_vdd_map_entry_exact(struct tegra_dfll *td, int uV)
  1161. {
  1162. int i, n_voltages, reg_uV;
  1163. n_voltages = regulator_count_voltages(td->vdd_reg);
  1164. for (i = 0; i < n_voltages; i++) {
  1165. reg_uV = regulator_list_voltage(td->vdd_reg, i);
  1166. if (reg_uV < 0)
  1167. break;
  1168. if (uV == reg_uV)
  1169. return i;
  1170. }
  1171. dev_err(td->dev, "no voltage map entry for %d uV\n", uV);
  1172. return -EINVAL;
  1173. }
  1174. /*
  1175. * Find a PMIC voltage register-to-voltage mapping for the given voltage,
  1176. * rounding up to the closest supported voltage.
  1177. * */
  1178. static int find_vdd_map_entry_min(struct tegra_dfll *td, int uV)
  1179. {
  1180. int i, n_voltages, reg_uV;
  1181. n_voltages = regulator_count_voltages(td->vdd_reg);
  1182. for (i = 0; i < n_voltages; i++) {
  1183. reg_uV = regulator_list_voltage(td->vdd_reg, i);
  1184. if (reg_uV < 0)
  1185. break;
  1186. if (uV <= reg_uV)
  1187. return i;
  1188. }
  1189. dev_err(td->dev, "no voltage map entry rounding to %d uV\n", uV);
  1190. return -EINVAL;
  1191. }
  1192. /**
  1193. * dfll_build_i2c_lut - build the I2C voltage register lookup table
  1194. * @td: DFLL instance
  1195. *
  1196. * The DFLL hardware has 33 bytes of look-up table RAM that must be filled with
  1197. * PMIC voltage register values that span the entire DFLL operating range.
  1198. * This function builds the look-up table based on the OPP table provided by
  1199. * the soc-specific platform driver (td->soc->opp_dev) and the PMIC
  1200. * register-to-voltage mapping queried from the regulator framework.
  1201. *
  1202. * On success, fills in td->i2c_lut and returns 0, or -err on failure.
  1203. */
  1204. static int dfll_build_i2c_lut(struct tegra_dfll *td)
  1205. {
  1206. int ret = -EINVAL;
  1207. int j, v, v_max, v_opp;
  1208. int selector;
  1209. unsigned long rate;
  1210. struct dev_pm_opp *opp;
  1211. int lut;
  1212. rate = ULONG_MAX;
  1213. opp = dev_pm_opp_find_freq_floor(td->soc->dev, &rate);
  1214. if (IS_ERR(opp)) {
  1215. dev_err(td->dev, "couldn't get vmax opp, empty opp table?\n");
  1216. goto out;
  1217. }
  1218. v_max = dev_pm_opp_get_voltage(opp);
  1219. dev_pm_opp_put(opp);
  1220. v = td->soc->cvb->min_millivolts * 1000;
  1221. lut = find_vdd_map_entry_exact(td, v);
  1222. if (lut < 0)
  1223. goto out;
  1224. td->i2c_lut[0] = lut;
  1225. for (j = 1, rate = 0; ; rate++) {
  1226. opp = dev_pm_opp_find_freq_ceil(td->soc->dev, &rate);
  1227. if (IS_ERR(opp))
  1228. break;
  1229. v_opp = dev_pm_opp_get_voltage(opp);
  1230. if (v_opp <= td->soc->cvb->min_millivolts * 1000)
  1231. td->dvco_rate_min = dev_pm_opp_get_freq(opp);
  1232. dev_pm_opp_put(opp);
  1233. for (;;) {
  1234. v += max(1, (v_max - v) / (MAX_DFLL_VOLTAGES - j));
  1235. if (v >= v_opp)
  1236. break;
  1237. selector = find_vdd_map_entry_min(td, v);
  1238. if (selector < 0)
  1239. goto out;
  1240. if (selector != td->i2c_lut[j - 1])
  1241. td->i2c_lut[j++] = selector;
  1242. }
  1243. v = (j == MAX_DFLL_VOLTAGES - 1) ? v_max : v_opp;
  1244. selector = find_vdd_map_entry_exact(td, v);
  1245. if (selector < 0)
  1246. goto out;
  1247. if (selector != td->i2c_lut[j - 1])
  1248. td->i2c_lut[j++] = selector;
  1249. if (v >= v_max)
  1250. break;
  1251. }
  1252. td->i2c_lut_size = j;
  1253. if (!td->dvco_rate_min)
  1254. dev_err(td->dev, "no opp above DFLL minimum voltage %d mV\n",
  1255. td->soc->cvb->min_millivolts);
  1256. else
  1257. ret = 0;
  1258. out:
  1259. return ret;
  1260. }
  1261. /**
  1262. * read_dt_param - helper function for reading required parameters from the DT
  1263. * @td: DFLL instance
  1264. * @param: DT property name
  1265. * @dest: output pointer for the value read
  1266. *
  1267. * Read a required numeric parameter from the DFLL device node, or complain
  1268. * if the property doesn't exist. Returns a boolean indicating success for
  1269. * easy chaining of multiple calls to this function.
  1270. */
  1271. static bool read_dt_param(struct tegra_dfll *td, const char *param, u32 *dest)
  1272. {
  1273. int err = of_property_read_u32(td->dev->of_node, param, dest);
  1274. if (err < 0) {
  1275. dev_err(td->dev, "failed to read DT parameter %s: %d\n",
  1276. param, err);
  1277. return false;
  1278. }
  1279. return true;
  1280. }
  1281. /**
  1282. * dfll_fetch_i2c_params - query PMIC I2C params from DT & regulator subsystem
  1283. * @td: DFLL instance
  1284. *
  1285. * Read all the parameters required for operation in I2C mode. The parameters
  1286. * can originate from the device tree or the regulator subsystem.
  1287. * Returns 0 on success or -err on failure.
  1288. */
  1289. static int dfll_fetch_i2c_params(struct tegra_dfll *td)
  1290. {
  1291. struct regmap *regmap;
  1292. struct device *i2c_dev;
  1293. struct i2c_client *i2c_client;
  1294. int vsel_reg, vsel_mask;
  1295. int ret;
  1296. if (!read_dt_param(td, "nvidia,i2c-fs-rate", &td->i2c_fs_rate))
  1297. return -EINVAL;
  1298. regmap = regulator_get_regmap(td->vdd_reg);
  1299. i2c_dev = regmap_get_device(regmap);
  1300. i2c_client = to_i2c_client(i2c_dev);
  1301. td->i2c_slave_addr = i2c_client->addr;
  1302. ret = regulator_get_hardware_vsel_register(td->vdd_reg,
  1303. &vsel_reg,
  1304. &vsel_mask);
  1305. if (ret < 0) {
  1306. dev_err(td->dev,
  1307. "regulator unsuitable for DFLL I2C operation\n");
  1308. return -EINVAL;
  1309. }
  1310. td->i2c_reg = vsel_reg;
  1311. ret = dfll_build_i2c_lut(td);
  1312. if (ret) {
  1313. dev_err(td->dev, "couldn't build I2C LUT\n");
  1314. return ret;
  1315. }
  1316. return 0;
  1317. }
  1318. /**
  1319. * dfll_fetch_common_params - read DFLL parameters from the device tree
  1320. * @td: DFLL instance
  1321. *
  1322. * Read all the DT parameters that are common to both I2C and PWM operation.
  1323. * Returns 0 on success or -EINVAL on any failure.
  1324. */
  1325. static int dfll_fetch_common_params(struct tegra_dfll *td)
  1326. {
  1327. bool ok = true;
  1328. ok &= read_dt_param(td, "nvidia,droop-ctrl", &td->droop_ctrl);
  1329. ok &= read_dt_param(td, "nvidia,sample-rate", &td->sample_rate);
  1330. ok &= read_dt_param(td, "nvidia,force-mode", &td->force_mode);
  1331. ok &= read_dt_param(td, "nvidia,cf", &td->cf);
  1332. ok &= read_dt_param(td, "nvidia,ci", &td->ci);
  1333. ok &= read_dt_param(td, "nvidia,cg", &td->cg);
  1334. td->cg_scale = of_property_read_bool(td->dev->of_node,
  1335. "nvidia,cg-scale");
  1336. if (of_property_read_string(td->dev->of_node, "clock-output-names",
  1337. &td->output_clock_name)) {
  1338. dev_err(td->dev, "missing clock-output-names property\n");
  1339. ok = false;
  1340. }
  1341. return ok ? 0 : -EINVAL;
  1342. }
  1343. /*
  1344. * API exported to per-SoC platform drivers
  1345. */
  1346. /**
  1347. * tegra_dfll_register - probe a Tegra DFLL device
  1348. * @pdev: DFLL platform_device *
  1349. * @soc: Per-SoC integration and characterization data for this DFLL instance
  1350. *
  1351. * Probe and initialize a DFLL device instance. Intended to be called
  1352. * by a SoC-specific shim driver that passes in per-SoC integration
  1353. * and configuration data via @soc. Returns 0 on success or -err on failure.
  1354. */
  1355. int tegra_dfll_register(struct platform_device *pdev,
  1356. struct tegra_dfll_soc_data *soc)
  1357. {
  1358. struct resource *mem;
  1359. struct tegra_dfll *td;
  1360. int ret;
  1361. if (!soc) {
  1362. dev_err(&pdev->dev, "no tegra_dfll_soc_data provided\n");
  1363. return -EINVAL;
  1364. }
  1365. td = devm_kzalloc(&pdev->dev, sizeof(*td), GFP_KERNEL);
  1366. if (!td)
  1367. return -ENOMEM;
  1368. td->dev = &pdev->dev;
  1369. platform_set_drvdata(pdev, td);
  1370. td->soc = soc;
  1371. td->vdd_reg = devm_regulator_get(td->dev, "vdd-cpu");
  1372. if (IS_ERR(td->vdd_reg)) {
  1373. dev_err(td->dev, "couldn't get vdd_cpu regulator\n");
  1374. return PTR_ERR(td->vdd_reg);
  1375. }
  1376. td->dvco_rst = devm_reset_control_get(td->dev, "dvco");
  1377. if (IS_ERR(td->dvco_rst)) {
  1378. dev_err(td->dev, "couldn't get dvco reset\n");
  1379. return PTR_ERR(td->dvco_rst);
  1380. }
  1381. ret = dfll_fetch_common_params(td);
  1382. if (ret) {
  1383. dev_err(td->dev, "couldn't parse device tree parameters\n");
  1384. return ret;
  1385. }
  1386. ret = dfll_fetch_i2c_params(td);
  1387. if (ret)
  1388. return ret;
  1389. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1390. if (!mem) {
  1391. dev_err(td->dev, "no control register resource\n");
  1392. return -ENODEV;
  1393. }
  1394. td->base = devm_ioremap(td->dev, mem->start, resource_size(mem));
  1395. if (!td->base) {
  1396. dev_err(td->dev, "couldn't ioremap DFLL control registers\n");
  1397. return -ENODEV;
  1398. }
  1399. mem = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1400. if (!mem) {
  1401. dev_err(td->dev, "no i2c_base resource\n");
  1402. return -ENODEV;
  1403. }
  1404. td->i2c_base = devm_ioremap(td->dev, mem->start, resource_size(mem));
  1405. if (!td->i2c_base) {
  1406. dev_err(td->dev, "couldn't ioremap i2c_base resource\n");
  1407. return -ENODEV;
  1408. }
  1409. mem = platform_get_resource(pdev, IORESOURCE_MEM, 2);
  1410. if (!mem) {
  1411. dev_err(td->dev, "no i2c_controller_base resource\n");
  1412. return -ENODEV;
  1413. }
  1414. td->i2c_controller_base = devm_ioremap(td->dev, mem->start,
  1415. resource_size(mem));
  1416. if (!td->i2c_controller_base) {
  1417. dev_err(td->dev,
  1418. "couldn't ioremap i2c_controller_base resource\n");
  1419. return -ENODEV;
  1420. }
  1421. mem = platform_get_resource(pdev, IORESOURCE_MEM, 3);
  1422. if (!mem) {
  1423. dev_err(td->dev, "no lut_base resource\n");
  1424. return -ENODEV;
  1425. }
  1426. td->lut_base = devm_ioremap(td->dev, mem->start, resource_size(mem));
  1427. if (!td->lut_base) {
  1428. dev_err(td->dev,
  1429. "couldn't ioremap lut_base resource\n");
  1430. return -ENODEV;
  1431. }
  1432. ret = dfll_init_clks(td);
  1433. if (ret) {
  1434. dev_err(&pdev->dev, "DFLL clock init error\n");
  1435. return ret;
  1436. }
  1437. /* Enable the clocks and set the device up */
  1438. ret = dfll_init(td);
  1439. if (ret)
  1440. return ret;
  1441. ret = dfll_register_clk(td);
  1442. if (ret) {
  1443. dev_err(&pdev->dev, "DFLL clk registration failed\n");
  1444. return ret;
  1445. }
  1446. #ifdef CONFIG_DEBUG_FS
  1447. dfll_debug_init(td);
  1448. #endif
  1449. return 0;
  1450. }
  1451. EXPORT_SYMBOL(tegra_dfll_register);
  1452. /**
  1453. * tegra_dfll_unregister - release all of the DFLL driver resources for a device
  1454. * @pdev: DFLL platform_device *
  1455. *
  1456. * Unbind this driver from the DFLL hardware device represented by
  1457. * @pdev. The DFLL must be disabled for this to succeed. Returns 0
  1458. * upon success or -EBUSY if the DFLL is still active.
  1459. */
  1460. int tegra_dfll_unregister(struct platform_device *pdev)
  1461. {
  1462. struct tegra_dfll *td = platform_get_drvdata(pdev);
  1463. /* Try to prevent removal while the DFLL is active */
  1464. if (td->mode != DFLL_DISABLED) {
  1465. dev_err(&pdev->dev,
  1466. "must disable DFLL before removing driver\n");
  1467. return -EBUSY;
  1468. }
  1469. debugfs_remove_recursive(td->debugfs_dir);
  1470. dfll_unregister_clk(td);
  1471. pm_runtime_disable(&pdev->dev);
  1472. clk_unprepare(td->ref_clk);
  1473. clk_unprepare(td->soc_clk);
  1474. clk_unprepare(td->i2c_clk);
  1475. reset_control_assert(td->dvco_rst);
  1476. return 0;
  1477. }
  1478. EXPORT_SYMBOL(tegra_dfll_unregister);