smartreflex.c 26 KB

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