smartreflex.c 28 KB

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