smartreflex.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027
  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 = kasprintf(GFP_KERNEL, "sr_%s", sr_info->voltdm->name);
  227. if (name == NULL) {
  228. ret = -ENOMEM;
  229. goto error;
  230. }
  231. ret = request_irq(sr_info->irq, sr_interrupt,
  232. 0, name, (void *)sr_info);
  233. if (ret)
  234. goto error;
  235. }
  236. if (pdata && pdata->enable_on_init)
  237. sr_start_vddautocomp(sr_info);
  238. return ret;
  239. error:
  240. iounmap(sr_info->base);
  241. mem = platform_get_resource(sr_info->pdev, IORESOURCE_MEM, 0);
  242. release_mem_region(mem->start, resource_size(mem));
  243. list_del(&sr_info->node);
  244. dev_err(&sr_info->pdev->dev, "%s: ERROR in registering"
  245. "interrupt handler. Smartreflex will"
  246. "not function as desired\n", __func__);
  247. kfree(name);
  248. kfree(sr_info);
  249. return ret;
  250. }
  251. static void sr_v1_disable(struct omap_sr *sr)
  252. {
  253. int timeout = 0;
  254. /* Enable MCUDisableAcknowledge interrupt */
  255. sr_modify_reg(sr, ERRCONFIG_V1,
  256. ERRCONFIG_MCUDISACKINTEN, ERRCONFIG_MCUDISACKINTEN);
  257. /* SRCONFIG - disable SR */
  258. sr_modify_reg(sr, SRCONFIG, SRCONFIG_SRENABLE, 0x0);
  259. /* Disable all other SR interrupts and clear the status */
  260. sr_modify_reg(sr, ERRCONFIG_V1,
  261. (ERRCONFIG_MCUACCUMINTEN | ERRCONFIG_MCUVALIDINTEN |
  262. ERRCONFIG_MCUBOUNDINTEN | ERRCONFIG_VPBOUNDINTEN_V1),
  263. (ERRCONFIG_MCUACCUMINTST | ERRCONFIG_MCUVALIDINTST |
  264. ERRCONFIG_MCUBOUNDINTST |
  265. ERRCONFIG_VPBOUNDINTST_V1));
  266. /*
  267. * Wait for SR to be disabled.
  268. * wait until ERRCONFIG.MCUDISACKINTST = 1. Typical latency is 1us.
  269. */
  270. omap_test_timeout((sr_read_reg(sr, ERRCONFIG_V1) &
  271. ERRCONFIG_MCUDISACKINTST), SR_DISABLE_TIMEOUT,
  272. timeout);
  273. if (timeout >= SR_DISABLE_TIMEOUT)
  274. dev_warn(&sr->pdev->dev, "%s: Smartreflex disable timedout\n",
  275. __func__);
  276. /* Disable MCUDisableAcknowledge interrupt & clear pending interrupt */
  277. sr_modify_reg(sr, ERRCONFIG_V1, ERRCONFIG_MCUDISACKINTEN,
  278. ERRCONFIG_MCUDISACKINTST);
  279. }
  280. static void sr_v2_disable(struct omap_sr *sr)
  281. {
  282. int timeout = 0;
  283. /* Enable MCUDisableAcknowledge interrupt */
  284. sr_write_reg(sr, IRQENABLE_SET, IRQENABLE_MCUDISABLEACKINT);
  285. /* SRCONFIG - disable SR */
  286. sr_modify_reg(sr, SRCONFIG, SRCONFIG_SRENABLE, 0x0);
  287. /* Disable all other SR interrupts and clear the status */
  288. sr_modify_reg(sr, ERRCONFIG_V2, ERRCONFIG_VPBOUNDINTEN_V2,
  289. ERRCONFIG_VPBOUNDINTST_V2);
  290. sr_write_reg(sr, IRQENABLE_CLR, (IRQENABLE_MCUACCUMINT |
  291. IRQENABLE_MCUVALIDINT |
  292. IRQENABLE_MCUBOUNDSINT));
  293. sr_write_reg(sr, IRQSTATUS, (IRQSTATUS_MCUACCUMINT |
  294. IRQSTATUS_MCVALIDINT |
  295. IRQSTATUS_MCBOUNDSINT));
  296. /*
  297. * Wait for SR to be disabled.
  298. * wait until IRQSTATUS.MCUDISACKINTST = 1. Typical latency is 1us.
  299. */
  300. omap_test_timeout((sr_read_reg(sr, IRQSTATUS) &
  301. IRQSTATUS_MCUDISABLEACKINT), SR_DISABLE_TIMEOUT,
  302. timeout);
  303. if (timeout >= SR_DISABLE_TIMEOUT)
  304. dev_warn(&sr->pdev->dev, "%s: Smartreflex disable timedout\n",
  305. __func__);
  306. /* Disable MCUDisableAcknowledge interrupt & clear pending interrupt */
  307. sr_write_reg(sr, IRQENABLE_CLR, IRQENABLE_MCUDISABLEACKINT);
  308. sr_write_reg(sr, IRQSTATUS, IRQSTATUS_MCUDISABLEACKINT);
  309. }
  310. static u32 sr_retrieve_nvalue(struct omap_sr *sr, u32 efuse_offs)
  311. {
  312. int i;
  313. if (!sr->nvalue_table) {
  314. dev_warn(&sr->pdev->dev, "%s: Missing ntarget value table\n",
  315. __func__);
  316. return 0;
  317. }
  318. for (i = 0; i < sr->nvalue_count; i++) {
  319. if (sr->nvalue_table[i].efuse_offs == efuse_offs)
  320. return sr->nvalue_table[i].nvalue;
  321. }
  322. return 0;
  323. }
  324. /* Public Functions */
  325. /**
  326. * sr_configure_errgen() - Configures the smrtreflex to perform AVS using the
  327. * error generator module.
  328. * @voltdm: VDD pointer to which the SR module to be configured belongs to.
  329. *
  330. * This API is to be called from the smartreflex class driver to
  331. * configure the error generator module inside the smartreflex module.
  332. * SR settings if using the ERROR module inside Smartreflex.
  333. * SR CLASS 3 by default uses only the ERROR module where as
  334. * SR CLASS 2 can choose between ERROR module and MINMAXAVG
  335. * module. Returns 0 on success and error value in case of failure.
  336. */
  337. int sr_configure_errgen(struct voltagedomain *voltdm)
  338. {
  339. u32 sr_config, sr_errconfig, errconfig_offs, vpboundint_en;
  340. u32 vpboundint_st, senp_en = 0, senn_en = 0;
  341. u8 senp_shift, senn_shift;
  342. struct omap_sr *sr = _sr_lookup(voltdm);
  343. if (IS_ERR(sr)) {
  344. pr_warning("%s: omap_sr struct for sr_%s not found\n",
  345. __func__, voltdm->name);
  346. return -EINVAL;
  347. }
  348. if (!sr->clk_length)
  349. sr_set_clk_length(sr);
  350. senp_en = sr->senp_mod;
  351. senn_en = sr->senn_mod;
  352. sr_config = (sr->clk_length << SRCONFIG_SRCLKLENGTH_SHIFT) |
  353. SRCONFIG_SENENABLE | SRCONFIG_ERRGEN_EN;
  354. if (sr->ip_type == SR_TYPE_V1) {
  355. sr_config |= SRCONFIG_DELAYCTRL;
  356. senn_shift = SRCONFIG_SENNENABLE_V1_SHIFT;
  357. senp_shift = SRCONFIG_SENPENABLE_V1_SHIFT;
  358. errconfig_offs = ERRCONFIG_V1;
  359. vpboundint_en = ERRCONFIG_VPBOUNDINTEN_V1;
  360. vpboundint_st = ERRCONFIG_VPBOUNDINTST_V1;
  361. } else if (sr->ip_type == SR_TYPE_V2) {
  362. senn_shift = SRCONFIG_SENNENABLE_V2_SHIFT;
  363. senp_shift = SRCONFIG_SENPENABLE_V2_SHIFT;
  364. errconfig_offs = ERRCONFIG_V2;
  365. vpboundint_en = ERRCONFIG_VPBOUNDINTEN_V2;
  366. vpboundint_st = ERRCONFIG_VPBOUNDINTST_V2;
  367. } else {
  368. dev_err(&sr->pdev->dev, "%s: Trying to Configure smartreflex"
  369. "module without specifying the ip\n", __func__);
  370. return -EINVAL;
  371. }
  372. sr_config |= ((senn_en << senn_shift) | (senp_en << senp_shift));
  373. sr_write_reg(sr, SRCONFIG, sr_config);
  374. sr_errconfig = (sr->err_weight << ERRCONFIG_ERRWEIGHT_SHIFT) |
  375. (sr->err_maxlimit << ERRCONFIG_ERRMAXLIMIT_SHIFT) |
  376. (sr->err_minlimit << ERRCONFIG_ERRMINLIMIT_SHIFT);
  377. sr_modify_reg(sr, errconfig_offs, (SR_ERRWEIGHT_MASK |
  378. SR_ERRMAXLIMIT_MASK | SR_ERRMINLIMIT_MASK),
  379. sr_errconfig);
  380. /* Enabling the interrupts if the ERROR module is used */
  381. sr_modify_reg(sr, errconfig_offs,
  382. vpboundint_en, (vpboundint_en | vpboundint_st));
  383. return 0;
  384. }
  385. /**
  386. * sr_configure_minmax() - Configures the smrtreflex to perform AVS using the
  387. * minmaxavg module.
  388. * @voltdm: VDD pointer to which the SR module to be configured belongs to.
  389. *
  390. * This API is to be called from the smartreflex class driver to
  391. * configure the minmaxavg module inside the smartreflex module.
  392. * SR settings if using the ERROR module inside Smartreflex.
  393. * SR CLASS 3 by default uses only the ERROR module where as
  394. * SR CLASS 2 can choose between ERROR module and MINMAXAVG
  395. * module. Returns 0 on success and error value in case of failure.
  396. */
  397. int sr_configure_minmax(struct voltagedomain *voltdm)
  398. {
  399. u32 sr_config, sr_avgwt;
  400. u32 senp_en = 0, senn_en = 0;
  401. u8 senp_shift, senn_shift;
  402. struct omap_sr *sr = _sr_lookup(voltdm);
  403. if (IS_ERR(sr)) {
  404. pr_warning("%s: omap_sr struct for sr_%s not found\n",
  405. __func__, voltdm->name);
  406. return -EINVAL;
  407. }
  408. if (!sr->clk_length)
  409. sr_set_clk_length(sr);
  410. senp_en = sr->senp_mod;
  411. senn_en = sr->senn_mod;
  412. sr_config = (sr->clk_length << SRCONFIG_SRCLKLENGTH_SHIFT) |
  413. SRCONFIG_SENENABLE |
  414. (sr->accum_data << SRCONFIG_ACCUMDATA_SHIFT);
  415. if (sr->ip_type == SR_TYPE_V1) {
  416. sr_config |= SRCONFIG_DELAYCTRL;
  417. senn_shift = SRCONFIG_SENNENABLE_V1_SHIFT;
  418. senp_shift = SRCONFIG_SENPENABLE_V1_SHIFT;
  419. } else if (sr->ip_type == SR_TYPE_V2) {
  420. senn_shift = SRCONFIG_SENNENABLE_V2_SHIFT;
  421. senp_shift = SRCONFIG_SENPENABLE_V2_SHIFT;
  422. } else {
  423. dev_err(&sr->pdev->dev, "%s: Trying to Configure smartreflex"
  424. "module without specifying the ip\n", __func__);
  425. return -EINVAL;
  426. }
  427. sr_config |= ((senn_en << senn_shift) | (senp_en << senp_shift));
  428. sr_write_reg(sr, SRCONFIG, sr_config);
  429. sr_avgwt = (sr->senp_avgweight << AVGWEIGHT_SENPAVGWEIGHT_SHIFT) |
  430. (sr->senn_avgweight << AVGWEIGHT_SENNAVGWEIGHT_SHIFT);
  431. sr_write_reg(sr, AVGWEIGHT, sr_avgwt);
  432. /*
  433. * Enabling the interrupts if MINMAXAVG module is used.
  434. * TODO: check if all the interrupts are mandatory
  435. */
  436. if (sr->ip_type == SR_TYPE_V1) {
  437. sr_modify_reg(sr, ERRCONFIG_V1,
  438. (ERRCONFIG_MCUACCUMINTEN | ERRCONFIG_MCUVALIDINTEN |
  439. ERRCONFIG_MCUBOUNDINTEN),
  440. (ERRCONFIG_MCUACCUMINTEN | ERRCONFIG_MCUACCUMINTST |
  441. ERRCONFIG_MCUVALIDINTEN | ERRCONFIG_MCUVALIDINTST |
  442. ERRCONFIG_MCUBOUNDINTEN | ERRCONFIG_MCUBOUNDINTST));
  443. } else if (sr->ip_type == SR_TYPE_V2) {
  444. sr_write_reg(sr, IRQSTATUS,
  445. IRQSTATUS_MCUACCUMINT | IRQSTATUS_MCVALIDINT |
  446. IRQSTATUS_MCBOUNDSINT | IRQSTATUS_MCUDISABLEACKINT);
  447. sr_write_reg(sr, IRQENABLE_SET,
  448. IRQENABLE_MCUACCUMINT | IRQENABLE_MCUVALIDINT |
  449. IRQENABLE_MCUBOUNDSINT | IRQENABLE_MCUDISABLEACKINT);
  450. }
  451. return 0;
  452. }
  453. /**
  454. * sr_enable() - Enables the smartreflex module.
  455. * @voltdm: VDD pointer to which the SR module to be configured belongs to.
  456. * @volt: The voltage at which the Voltage domain associated with
  457. * the smartreflex module is operating at.
  458. * This is required only to program the correct Ntarget value.
  459. *
  460. * This API is to be called from the smartreflex class driver to
  461. * enable a smartreflex module. Returns 0 on success. Returns error
  462. * value if the voltage passed is wrong or if ntarget value is wrong.
  463. */
  464. int sr_enable(struct voltagedomain *voltdm, unsigned long volt)
  465. {
  466. u32 nvalue_reciprocal;
  467. struct omap_volt_data *volt_data;
  468. struct omap_sr *sr = _sr_lookup(voltdm);
  469. int ret;
  470. if (IS_ERR(sr)) {
  471. pr_warning("%s: omap_sr struct for sr_%s not found\n",
  472. __func__, voltdm->name);
  473. return -EINVAL;
  474. }
  475. volt_data = omap_voltage_get_voltdata(sr->voltdm, volt);
  476. if (IS_ERR(volt_data)) {
  477. dev_warn(&sr->pdev->dev, "%s: Unable to get voltage table"
  478. "for nominal voltage %ld\n", __func__, volt);
  479. return -ENODATA;
  480. }
  481. nvalue_reciprocal = sr_retrieve_nvalue(sr, volt_data->sr_efuse_offs);
  482. if (!nvalue_reciprocal) {
  483. dev_warn(&sr->pdev->dev, "%s: NVALUE = 0 at voltage %ld\n",
  484. __func__, volt);
  485. return -ENODATA;
  486. }
  487. /* errminlimit is opp dependent and hence linked to voltage */
  488. sr->err_minlimit = volt_data->sr_errminlimit;
  489. pm_runtime_get_sync(&sr->pdev->dev);
  490. /* Check if SR is already enabled. If yes do nothing */
  491. if (sr_read_reg(sr, SRCONFIG) & SRCONFIG_SRENABLE)
  492. return 0;
  493. /* Configure SR */
  494. ret = sr_class->configure(voltdm);
  495. if (ret)
  496. return ret;
  497. sr_write_reg(sr, NVALUERECIPROCAL, nvalue_reciprocal);
  498. /* SRCONFIG - enable SR */
  499. sr_modify_reg(sr, SRCONFIG, SRCONFIG_SRENABLE, SRCONFIG_SRENABLE);
  500. return 0;
  501. }
  502. /**
  503. * sr_disable() - Disables the smartreflex module.
  504. * @voltdm: VDD pointer to which the SR module to be configured belongs to.
  505. *
  506. * This API is to be called from the smartreflex class driver to
  507. * disable a smartreflex module.
  508. */
  509. void sr_disable(struct voltagedomain *voltdm)
  510. {
  511. struct omap_sr *sr = _sr_lookup(voltdm);
  512. if (IS_ERR(sr)) {
  513. pr_warning("%s: omap_sr struct for sr_%s not found\n",
  514. __func__, voltdm->name);
  515. return;
  516. }
  517. /* Check if SR clocks are already disabled. If yes do nothing */
  518. if (pm_runtime_suspended(&sr->pdev->dev))
  519. return;
  520. /*
  521. * Disable SR if only it is indeed enabled. Else just
  522. * disable the clocks.
  523. */
  524. if (sr_read_reg(sr, SRCONFIG) & SRCONFIG_SRENABLE) {
  525. if (sr->ip_type == SR_TYPE_V1)
  526. sr_v1_disable(sr);
  527. else if (sr->ip_type == SR_TYPE_V2)
  528. sr_v2_disable(sr);
  529. }
  530. pm_runtime_put_sync(&sr->pdev->dev);
  531. }
  532. /**
  533. * sr_register_class() - API to register a smartreflex class parameters.
  534. * @class_data: The structure containing various sr class specific data.
  535. *
  536. * This API is to be called by the smartreflex class driver to register itself
  537. * with the smartreflex driver during init. Returns 0 on success else the
  538. * error value.
  539. */
  540. int sr_register_class(struct omap_sr_class_data *class_data)
  541. {
  542. struct omap_sr *sr_info;
  543. if (!class_data) {
  544. pr_warning("%s:, Smartreflex class data passed is NULL\n",
  545. __func__);
  546. return -EINVAL;
  547. }
  548. if (sr_class) {
  549. pr_warning("%s: Smartreflex class driver already registered\n",
  550. __func__);
  551. return -EBUSY;
  552. }
  553. sr_class = class_data;
  554. /*
  555. * Call into late init to do intializations that require
  556. * both sr driver and sr class driver to be initiallized.
  557. */
  558. list_for_each_entry(sr_info, &sr_list, node)
  559. sr_late_init(sr_info);
  560. return 0;
  561. }
  562. /**
  563. * omap_sr_enable() - API to enable SR clocks and to call into the
  564. * registered smartreflex class enable API.
  565. * @voltdm: VDD pointer to which the SR module to be configured belongs to.
  566. *
  567. * This API is to be called from the kernel in order to enable
  568. * a particular smartreflex module. This API will do the initial
  569. * configurations to turn on the smartreflex module and in turn call
  570. * into the registered smartreflex class enable API.
  571. */
  572. void omap_sr_enable(struct voltagedomain *voltdm)
  573. {
  574. struct omap_sr *sr = _sr_lookup(voltdm);
  575. if (IS_ERR(sr)) {
  576. pr_warning("%s: omap_sr struct for sr_%s not found\n",
  577. __func__, voltdm->name);
  578. return;
  579. }
  580. if (!sr->autocomp_active)
  581. return;
  582. if (!sr_class || !(sr_class->enable) || !(sr_class->configure)) {
  583. dev_warn(&sr->pdev->dev, "%s: smartreflex class driver not"
  584. "registered\n", __func__);
  585. return;
  586. }
  587. sr_class->enable(voltdm);
  588. }
  589. /**
  590. * omap_sr_disable() - API to disable SR without resetting the voltage
  591. * processor voltage
  592. * @voltdm: VDD pointer to which the SR module to be configured belongs to.
  593. *
  594. * This API is to be called from the kernel in order to disable
  595. * a particular smartreflex module. This API will in turn call
  596. * into the registered smartreflex class disable API. This API will tell
  597. * the smartreflex class disable not to reset the VP voltage after
  598. * disabling smartreflex.
  599. */
  600. void omap_sr_disable(struct voltagedomain *voltdm)
  601. {
  602. struct omap_sr *sr = _sr_lookup(voltdm);
  603. if (IS_ERR(sr)) {
  604. pr_warning("%s: omap_sr struct for sr_%s not found\n",
  605. __func__, voltdm->name);
  606. return;
  607. }
  608. if (!sr->autocomp_active)
  609. return;
  610. if (!sr_class || !(sr_class->disable)) {
  611. dev_warn(&sr->pdev->dev, "%s: smartreflex class driver not"
  612. "registered\n", __func__);
  613. return;
  614. }
  615. sr_class->disable(voltdm, 0);
  616. }
  617. /**
  618. * omap_sr_disable_reset_volt() - API to disable SR and reset the
  619. * voltage processor voltage
  620. * @voltdm: VDD pointer to which the SR module to be configured belongs to.
  621. *
  622. * This API is to be called from the kernel in order to disable
  623. * a particular smartreflex module. This API will in turn call
  624. * into the registered smartreflex class disable API. This API will tell
  625. * the smartreflex class disable to reset the VP voltage after
  626. * disabling smartreflex.
  627. */
  628. void omap_sr_disable_reset_volt(struct voltagedomain *voltdm)
  629. {
  630. struct omap_sr *sr = _sr_lookup(voltdm);
  631. if (IS_ERR(sr)) {
  632. pr_warning("%s: omap_sr struct for sr_%s not found\n",
  633. __func__, voltdm->name);
  634. return;
  635. }
  636. if (!sr->autocomp_active)
  637. return;
  638. if (!sr_class || !(sr_class->disable)) {
  639. dev_warn(&sr->pdev->dev, "%s: smartreflex class driver not"
  640. "registered\n", __func__);
  641. return;
  642. }
  643. sr_class->disable(voltdm, 1);
  644. }
  645. /**
  646. * omap_sr_register_pmic() - API to register pmic specific info.
  647. * @pmic_data: The structure containing pmic specific data.
  648. *
  649. * This API is to be called from the PMIC specific code to register with
  650. * smartreflex driver pmic specific info. Currently the only info required
  651. * is the smartreflex init on the PMIC side.
  652. */
  653. void omap_sr_register_pmic(struct omap_sr_pmic_data *pmic_data)
  654. {
  655. if (!pmic_data) {
  656. pr_warning("%s: Trying to register NULL PMIC data structure"
  657. "with smartreflex\n", __func__);
  658. return;
  659. }
  660. sr_pmic_data = pmic_data;
  661. }
  662. /* PM Debug Fs enteries to enable disable smartreflex. */
  663. static int omap_sr_autocomp_show(void *data, u64 *val)
  664. {
  665. struct omap_sr *sr_info = (struct omap_sr *) data;
  666. if (!sr_info) {
  667. pr_warning("%s: omap_sr struct not found\n", __func__);
  668. return -EINVAL;
  669. }
  670. *val = sr_info->autocomp_active;
  671. return 0;
  672. }
  673. static int omap_sr_autocomp_store(void *data, u64 val)
  674. {
  675. struct omap_sr *sr_info = (struct omap_sr *) data;
  676. if (!sr_info) {
  677. pr_warning("%s: omap_sr struct not found\n", __func__);
  678. return -EINVAL;
  679. }
  680. /* Sanity check */
  681. if (val && (val != 1)) {
  682. pr_warning("%s: Invalid argument %lld\n", __func__, val);
  683. return -EINVAL;
  684. }
  685. if (!val)
  686. sr_stop_vddautocomp(sr_info);
  687. else
  688. sr_start_vddautocomp(sr_info);
  689. return 0;
  690. }
  691. DEFINE_SIMPLE_ATTRIBUTE(pm_sr_fops, omap_sr_autocomp_show,
  692. omap_sr_autocomp_store, "%llu\n");
  693. static int __init omap_sr_probe(struct platform_device *pdev)
  694. {
  695. struct omap_sr *sr_info = kzalloc(sizeof(struct omap_sr), GFP_KERNEL);
  696. struct omap_sr_data *pdata = pdev->dev.platform_data;
  697. struct resource *mem, *irq;
  698. struct dentry *vdd_dbg_dir, *dbg_dir, *nvalue_dir;
  699. struct omap_volt_data *volt_data;
  700. int i, ret = 0;
  701. if (!sr_info) {
  702. dev_err(&pdev->dev, "%s: unable to allocate sr_info\n",
  703. __func__);
  704. return -ENOMEM;
  705. }
  706. if (!pdata) {
  707. dev_err(&pdev->dev, "%s: platform data missing\n", __func__);
  708. ret = -EINVAL;
  709. goto err_free_devinfo;
  710. }
  711. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  712. if (!mem) {
  713. dev_err(&pdev->dev, "%s: no mem resource\n", __func__);
  714. ret = -ENODEV;
  715. goto err_free_devinfo;
  716. }
  717. irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  718. pm_runtime_enable(&pdev->dev);
  719. sr_info->pdev = pdev;
  720. sr_info->srid = pdev->id;
  721. sr_info->voltdm = pdata->voltdm;
  722. sr_info->nvalue_table = pdata->nvalue_table;
  723. sr_info->nvalue_count = pdata->nvalue_count;
  724. sr_info->senn_mod = pdata->senn_mod;
  725. sr_info->senp_mod = pdata->senp_mod;
  726. sr_info->autocomp_active = false;
  727. sr_info->ip_type = pdata->ip_type;
  728. sr_info->base = ioremap(mem->start, resource_size(mem));
  729. if (!sr_info->base) {
  730. dev_err(&pdev->dev, "%s: ioremap fail\n", __func__);
  731. ret = -ENOMEM;
  732. goto err_release_region;
  733. }
  734. if (irq)
  735. sr_info->irq = irq->start;
  736. sr_set_clk_length(sr_info);
  737. sr_set_regfields(sr_info);
  738. list_add(&sr_info->node, &sr_list);
  739. /*
  740. * Call into late init to do intializations that require
  741. * both sr driver and sr class driver to be initiallized.
  742. */
  743. if (sr_class) {
  744. ret = sr_late_init(sr_info);
  745. if (ret) {
  746. pr_warning("%s: Error in SR late init\n", __func__);
  747. goto err_release_region;
  748. }
  749. }
  750. dev_info(&pdev->dev, "%s: SmartReflex driver initialized\n", __func__);
  751. /*
  752. * If the voltage domain debugfs directory is not created, do
  753. * not try to create rest of the debugfs entries.
  754. */
  755. vdd_dbg_dir = omap_voltage_get_dbgdir(sr_info->voltdm);
  756. if (!vdd_dbg_dir) {
  757. ret = -EINVAL;
  758. goto err_release_region;
  759. }
  760. dbg_dir = debugfs_create_dir("smartreflex", vdd_dbg_dir);
  761. if (IS_ERR(dbg_dir)) {
  762. dev_err(&pdev->dev, "%s: Unable to create debugfs directory\n",
  763. __func__);
  764. ret = PTR_ERR(dbg_dir);
  765. goto err_release_region;
  766. }
  767. (void) debugfs_create_file("autocomp", S_IRUGO | S_IWUSR, dbg_dir,
  768. (void *)sr_info, &pm_sr_fops);
  769. (void) debugfs_create_x32("errweight", S_IRUGO, dbg_dir,
  770. &sr_info->err_weight);
  771. (void) debugfs_create_x32("errmaxlimit", S_IRUGO, dbg_dir,
  772. &sr_info->err_maxlimit);
  773. (void) debugfs_create_x32("errminlimit", S_IRUGO, dbg_dir,
  774. &sr_info->err_minlimit);
  775. nvalue_dir = debugfs_create_dir("nvalue", dbg_dir);
  776. if (IS_ERR(nvalue_dir)) {
  777. dev_err(&pdev->dev, "%s: Unable to create debugfs directory"
  778. "for n-values\n", __func__);
  779. ret = PTR_ERR(nvalue_dir);
  780. goto err_release_region;
  781. }
  782. omap_voltage_get_volttable(sr_info->voltdm, &volt_data);
  783. if (!volt_data) {
  784. dev_warn(&pdev->dev, "%s: No Voltage table for the"
  785. " corresponding vdd vdd_%s. Cannot create debugfs"
  786. "entries for n-values\n",
  787. __func__, sr_info->voltdm->name);
  788. ret = -ENODATA;
  789. goto err_release_region;
  790. }
  791. for (i = 0; i < sr_info->nvalue_count; i++) {
  792. char name[NVALUE_NAME_LEN + 1];
  793. snprintf(name, sizeof(name), "volt_%d",
  794. volt_data[i].volt_nominal);
  795. (void) debugfs_create_x32(name, S_IRUGO | S_IWUSR, nvalue_dir,
  796. &(sr_info->nvalue_table[i].nvalue));
  797. }
  798. return ret;
  799. err_release_region:
  800. release_mem_region(mem->start, resource_size(mem));
  801. err_free_devinfo:
  802. kfree(sr_info);
  803. return ret;
  804. }
  805. static int __devexit omap_sr_remove(struct platform_device *pdev)
  806. {
  807. struct omap_sr_data *pdata = pdev->dev.platform_data;
  808. struct omap_sr *sr_info;
  809. struct resource *mem;
  810. if (!pdata) {
  811. dev_err(&pdev->dev, "%s: platform data missing\n", __func__);
  812. return -EINVAL;
  813. }
  814. sr_info = _sr_lookup(pdata->voltdm);
  815. if (IS_ERR(sr_info)) {
  816. dev_warn(&pdev->dev, "%s: omap_sr struct not found\n",
  817. __func__);
  818. return -EINVAL;
  819. }
  820. if (sr_info->autocomp_active)
  821. sr_stop_vddautocomp(sr_info);
  822. list_del(&sr_info->node);
  823. iounmap(sr_info->base);
  824. kfree(sr_info);
  825. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  826. release_mem_region(mem->start, resource_size(mem));
  827. return 0;
  828. }
  829. static struct platform_driver smartreflex_driver = {
  830. .remove = omap_sr_remove,
  831. .driver = {
  832. .name = "smartreflex",
  833. },
  834. };
  835. static int __init sr_init(void)
  836. {
  837. int ret = 0;
  838. /*
  839. * sr_init is a late init. If by then a pmic specific API is not
  840. * registered either there is no need for anything to be done on
  841. * the PMIC side or somebody has forgotten to register a PMIC
  842. * handler. Warn for the second condition.
  843. */
  844. if (sr_pmic_data && sr_pmic_data->sr_pmic_init)
  845. sr_pmic_data->sr_pmic_init();
  846. else
  847. pr_warning("%s: No PMIC hook to init smartreflex\n", __func__);
  848. ret = platform_driver_probe(&smartreflex_driver, omap_sr_probe);
  849. if (ret) {
  850. pr_err("%s: platform driver register failed for SR\n",
  851. __func__);
  852. return ret;
  853. }
  854. return 0;
  855. }
  856. static void __exit sr_exit(void)
  857. {
  858. platform_driver_unregister(&smartreflex_driver);
  859. }
  860. late_initcall(sr_init);
  861. module_exit(sr_exit);
  862. MODULE_DESCRIPTION("OMAP Smartreflex Driver");
  863. MODULE_LICENSE("GPL");
  864. MODULE_ALIAS("platform:" DRIVER_NAME);
  865. MODULE_AUTHOR("Texas Instruments Inc");