smartreflex.c 29 KB

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