smartreflex.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  1. /*
  2. * OMAP SmartReflex Voltage Control
  3. *
  4. * Author: Thara Gopinath <thara@ti.com>
  5. *
  6. * Copyright (C) 2010 Texas Instruments, Inc.
  7. * Thara Gopinath <thara@ti.com>
  8. *
  9. * Copyright (C) 2008 Nokia Corporation
  10. * Kalle Jokiniemi
  11. *
  12. * Copyright (C) 2007 Texas Instruments, Inc.
  13. * Lesly A M <x0080970@ti.com>
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License version 2 as
  17. * published by the Free Software Foundation.
  18. */
  19. #include <linux/interrupt.h>
  20. #include <linux/clk.h>
  21. #include <linux/io.h>
  22. #include <linux/debugfs.h>
  23. #include <linux/delay.h>
  24. #include <linux/slab.h>
  25. #include <linux/pm_runtime.h>
  26. #include <plat/common.h>
  27. #include <plat/smartreflex.h>
  28. #include "pm.h"
  29. #define SMARTREFLEX_NAME_LEN 16
  30. #define NVALUE_NAME_LEN 40
  31. #define SR_DISABLE_TIMEOUT 200
  32. struct omap_sr {
  33. int srid;
  34. int ip_type;
  35. int nvalue_count;
  36. bool autocomp_active;
  37. u32 clk_length;
  38. u32 err_weight;
  39. u32 err_minlimit;
  40. u32 err_maxlimit;
  41. u32 accum_data;
  42. u32 senn_avgweight;
  43. u32 senp_avgweight;
  44. u32 senp_mod;
  45. u32 senn_mod;
  46. unsigned int irq;
  47. void __iomem *base;
  48. struct platform_device *pdev;
  49. struct list_head node;
  50. struct omap_sr_nvalue_table *nvalue_table;
  51. struct voltagedomain *voltdm;
  52. };
  53. /* sr_list contains all the instances of smartreflex module */
  54. static LIST_HEAD(sr_list);
  55. static struct omap_sr_class_data *sr_class;
  56. static struct omap_sr_pmic_data *sr_pmic_data;
  57. static inline void sr_write_reg(struct omap_sr *sr, unsigned offset, u32 value)
  58. {
  59. __raw_writel(value, (sr->base + offset));
  60. }
  61. static inline void sr_modify_reg(struct omap_sr *sr, unsigned offset, u32 mask,
  62. u32 value)
  63. {
  64. u32 reg_val;
  65. u32 errconfig_offs = 0, errconfig_mask = 0;
  66. reg_val = __raw_readl(sr->base + offset);
  67. reg_val &= ~mask;
  68. /*
  69. * Smartreflex error config register is special as it contains
  70. * certain status bits which if written a 1 into means a clear
  71. * of those bits. So in order to make sure no accidental write of
  72. * 1 happens to those status bits, do a clear of them in the read
  73. * value. This mean this API doesn't rewrite values in these bits
  74. * if they are currently set, but does allow the caller to write
  75. * those bits.
  76. */
  77. if (sr->ip_type == SR_TYPE_V1) {
  78. errconfig_offs = ERRCONFIG_V1;
  79. errconfig_mask = ERRCONFIG_STATUS_V1_MASK;
  80. } else if (sr->ip_type == SR_TYPE_V2) {
  81. errconfig_offs = ERRCONFIG_V2;
  82. errconfig_mask = ERRCONFIG_VPBOUNDINTST_V2;
  83. }
  84. if (offset == errconfig_offs)
  85. reg_val &= ~errconfig_mask;
  86. reg_val |= value;
  87. __raw_writel(reg_val, (sr->base + offset));
  88. }
  89. static inline u32 sr_read_reg(struct omap_sr *sr, unsigned offset)
  90. {
  91. return __raw_readl(sr->base + offset);
  92. }
  93. static struct omap_sr *_sr_lookup(struct voltagedomain *voltdm)
  94. {
  95. struct omap_sr *sr_info;
  96. if (!voltdm) {
  97. pr_err("%s: Null voltage domain passed!\n", __func__);
  98. return ERR_PTR(-EINVAL);
  99. }
  100. list_for_each_entry(sr_info, &sr_list, node) {
  101. if (voltdm == sr_info->voltdm)
  102. return sr_info;
  103. }
  104. return ERR_PTR(-ENODATA);
  105. }
  106. static irqreturn_t sr_interrupt(int irq, void *data)
  107. {
  108. struct omap_sr *sr_info = (struct omap_sr *)data;
  109. u32 status = 0;
  110. if (sr_info->ip_type == SR_TYPE_V1) {
  111. /* Read the status bits */
  112. status = sr_read_reg(sr_info, ERRCONFIG_V1);
  113. /* Clear them by writing back */
  114. sr_write_reg(sr_info, ERRCONFIG_V1, status);
  115. } else if (sr_info->ip_type == SR_TYPE_V2) {
  116. /* Read the status bits */
  117. sr_read_reg(sr_info, IRQSTATUS);
  118. /* Clear them by writing back */
  119. sr_write_reg(sr_info, IRQSTATUS, status);
  120. }
  121. if (sr_class->class_type == SR_CLASS2 && sr_class->notify)
  122. sr_class->notify(sr_info->voltdm, status);
  123. return IRQ_HANDLED;
  124. }
  125. static void sr_set_clk_length(struct omap_sr *sr)
  126. {
  127. struct clk *sys_ck;
  128. u32 sys_clk_speed;
  129. if (cpu_is_omap34xx())
  130. sys_ck = clk_get(NULL, "sys_ck");
  131. else
  132. sys_ck = clk_get(NULL, "sys_clkin_ck");
  133. if (IS_ERR(sys_ck)) {
  134. dev_err(&sr->pdev->dev, "%s: unable to get sys clk\n",
  135. __func__);
  136. return;
  137. }
  138. sys_clk_speed = clk_get_rate(sys_ck);
  139. clk_put(sys_ck);
  140. switch (sys_clk_speed) {
  141. case 12000000:
  142. sr->clk_length = SRCLKLENGTH_12MHZ_SYSCLK;
  143. break;
  144. case 13000000:
  145. sr->clk_length = SRCLKLENGTH_13MHZ_SYSCLK;
  146. break;
  147. case 19200000:
  148. sr->clk_length = SRCLKLENGTH_19MHZ_SYSCLK;
  149. break;
  150. case 26000000:
  151. sr->clk_length = SRCLKLENGTH_26MHZ_SYSCLK;
  152. break;
  153. case 38400000:
  154. sr->clk_length = SRCLKLENGTH_38MHZ_SYSCLK;
  155. break;
  156. default:
  157. dev_err(&sr->pdev->dev, "%s: Invalid sysclk value: %d\n",
  158. __func__, sys_clk_speed);
  159. break;
  160. }
  161. }
  162. static void sr_set_regfields(struct omap_sr *sr)
  163. {
  164. /*
  165. * For time being these values are defined in smartreflex.h
  166. * and populated during init. May be they can be moved to board
  167. * file or pmic specific data structure. In that case these structure
  168. * fields will have to be populated using the pdata or pmic structure.
  169. */
  170. if (cpu_is_omap34xx() || cpu_is_omap44xx()) {
  171. sr->err_weight = OMAP3430_SR_ERRWEIGHT;
  172. sr->err_maxlimit = OMAP3430_SR_ERRMAXLIMIT;
  173. sr->accum_data = OMAP3430_SR_ACCUMDATA;
  174. if (!(strcmp(sr->voltdm->name, "mpu"))) {
  175. sr->senn_avgweight = OMAP3430_SR1_SENNAVGWEIGHT;
  176. sr->senp_avgweight = OMAP3430_SR1_SENPAVGWEIGHT;
  177. } else {
  178. sr->senn_avgweight = OMAP3430_SR2_SENNAVGWEIGHT;
  179. sr->senp_avgweight = OMAP3430_SR2_SENPAVGWEIGHT;
  180. }
  181. }
  182. }
  183. static void sr_start_vddautocomp(struct omap_sr *sr)
  184. {
  185. if (!sr_class || !(sr_class->enable) || !(sr_class->configure)) {
  186. dev_warn(&sr->pdev->dev,
  187. "%s: smartreflex class driver not registered\n",
  188. __func__);
  189. return;
  190. }
  191. if (!sr_class->enable(sr->voltdm))
  192. sr->autocomp_active = true;
  193. }
  194. static void sr_stop_vddautocomp(struct omap_sr *sr)
  195. {
  196. if (!sr_class || !(sr_class->disable)) {
  197. dev_warn(&sr->pdev->dev,
  198. "%s: smartreflex class driver not registered\n",
  199. __func__);
  200. return;
  201. }
  202. if (sr->autocomp_active) {
  203. sr_class->disable(sr->voltdm, 1);
  204. sr->autocomp_active = false;
  205. }
  206. }
  207. /*
  208. * This function handles the intializations which have to be done
  209. * only when both sr device and class driver regiter has
  210. * completed. This will be attempted to be called from both sr class
  211. * driver register and sr device intializtion API's. Only one call
  212. * will ultimately succeed.
  213. *
  214. * Currenly this function registers interrrupt handler for a particular SR
  215. * if smartreflex class driver is already registered and has
  216. * requested for interrupts and the SR interrupt line in present.
  217. */
  218. static int sr_late_init(struct omap_sr *sr_info)
  219. {
  220. char *name;
  221. struct omap_sr_data *pdata = sr_info->pdev->dev.platform_data;
  222. struct resource *mem;
  223. int ret = 0;
  224. if (sr_class->class_type == SR_CLASS2 &&
  225. sr_class->notify_flags && sr_info->irq) {
  226. name = kzalloc(SMARTREFLEX_NAME_LEN + 1, GFP_KERNEL);
  227. strcpy(name, "sr_");
  228. strcat(name, sr_info->voltdm->name);
  229. ret = request_irq(sr_info->irq, sr_interrupt,
  230. 0, name, (void *)sr_info);
  231. if (ret)
  232. goto error;
  233. }
  234. if (pdata && pdata->enable_on_init)
  235. sr_start_vddautocomp(sr_info);
  236. return ret;
  237. error:
  238. iounmap(sr_info->base);
  239. mem = platform_get_resource(sr_info->pdev, IORESOURCE_MEM, 0);
  240. release_mem_region(mem->start, resource_size(mem));
  241. list_del(&sr_info->node);
  242. dev_err(&sr_info->pdev->dev, "%s: ERROR in registering"
  243. "interrupt handler. Smartreflex will"
  244. "not function as desired\n", __func__);
  245. kfree(name);
  246. kfree(sr_info);
  247. return ret;
  248. }
  249. static void sr_v1_disable(struct omap_sr *sr)
  250. {
  251. int timeout = 0;
  252. /* Enable MCUDisableAcknowledge interrupt */
  253. sr_modify_reg(sr, ERRCONFIG_V1,
  254. ERRCONFIG_MCUDISACKINTEN, ERRCONFIG_MCUDISACKINTEN);
  255. /* SRCONFIG - disable SR */
  256. sr_modify_reg(sr, SRCONFIG, SRCONFIG_SRENABLE, 0x0);
  257. /* Disable all other SR interrupts and clear the status */
  258. sr_modify_reg(sr, ERRCONFIG_V1,
  259. (ERRCONFIG_MCUACCUMINTEN | ERRCONFIG_MCUVALIDINTEN |
  260. ERRCONFIG_MCUBOUNDINTEN | ERRCONFIG_VPBOUNDINTEN_V1),
  261. (ERRCONFIG_MCUACCUMINTST | ERRCONFIG_MCUVALIDINTST |
  262. ERRCONFIG_MCUBOUNDINTST |
  263. ERRCONFIG_VPBOUNDINTST_V1));
  264. /*
  265. * Wait for SR to be disabled.
  266. * wait until ERRCONFIG.MCUDISACKINTST = 1. Typical latency is 1us.
  267. */
  268. omap_test_timeout((sr_read_reg(sr, ERRCONFIG_V1) &
  269. ERRCONFIG_MCUDISACKINTST), SR_DISABLE_TIMEOUT,
  270. timeout);
  271. if (timeout >= SR_DISABLE_TIMEOUT)
  272. dev_warn(&sr->pdev->dev, "%s: Smartreflex disable timedout\n",
  273. __func__);
  274. /* Disable MCUDisableAcknowledge interrupt & clear pending interrupt */
  275. sr_modify_reg(sr, ERRCONFIG_V1, ERRCONFIG_MCUDISACKINTEN,
  276. ERRCONFIG_MCUDISACKINTST);
  277. }
  278. static void sr_v2_disable(struct omap_sr *sr)
  279. {
  280. int timeout = 0;
  281. /* Enable MCUDisableAcknowledge interrupt */
  282. sr_write_reg(sr, IRQENABLE_SET, IRQENABLE_MCUDISABLEACKINT);
  283. /* SRCONFIG - disable SR */
  284. sr_modify_reg(sr, SRCONFIG, SRCONFIG_SRENABLE, 0x0);
  285. /* Disable all other SR interrupts and clear the status */
  286. sr_modify_reg(sr, ERRCONFIG_V2, ERRCONFIG_VPBOUNDINTEN_V2,
  287. ERRCONFIG_VPBOUNDINTST_V2);
  288. sr_write_reg(sr, IRQENABLE_CLR, (IRQENABLE_MCUACCUMINT |
  289. IRQENABLE_MCUVALIDINT |
  290. IRQENABLE_MCUBOUNDSINT));
  291. sr_write_reg(sr, IRQSTATUS, (IRQSTATUS_MCUACCUMINT |
  292. IRQSTATUS_MCVALIDINT |
  293. IRQSTATUS_MCBOUNDSINT));
  294. /*
  295. * Wait for SR to be disabled.
  296. * wait until IRQSTATUS.MCUDISACKINTST = 1. Typical latency is 1us.
  297. */
  298. omap_test_timeout((sr_read_reg(sr, IRQSTATUS) &
  299. IRQSTATUS_MCUDISABLEACKINT), SR_DISABLE_TIMEOUT,
  300. timeout);
  301. if (timeout >= SR_DISABLE_TIMEOUT)
  302. dev_warn(&sr->pdev->dev, "%s: Smartreflex disable timedout\n",
  303. __func__);
  304. /* Disable MCUDisableAcknowledge interrupt & clear pending interrupt */
  305. sr_write_reg(sr, IRQENABLE_CLR, IRQENABLE_MCUDISABLEACKINT);
  306. sr_write_reg(sr, IRQSTATUS, IRQSTATUS_MCUDISABLEACKINT);
  307. }
  308. static u32 sr_retrieve_nvalue(struct omap_sr *sr, u32 efuse_offs)
  309. {
  310. int i;
  311. if (!sr->nvalue_table) {
  312. dev_warn(&sr->pdev->dev, "%s: Missing ntarget value table\n",
  313. __func__);
  314. return 0;
  315. }
  316. for (i = 0; i < sr->nvalue_count; i++) {
  317. if (sr->nvalue_table[i].efuse_offs == efuse_offs)
  318. return sr->nvalue_table[i].nvalue;
  319. }
  320. return 0;
  321. }
  322. /* Public Functions */
  323. /**
  324. * sr_configure_errgen() - Configures the smrtreflex to perform AVS using the
  325. * error generator module.
  326. * @voltdm: VDD pointer to which the SR module to be configured belongs to.
  327. *
  328. * This API is to be called from the smartreflex class driver to
  329. * configure the error generator module inside the smartreflex module.
  330. * SR settings if using the ERROR module inside Smartreflex.
  331. * SR CLASS 3 by default uses only the ERROR module where as
  332. * SR CLASS 2 can choose between ERROR module and MINMAXAVG
  333. * module. Returns 0 on success and error value in case of failure.
  334. */
  335. int sr_configure_errgen(struct voltagedomain *voltdm)
  336. {
  337. u32 sr_config, sr_errconfig, errconfig_offs, vpboundint_en;
  338. u32 vpboundint_st, senp_en = 0, senn_en = 0;
  339. u8 senp_shift, senn_shift;
  340. struct omap_sr *sr = _sr_lookup(voltdm);
  341. if (IS_ERR(sr)) {
  342. pr_warning("%s: omap_sr struct for sr_%s not found\n",
  343. __func__, voltdm->name);
  344. return -EINVAL;
  345. }
  346. if (!sr->clk_length)
  347. sr_set_clk_length(sr);
  348. senp_en = sr->senp_mod;
  349. senn_en = sr->senn_mod;
  350. sr_config = (sr->clk_length << SRCONFIG_SRCLKLENGTH_SHIFT) |
  351. SRCONFIG_SENENABLE | SRCONFIG_ERRGEN_EN;
  352. if (sr->ip_type == SR_TYPE_V1) {
  353. sr_config |= SRCONFIG_DELAYCTRL;
  354. senn_shift = SRCONFIG_SENNENABLE_V1_SHIFT;
  355. senp_shift = SRCONFIG_SENPENABLE_V1_SHIFT;
  356. errconfig_offs = ERRCONFIG_V1;
  357. vpboundint_en = ERRCONFIG_VPBOUNDINTEN_V1;
  358. vpboundint_st = ERRCONFIG_VPBOUNDINTST_V1;
  359. } else if (sr->ip_type == SR_TYPE_V2) {
  360. senn_shift = SRCONFIG_SENNENABLE_V2_SHIFT;
  361. senp_shift = SRCONFIG_SENPENABLE_V2_SHIFT;
  362. errconfig_offs = ERRCONFIG_V2;
  363. vpboundint_en = ERRCONFIG_VPBOUNDINTEN_V2;
  364. vpboundint_st = ERRCONFIG_VPBOUNDINTST_V2;
  365. } else {
  366. dev_err(&sr->pdev->dev, "%s: Trying to Configure smartreflex"
  367. "module without specifying the ip\n", __func__);
  368. return -EINVAL;
  369. }
  370. sr_config |= ((senn_en << senn_shift) | (senp_en << senp_shift));
  371. sr_write_reg(sr, SRCONFIG, sr_config);
  372. sr_errconfig = (sr->err_weight << ERRCONFIG_ERRWEIGHT_SHIFT) |
  373. (sr->err_maxlimit << ERRCONFIG_ERRMAXLIMIT_SHIFT) |
  374. (sr->err_minlimit << ERRCONFIG_ERRMINLIMIT_SHIFT);
  375. sr_modify_reg(sr, errconfig_offs, (SR_ERRWEIGHT_MASK |
  376. SR_ERRMAXLIMIT_MASK | SR_ERRMINLIMIT_MASK),
  377. sr_errconfig);
  378. /* Enabling the interrupts if the ERROR module is used */
  379. sr_modify_reg(sr, errconfig_offs,
  380. vpboundint_en, (vpboundint_en | vpboundint_st));
  381. return 0;
  382. }
  383. /**
  384. * sr_configure_minmax() - Configures the smrtreflex to perform AVS using the
  385. * minmaxavg module.
  386. * @voltdm: VDD pointer to which the SR module to be configured belongs to.
  387. *
  388. * This API is to be called from the smartreflex class driver to
  389. * configure the minmaxavg module inside the smartreflex module.
  390. * SR settings if using the ERROR module inside Smartreflex.
  391. * SR CLASS 3 by default uses only the ERROR module where as
  392. * SR CLASS 2 can choose between ERROR module and MINMAXAVG
  393. * module. Returns 0 on success and error value in case of failure.
  394. */
  395. int sr_configure_minmax(struct voltagedomain *voltdm)
  396. {
  397. u32 sr_config, sr_avgwt;
  398. u32 senp_en = 0, senn_en = 0;
  399. u8 senp_shift, senn_shift;
  400. struct omap_sr *sr = _sr_lookup(voltdm);
  401. if (IS_ERR(sr)) {
  402. pr_warning("%s: omap_sr struct for sr_%s not found\n",
  403. __func__, voltdm->name);
  404. return -EINVAL;
  405. }
  406. if (!sr->clk_length)
  407. sr_set_clk_length(sr);
  408. senp_en = sr->senp_mod;
  409. senn_en = sr->senn_mod;
  410. sr_config = (sr->clk_length << SRCONFIG_SRCLKLENGTH_SHIFT) |
  411. SRCONFIG_SENENABLE |
  412. (sr->accum_data << SRCONFIG_ACCUMDATA_SHIFT);
  413. if (sr->ip_type == SR_TYPE_V1) {
  414. sr_config |= SRCONFIG_DELAYCTRL;
  415. senn_shift = SRCONFIG_SENNENABLE_V1_SHIFT;
  416. senp_shift = SRCONFIG_SENPENABLE_V1_SHIFT;
  417. } else if (sr->ip_type == SR_TYPE_V2) {
  418. senn_shift = SRCONFIG_SENNENABLE_V2_SHIFT;
  419. senp_shift = SRCONFIG_SENPENABLE_V2_SHIFT;
  420. } else {
  421. dev_err(&sr->pdev->dev, "%s: Trying to Configure smartreflex"
  422. "module without specifying the ip\n", __func__);
  423. return -EINVAL;
  424. }
  425. sr_config |= ((senn_en << senn_shift) | (senp_en << senp_shift));
  426. sr_write_reg(sr, SRCONFIG, sr_config);
  427. sr_avgwt = (sr->senp_avgweight << AVGWEIGHT_SENPAVGWEIGHT_SHIFT) |
  428. (sr->senn_avgweight << AVGWEIGHT_SENNAVGWEIGHT_SHIFT);
  429. sr_write_reg(sr, AVGWEIGHT, sr_avgwt);
  430. /*
  431. * Enabling the interrupts if MINMAXAVG module is used.
  432. * TODO: check if all the interrupts are mandatory
  433. */
  434. if (sr->ip_type == SR_TYPE_V1) {
  435. sr_modify_reg(sr, ERRCONFIG_V1,
  436. (ERRCONFIG_MCUACCUMINTEN | ERRCONFIG_MCUVALIDINTEN |
  437. ERRCONFIG_MCUBOUNDINTEN),
  438. (ERRCONFIG_MCUACCUMINTEN | ERRCONFIG_MCUACCUMINTST |
  439. ERRCONFIG_MCUVALIDINTEN | ERRCONFIG_MCUVALIDINTST |
  440. ERRCONFIG_MCUBOUNDINTEN | ERRCONFIG_MCUBOUNDINTST));
  441. } else if (sr->ip_type == SR_TYPE_V2) {
  442. sr_write_reg(sr, IRQSTATUS,
  443. IRQSTATUS_MCUACCUMINT | IRQSTATUS_MCVALIDINT |
  444. IRQSTATUS_MCBOUNDSINT | IRQSTATUS_MCUDISABLEACKINT);
  445. sr_write_reg(sr, IRQENABLE_SET,
  446. IRQENABLE_MCUACCUMINT | IRQENABLE_MCUVALIDINT |
  447. IRQENABLE_MCUBOUNDSINT | IRQENABLE_MCUDISABLEACKINT);
  448. }
  449. return 0;
  450. }
  451. /**
  452. * sr_enable() - Enables the smartreflex module.
  453. * @voltdm: VDD pointer to which the SR module to be configured belongs to.
  454. * @volt: The voltage at which the Voltage domain associated with
  455. * the smartreflex module is operating at.
  456. * This is required only to program the correct Ntarget value.
  457. *
  458. * This API is to be called from the smartreflex class driver to
  459. * enable a smartreflex module. Returns 0 on success. Returns error
  460. * value if the voltage passed is wrong or if ntarget value is wrong.
  461. */
  462. int sr_enable(struct voltagedomain *voltdm, unsigned long volt)
  463. {
  464. u32 nvalue_reciprocal;
  465. struct omap_volt_data *volt_data;
  466. struct omap_sr *sr = _sr_lookup(voltdm);
  467. int ret;
  468. if (IS_ERR(sr)) {
  469. pr_warning("%s: omap_sr struct for sr_%s not found\n",
  470. __func__, voltdm->name);
  471. return -EINVAL;
  472. }
  473. volt_data = omap_voltage_get_voltdata(sr->voltdm, volt);
  474. if (IS_ERR(volt_data)) {
  475. dev_warn(&sr->pdev->dev, "%s: Unable to get voltage table"
  476. "for nominal voltage %ld\n", __func__, volt);
  477. return -ENODATA;
  478. }
  479. nvalue_reciprocal = sr_retrieve_nvalue(sr, volt_data->sr_efuse_offs);
  480. if (!nvalue_reciprocal) {
  481. dev_warn(&sr->pdev->dev, "%s: NVALUE = 0 at voltage %ld\n",
  482. __func__, volt);
  483. return -ENODATA;
  484. }
  485. /* errminlimit is opp dependent and hence linked to voltage */
  486. sr->err_minlimit = volt_data->sr_errminlimit;
  487. pm_runtime_get_sync(&sr->pdev->dev);
  488. /* Check if SR is already enabled. If yes do nothing */
  489. if (sr_read_reg(sr, SRCONFIG) & SRCONFIG_SRENABLE)
  490. return 0;
  491. /* Configure SR */
  492. ret = sr_class->configure(voltdm);
  493. if (ret)
  494. return ret;
  495. sr_write_reg(sr, NVALUERECIPROCAL, nvalue_reciprocal);
  496. /* SRCONFIG - enable SR */
  497. sr_modify_reg(sr, SRCONFIG, SRCONFIG_SRENABLE, SRCONFIG_SRENABLE);
  498. return 0;
  499. }
  500. /**
  501. * sr_disable() - Disables the smartreflex module.
  502. * @voltdm: VDD pointer to which the SR module to be configured belongs to.
  503. *
  504. * This API is to be called from the smartreflex class driver to
  505. * disable a smartreflex module.
  506. */
  507. void sr_disable(struct voltagedomain *voltdm)
  508. {
  509. struct omap_sr *sr = _sr_lookup(voltdm);
  510. if (IS_ERR(sr)) {
  511. pr_warning("%s: omap_sr struct for sr_%s not found\n",
  512. __func__, voltdm->name);
  513. return;
  514. }
  515. /* Check if SR clocks are already disabled. If yes do nothing */
  516. if (pm_runtime_suspended(&sr->pdev->dev))
  517. return;
  518. /*
  519. * Disable SR if only it is indeed enabled. Else just
  520. * disable the clocks.
  521. */
  522. if (sr_read_reg(sr, SRCONFIG) & SRCONFIG_SRENABLE) {
  523. if (sr->ip_type == SR_TYPE_V1)
  524. sr_v1_disable(sr);
  525. else if (sr->ip_type == SR_TYPE_V2)
  526. sr_v2_disable(sr);
  527. }
  528. pm_runtime_put_sync(&sr->pdev->dev);
  529. }
  530. /**
  531. * sr_register_class() - API to register a smartreflex class parameters.
  532. * @class_data: The structure containing various sr class specific data.
  533. *
  534. * This API is to be called by the smartreflex class driver to register itself
  535. * with the smartreflex driver during init. Returns 0 on success else the
  536. * error value.
  537. */
  538. int sr_register_class(struct omap_sr_class_data *class_data)
  539. {
  540. struct omap_sr *sr_info;
  541. if (!class_data) {
  542. pr_warning("%s:, Smartreflex class data passed is NULL\n",
  543. __func__);
  544. return -EINVAL;
  545. }
  546. if (sr_class) {
  547. pr_warning("%s: Smartreflex class driver already registered\n",
  548. __func__);
  549. return -EBUSY;
  550. }
  551. sr_class = class_data;
  552. /*
  553. * Call into late init to do intializations that require
  554. * both sr driver and sr class driver to be initiallized.
  555. */
  556. list_for_each_entry(sr_info, &sr_list, node)
  557. sr_late_init(sr_info);
  558. return 0;
  559. }
  560. /**
  561. * omap_sr_enable() - API to enable SR clocks and to call into the
  562. * registered smartreflex class enable API.
  563. * @voltdm: VDD pointer to which the SR module to be configured belongs to.
  564. *
  565. * This API is to be called from the kernel in order to enable
  566. * a particular smartreflex module. This API will do the initial
  567. * configurations to turn on the smartreflex module and in turn call
  568. * into the registered smartreflex class enable API.
  569. */
  570. void omap_sr_enable(struct voltagedomain *voltdm)
  571. {
  572. struct omap_sr *sr = _sr_lookup(voltdm);
  573. if (IS_ERR(sr)) {
  574. pr_warning("%s: omap_sr struct for sr_%s not found\n",
  575. __func__, voltdm->name);
  576. return;
  577. }
  578. if (!sr->autocomp_active)
  579. return;
  580. if (!sr_class || !(sr_class->enable) || !(sr_class->configure)) {
  581. dev_warn(&sr->pdev->dev, "%s: smartreflex class driver not"
  582. "registered\n", __func__);
  583. return;
  584. }
  585. sr_class->enable(voltdm);
  586. }
  587. /**
  588. * omap_sr_disable() - API to disable SR without resetting the voltage
  589. * processor voltage
  590. * @voltdm: VDD pointer to which the SR module to be configured belongs to.
  591. *
  592. * This API is to be called from the kernel in order to disable
  593. * a particular smartreflex module. This API will in turn call
  594. * into the registered smartreflex class disable API. This API will tell
  595. * the smartreflex class disable not to reset the VP voltage after
  596. * disabling smartreflex.
  597. */
  598. void omap_sr_disable(struct voltagedomain *voltdm)
  599. {
  600. struct omap_sr *sr = _sr_lookup(voltdm);
  601. if (IS_ERR(sr)) {
  602. pr_warning("%s: omap_sr struct for sr_%s not found\n",
  603. __func__, voltdm->name);
  604. return;
  605. }
  606. if (!sr->autocomp_active)
  607. return;
  608. if (!sr_class || !(sr_class->disable)) {
  609. dev_warn(&sr->pdev->dev, "%s: smartreflex class driver not"
  610. "registered\n", __func__);
  611. return;
  612. }
  613. sr_class->disable(voltdm, 0);
  614. }
  615. /**
  616. * omap_sr_disable_reset_volt() - API to disable SR and reset the
  617. * voltage processor voltage
  618. * @voltdm: VDD pointer to which the SR module to be configured belongs to.
  619. *
  620. * This API is to be called from the kernel in order to disable
  621. * a particular smartreflex module. This API will in turn call
  622. * into the registered smartreflex class disable API. This API will tell
  623. * the smartreflex class disable to reset the VP voltage after
  624. * disabling smartreflex.
  625. */
  626. void omap_sr_disable_reset_volt(struct voltagedomain *voltdm)
  627. {
  628. struct omap_sr *sr = _sr_lookup(voltdm);
  629. if (IS_ERR(sr)) {
  630. pr_warning("%s: omap_sr struct for sr_%s not found\n",
  631. __func__, voltdm->name);
  632. return;
  633. }
  634. if (!sr->autocomp_active)
  635. return;
  636. if (!sr_class || !(sr_class->disable)) {
  637. dev_warn(&sr->pdev->dev, "%s: smartreflex class driver not"
  638. "registered\n", __func__);
  639. return;
  640. }
  641. sr_class->disable(voltdm, 1);
  642. }
  643. /**
  644. * omap_sr_register_pmic() - API to register pmic specific info.
  645. * @pmic_data: The structure containing pmic specific data.
  646. *
  647. * This API is to be called from the PMIC specific code to register with
  648. * smartreflex driver pmic specific info. Currently the only info required
  649. * is the smartreflex init on the PMIC side.
  650. */
  651. void omap_sr_register_pmic(struct omap_sr_pmic_data *pmic_data)
  652. {
  653. if (!pmic_data) {
  654. pr_warning("%s: Trying to register NULL PMIC data structure"
  655. "with smartreflex\n", __func__);
  656. return;
  657. }
  658. sr_pmic_data = pmic_data;
  659. }
  660. /* PM Debug Fs enteries to enable disable smartreflex. */
  661. static int omap_sr_autocomp_show(void *data, u64 *val)
  662. {
  663. struct omap_sr *sr_info = (struct omap_sr *) data;
  664. if (!sr_info) {
  665. pr_warning("%s: omap_sr struct not found\n", __func__);
  666. return -EINVAL;
  667. }
  668. *val = sr_info->autocomp_active;
  669. return 0;
  670. }
  671. static int omap_sr_autocomp_store(void *data, u64 val)
  672. {
  673. struct omap_sr *sr_info = (struct omap_sr *) data;
  674. if (!sr_info) {
  675. pr_warning("%s: omap_sr struct not found\n", __func__);
  676. return -EINVAL;
  677. }
  678. /* Sanity check */
  679. if (val && (val != 1)) {
  680. pr_warning("%s: Invalid argument %lld\n", __func__, val);
  681. return -EINVAL;
  682. }
  683. if (!val)
  684. sr_stop_vddautocomp(sr_info);
  685. else
  686. sr_start_vddautocomp(sr_info);
  687. return 0;
  688. }
  689. DEFINE_SIMPLE_ATTRIBUTE(pm_sr_fops, omap_sr_autocomp_show,
  690. omap_sr_autocomp_store, "%llu\n");
  691. static int __init omap_sr_probe(struct platform_device *pdev)
  692. {
  693. struct omap_sr *sr_info = kzalloc(sizeof(struct omap_sr), GFP_KERNEL);
  694. struct omap_sr_data *pdata = pdev->dev.platform_data;
  695. struct resource *mem, *irq;
  696. struct dentry *vdd_dbg_dir, *dbg_dir, *nvalue_dir;
  697. struct omap_volt_data *volt_data;
  698. int i, ret = 0;
  699. if (!sr_info) {
  700. dev_err(&pdev->dev, "%s: unable to allocate sr_info\n",
  701. __func__);
  702. return -ENOMEM;
  703. }
  704. if (!pdata) {
  705. dev_err(&pdev->dev, "%s: platform data missing\n", __func__);
  706. ret = -EINVAL;
  707. goto err_free_devinfo;
  708. }
  709. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  710. if (!mem) {
  711. dev_err(&pdev->dev, "%s: no mem resource\n", __func__);
  712. ret = -ENODEV;
  713. goto err_free_devinfo;
  714. }
  715. irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  716. pm_runtime_enable(&pdev->dev);
  717. sr_info->pdev = pdev;
  718. sr_info->srid = pdev->id;
  719. sr_info->voltdm = pdata->voltdm;
  720. sr_info->nvalue_table = pdata->nvalue_table;
  721. sr_info->nvalue_count = pdata->nvalue_count;
  722. sr_info->senn_mod = pdata->senn_mod;
  723. sr_info->senp_mod = pdata->senp_mod;
  724. sr_info->autocomp_active = false;
  725. sr_info->ip_type = pdata->ip_type;
  726. sr_info->base = ioremap(mem->start, resource_size(mem));
  727. if (!sr_info->base) {
  728. dev_err(&pdev->dev, "%s: ioremap fail\n", __func__);
  729. ret = -ENOMEM;
  730. goto err_release_region;
  731. }
  732. if (irq)
  733. sr_info->irq = irq->start;
  734. sr_set_clk_length(sr_info);
  735. sr_set_regfields(sr_info);
  736. list_add(&sr_info->node, &sr_list);
  737. /*
  738. * Call into late init to do intializations that require
  739. * both sr driver and sr class driver to be initiallized.
  740. */
  741. if (sr_class) {
  742. ret = sr_late_init(sr_info);
  743. if (ret) {
  744. pr_warning("%s: Error in SR late init\n", __func__);
  745. goto err_release_region;
  746. }
  747. }
  748. dev_info(&pdev->dev, "%s: SmartReflex driver initialized\n", __func__);
  749. /*
  750. * If the voltage domain debugfs directory is not created, do
  751. * not try to create rest of the debugfs entries.
  752. */
  753. vdd_dbg_dir = omap_voltage_get_dbgdir(sr_info->voltdm);
  754. if (!vdd_dbg_dir) {
  755. ret = -EINVAL;
  756. goto err_release_region;
  757. }
  758. dbg_dir = debugfs_create_dir("smartreflex", vdd_dbg_dir);
  759. if (IS_ERR(dbg_dir)) {
  760. dev_err(&pdev->dev, "%s: Unable to create debugfs directory\n",
  761. __func__);
  762. ret = PTR_ERR(dbg_dir);
  763. goto err_release_region;
  764. }
  765. (void) debugfs_create_file("autocomp", S_IRUGO | S_IWUSR, dbg_dir,
  766. (void *)sr_info, &pm_sr_fops);
  767. (void) debugfs_create_x32("errweight", S_IRUGO, dbg_dir,
  768. &sr_info->err_weight);
  769. (void) debugfs_create_x32("errmaxlimit", S_IRUGO, dbg_dir,
  770. &sr_info->err_maxlimit);
  771. (void) debugfs_create_x32("errminlimit", S_IRUGO, dbg_dir,
  772. &sr_info->err_minlimit);
  773. nvalue_dir = debugfs_create_dir("nvalue", dbg_dir);
  774. if (IS_ERR(nvalue_dir)) {
  775. dev_err(&pdev->dev, "%s: Unable to create debugfs directory"
  776. "for n-values\n", __func__);
  777. ret = PTR_ERR(nvalue_dir);
  778. goto err_release_region;
  779. }
  780. omap_voltage_get_volttable(sr_info->voltdm, &volt_data);
  781. if (!volt_data) {
  782. dev_warn(&pdev->dev, "%s: No Voltage table for the"
  783. " corresponding vdd vdd_%s. Cannot create debugfs"
  784. "entries for n-values\n",
  785. __func__, sr_info->voltdm->name);
  786. ret = -ENODATA;
  787. goto err_release_region;
  788. }
  789. for (i = 0; i < sr_info->nvalue_count; i++) {
  790. char name[NVALUE_NAME_LEN + 1];
  791. snprintf(name, sizeof(name), "volt_%d",
  792. volt_data[i].volt_nominal);
  793. (void) debugfs_create_x32(name, S_IRUGO | S_IWUSR, nvalue_dir,
  794. &(sr_info->nvalue_table[i].nvalue));
  795. }
  796. return ret;
  797. err_release_region:
  798. release_mem_region(mem->start, resource_size(mem));
  799. err_free_devinfo:
  800. kfree(sr_info);
  801. return ret;
  802. }
  803. static int __devexit omap_sr_remove(struct platform_device *pdev)
  804. {
  805. struct omap_sr_data *pdata = pdev->dev.platform_data;
  806. struct omap_sr *sr_info;
  807. struct resource *mem;
  808. if (!pdata) {
  809. dev_err(&pdev->dev, "%s: platform data missing\n", __func__);
  810. return -EINVAL;
  811. }
  812. sr_info = _sr_lookup(pdata->voltdm);
  813. if (IS_ERR(sr_info)) {
  814. dev_warn(&pdev->dev, "%s: omap_sr struct not found\n",
  815. __func__);
  816. return -EINVAL;
  817. }
  818. if (sr_info->autocomp_active)
  819. sr_stop_vddautocomp(sr_info);
  820. list_del(&sr_info->node);
  821. iounmap(sr_info->base);
  822. kfree(sr_info);
  823. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  824. release_mem_region(mem->start, resource_size(mem));
  825. return 0;
  826. }
  827. static struct platform_driver smartreflex_driver = {
  828. .remove = omap_sr_remove,
  829. .driver = {
  830. .name = "smartreflex",
  831. },
  832. };
  833. static int __init sr_init(void)
  834. {
  835. int ret = 0;
  836. /*
  837. * sr_init is a late init. If by then a pmic specific API is not
  838. * registered either there is no need for anything to be done on
  839. * the PMIC side or somebody has forgotten to register a PMIC
  840. * handler. Warn for the second condition.
  841. */
  842. if (sr_pmic_data && sr_pmic_data->sr_pmic_init)
  843. sr_pmic_data->sr_pmic_init();
  844. else
  845. pr_warning("%s: No PMIC hook to init smartreflex\n", __func__);
  846. ret = platform_driver_probe(&smartreflex_driver, omap_sr_probe);
  847. if (ret) {
  848. pr_err("%s: platform driver register failed for SR\n",
  849. __func__);
  850. return ret;
  851. }
  852. return 0;
  853. }
  854. static void __exit sr_exit(void)
  855. {
  856. platform_driver_unregister(&smartreflex_driver);
  857. }
  858. late_initcall(sr_init);
  859. module_exit(sr_exit);
  860. MODULE_DESCRIPTION("OMAP Smartreflex Driver");
  861. MODULE_LICENSE("GPL");
  862. MODULE_ALIAS("platform:" DRIVER_NAME);
  863. MODULE_AUTHOR("Texas Instruments Inc");