smartreflex.c 29 KB

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