clk-dfll.c 46 KB

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