smartreflex.c 31 KB

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