smartreflex.c 28 KB

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