smartreflex.c 28 KB

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