smartreflex.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  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, (vpboundint_en | vpboundint_st),
  385. vpboundint_en);
  386. return 0;
  387. }
  388. /**
  389. * sr_disable_errgen() - Disables SmartReflex AVS module's errgen component
  390. * @voltdm: VDD pointer to which the SR module to be configured belongs to.
  391. *
  392. * This API is to be called from the smartreflex class driver to
  393. * disable the error generator module inside the smartreflex module.
  394. *
  395. * Returns 0 on success and error value in case of failure.
  396. */
  397. int sr_disable_errgen(struct voltagedomain *voltdm)
  398. {
  399. u32 errconfig_offs, vpboundint_en;
  400. u32 vpboundint_st;
  401. struct omap_sr *sr = _sr_lookup(voltdm);
  402. if (IS_ERR(sr)) {
  403. pr_warning("%s: omap_sr struct for sr_%s not found\n",
  404. __func__, voltdm->name);
  405. return -EINVAL;
  406. }
  407. if (sr->ip_type == SR_TYPE_V1) {
  408. errconfig_offs = ERRCONFIG_V1;
  409. vpboundint_en = ERRCONFIG_VPBOUNDINTEN_V1;
  410. vpboundint_st = ERRCONFIG_VPBOUNDINTST_V1;
  411. } else if (sr->ip_type == SR_TYPE_V2) {
  412. errconfig_offs = ERRCONFIG_V2;
  413. vpboundint_en = ERRCONFIG_VPBOUNDINTEN_V2;
  414. vpboundint_st = ERRCONFIG_VPBOUNDINTST_V2;
  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. /* Disable the interrupts of ERROR module */
  421. sr_modify_reg(sr, errconfig_offs, vpboundint_en | vpboundint_st, 0);
  422. /* Disable the Sensor and errorgen */
  423. sr_modify_reg(sr, SRCONFIG, SRCONFIG_SENENABLE | SRCONFIG_ERRGEN_EN, 0);
  424. return 0;
  425. }
  426. /**
  427. * sr_configure_minmax() - Configures the smrtreflex to perform AVS using the
  428. * minmaxavg module.
  429. * @voltdm: VDD pointer to which the SR module to be configured belongs to.
  430. *
  431. * This API is to be called from the smartreflex class driver to
  432. * configure the minmaxavg module inside the smartreflex module.
  433. * SR settings if using the ERROR module inside Smartreflex.
  434. * SR CLASS 3 by default uses only the ERROR module where as
  435. * SR CLASS 2 can choose between ERROR module and MINMAXAVG
  436. * module. Returns 0 on success and error value in case of failure.
  437. */
  438. int sr_configure_minmax(struct voltagedomain *voltdm)
  439. {
  440. u32 sr_config, sr_avgwt;
  441. u32 senp_en = 0, senn_en = 0;
  442. u8 senp_shift, senn_shift;
  443. struct omap_sr *sr = _sr_lookup(voltdm);
  444. if (IS_ERR(sr)) {
  445. pr_warning("%s: omap_sr struct for sr_%s not found\n",
  446. __func__, voltdm->name);
  447. return -EINVAL;
  448. }
  449. if (!sr->clk_length)
  450. sr_set_clk_length(sr);
  451. senp_en = sr->senp_mod;
  452. senn_en = sr->senn_mod;
  453. sr_config = (sr->clk_length << SRCONFIG_SRCLKLENGTH_SHIFT) |
  454. SRCONFIG_SENENABLE |
  455. (sr->accum_data << SRCONFIG_ACCUMDATA_SHIFT);
  456. if (sr->ip_type == SR_TYPE_V1) {
  457. sr_config |= SRCONFIG_DELAYCTRL;
  458. senn_shift = SRCONFIG_SENNENABLE_V1_SHIFT;
  459. senp_shift = SRCONFIG_SENPENABLE_V1_SHIFT;
  460. } else if (sr->ip_type == SR_TYPE_V2) {
  461. senn_shift = SRCONFIG_SENNENABLE_V2_SHIFT;
  462. senp_shift = SRCONFIG_SENPENABLE_V2_SHIFT;
  463. } else {
  464. dev_err(&sr->pdev->dev, "%s: Trying to Configure smartreflex"
  465. "module without specifying the ip\n", __func__);
  466. return -EINVAL;
  467. }
  468. sr_config |= ((senn_en << senn_shift) | (senp_en << senp_shift));
  469. sr_write_reg(sr, SRCONFIG, sr_config);
  470. sr_avgwt = (sr->senp_avgweight << AVGWEIGHT_SENPAVGWEIGHT_SHIFT) |
  471. (sr->senn_avgweight << AVGWEIGHT_SENNAVGWEIGHT_SHIFT);
  472. sr_write_reg(sr, AVGWEIGHT, sr_avgwt);
  473. /*
  474. * Enabling the interrupts if MINMAXAVG module is used.
  475. * TODO: check if all the interrupts are mandatory
  476. */
  477. if (sr->ip_type == SR_TYPE_V1) {
  478. sr_modify_reg(sr, ERRCONFIG_V1,
  479. (ERRCONFIG_MCUACCUMINTEN | ERRCONFIG_MCUVALIDINTEN |
  480. ERRCONFIG_MCUBOUNDINTEN),
  481. (ERRCONFIG_MCUACCUMINTEN | ERRCONFIG_MCUACCUMINTST |
  482. ERRCONFIG_MCUVALIDINTEN | ERRCONFIG_MCUVALIDINTST |
  483. ERRCONFIG_MCUBOUNDINTEN | ERRCONFIG_MCUBOUNDINTST));
  484. } else if (sr->ip_type == SR_TYPE_V2) {
  485. sr_write_reg(sr, IRQSTATUS,
  486. IRQSTATUS_MCUACCUMINT | IRQSTATUS_MCVALIDINT |
  487. IRQSTATUS_MCBOUNDSINT | IRQSTATUS_MCUDISABLEACKINT);
  488. sr_write_reg(sr, IRQENABLE_SET,
  489. IRQENABLE_MCUACCUMINT | IRQENABLE_MCUVALIDINT |
  490. IRQENABLE_MCUBOUNDSINT | IRQENABLE_MCUDISABLEACKINT);
  491. }
  492. return 0;
  493. }
  494. /**
  495. * sr_enable() - Enables the smartreflex module.
  496. * @voltdm: VDD pointer to which the SR module to be configured belongs to.
  497. * @volt: The voltage at which the Voltage domain associated with
  498. * the smartreflex module is operating at.
  499. * This is required only to program the correct Ntarget value.
  500. *
  501. * This API is to be called from the smartreflex class driver to
  502. * enable a smartreflex module. Returns 0 on success. Returns error
  503. * value if the voltage passed is wrong or if ntarget value is wrong.
  504. */
  505. int sr_enable(struct voltagedomain *voltdm, unsigned long volt)
  506. {
  507. u32 nvalue_reciprocal;
  508. struct omap_volt_data *volt_data;
  509. struct omap_sr *sr = _sr_lookup(voltdm);
  510. int ret;
  511. if (IS_ERR(sr)) {
  512. pr_warning("%s: omap_sr struct for sr_%s not found\n",
  513. __func__, voltdm->name);
  514. return -EINVAL;
  515. }
  516. volt_data = omap_voltage_get_voltdata(sr->voltdm, volt);
  517. if (IS_ERR(volt_data)) {
  518. dev_warn(&sr->pdev->dev, "%s: Unable to get voltage table"
  519. "for nominal voltage %ld\n", __func__, volt);
  520. return -ENODATA;
  521. }
  522. nvalue_reciprocal = sr_retrieve_nvalue(sr, volt_data->sr_efuse_offs);
  523. if (!nvalue_reciprocal) {
  524. dev_warn(&sr->pdev->dev, "%s: NVALUE = 0 at voltage %ld\n",
  525. __func__, volt);
  526. return -ENODATA;
  527. }
  528. /* errminlimit is opp dependent and hence linked to voltage */
  529. sr->err_minlimit = volt_data->sr_errminlimit;
  530. pm_runtime_get_sync(&sr->pdev->dev);
  531. /* Check if SR is already enabled. If yes do nothing */
  532. if (sr_read_reg(sr, SRCONFIG) & SRCONFIG_SRENABLE)
  533. return 0;
  534. /* Configure SR */
  535. ret = sr_class->configure(voltdm);
  536. if (ret)
  537. return ret;
  538. sr_write_reg(sr, NVALUERECIPROCAL, nvalue_reciprocal);
  539. /* SRCONFIG - enable SR */
  540. sr_modify_reg(sr, SRCONFIG, SRCONFIG_SRENABLE, SRCONFIG_SRENABLE);
  541. return 0;
  542. }
  543. /**
  544. * sr_disable() - Disables the smartreflex module.
  545. * @voltdm: VDD pointer to which the SR module to be configured belongs to.
  546. *
  547. * This API is to be called from the smartreflex class driver to
  548. * disable a smartreflex module.
  549. */
  550. void sr_disable(struct voltagedomain *voltdm)
  551. {
  552. struct omap_sr *sr = _sr_lookup(voltdm);
  553. if (IS_ERR(sr)) {
  554. pr_warning("%s: omap_sr struct for sr_%s not found\n",
  555. __func__, voltdm->name);
  556. return;
  557. }
  558. /* Check if SR clocks are already disabled. If yes do nothing */
  559. if (pm_runtime_suspended(&sr->pdev->dev))
  560. return;
  561. /*
  562. * Disable SR if only it is indeed enabled. Else just
  563. * disable the clocks.
  564. */
  565. if (sr_read_reg(sr, SRCONFIG) & SRCONFIG_SRENABLE) {
  566. if (sr->ip_type == SR_TYPE_V1)
  567. sr_v1_disable(sr);
  568. else if (sr->ip_type == SR_TYPE_V2)
  569. sr_v2_disable(sr);
  570. }
  571. pm_runtime_put_sync_suspend(&sr->pdev->dev);
  572. }
  573. /**
  574. * sr_register_class() - API to register a smartreflex class parameters.
  575. * @class_data: The structure containing various sr class specific data.
  576. *
  577. * This API is to be called by the smartreflex class driver to register itself
  578. * with the smartreflex driver during init. Returns 0 on success else the
  579. * error value.
  580. */
  581. int sr_register_class(struct omap_sr_class_data *class_data)
  582. {
  583. struct omap_sr *sr_info;
  584. if (!class_data) {
  585. pr_warning("%s:, Smartreflex class data passed is NULL\n",
  586. __func__);
  587. return -EINVAL;
  588. }
  589. if (sr_class) {
  590. pr_warning("%s: Smartreflex class driver already registered\n",
  591. __func__);
  592. return -EBUSY;
  593. }
  594. sr_class = class_data;
  595. /*
  596. * Call into late init to do intializations that require
  597. * both sr driver and sr class driver to be initiallized.
  598. */
  599. list_for_each_entry(sr_info, &sr_list, node)
  600. sr_late_init(sr_info);
  601. return 0;
  602. }
  603. /**
  604. * omap_sr_enable() - API to enable SR clocks and to call into the
  605. * registered smartreflex class enable API.
  606. * @voltdm: VDD pointer to which the SR module to be configured belongs to.
  607. *
  608. * This API is to be called from the kernel in order to enable
  609. * a particular smartreflex module. This API will do the initial
  610. * configurations to turn on the smartreflex module and in turn call
  611. * into the registered smartreflex class enable API.
  612. */
  613. void omap_sr_enable(struct voltagedomain *voltdm)
  614. {
  615. struct omap_sr *sr = _sr_lookup(voltdm);
  616. if (IS_ERR(sr)) {
  617. pr_warning("%s: omap_sr struct for sr_%s not found\n",
  618. __func__, voltdm->name);
  619. return;
  620. }
  621. if (!sr->autocomp_active)
  622. return;
  623. if (!sr_class || !(sr_class->enable) || !(sr_class->configure)) {
  624. dev_warn(&sr->pdev->dev, "%s: smartreflex class driver not"
  625. "registered\n", __func__);
  626. return;
  627. }
  628. sr_class->enable(voltdm);
  629. }
  630. /**
  631. * omap_sr_disable() - API to disable SR without resetting the voltage
  632. * processor voltage
  633. * @voltdm: VDD pointer to which the SR module to be configured belongs to.
  634. *
  635. * This API is to be called from the kernel in order to disable
  636. * a particular smartreflex module. This API will in turn call
  637. * into the registered smartreflex class disable API. This API will tell
  638. * the smartreflex class disable not to reset the VP voltage after
  639. * disabling smartreflex.
  640. */
  641. void omap_sr_disable(struct voltagedomain *voltdm)
  642. {
  643. struct omap_sr *sr = _sr_lookup(voltdm);
  644. if (IS_ERR(sr)) {
  645. pr_warning("%s: omap_sr struct for sr_%s not found\n",
  646. __func__, voltdm->name);
  647. return;
  648. }
  649. if (!sr->autocomp_active)
  650. return;
  651. if (!sr_class || !(sr_class->disable)) {
  652. dev_warn(&sr->pdev->dev, "%s: smartreflex class driver not"
  653. "registered\n", __func__);
  654. return;
  655. }
  656. sr_class->disable(voltdm, 0);
  657. }
  658. /**
  659. * omap_sr_disable_reset_volt() - API to disable SR and reset the
  660. * voltage processor voltage
  661. * @voltdm: VDD pointer to which the SR module to be configured belongs to.
  662. *
  663. * This API is to be called from the kernel in order to disable
  664. * a particular smartreflex module. This API will in turn call
  665. * into the registered smartreflex class disable API. This API will tell
  666. * the smartreflex class disable to reset the VP voltage after
  667. * disabling smartreflex.
  668. */
  669. void omap_sr_disable_reset_volt(struct voltagedomain *voltdm)
  670. {
  671. struct omap_sr *sr = _sr_lookup(voltdm);
  672. if (IS_ERR(sr)) {
  673. pr_warning("%s: omap_sr struct for sr_%s not found\n",
  674. __func__, voltdm->name);
  675. return;
  676. }
  677. if (!sr->autocomp_active)
  678. return;
  679. if (!sr_class || !(sr_class->disable)) {
  680. dev_warn(&sr->pdev->dev, "%s: smartreflex class driver not"
  681. "registered\n", __func__);
  682. return;
  683. }
  684. sr_class->disable(voltdm, 1);
  685. }
  686. /**
  687. * omap_sr_register_pmic() - API to register pmic specific info.
  688. * @pmic_data: The structure containing pmic specific data.
  689. *
  690. * This API is to be called from the PMIC specific code to register with
  691. * smartreflex driver pmic specific info. Currently the only info required
  692. * is the smartreflex init on the PMIC side.
  693. */
  694. void omap_sr_register_pmic(struct omap_sr_pmic_data *pmic_data)
  695. {
  696. if (!pmic_data) {
  697. pr_warning("%s: Trying to register NULL PMIC data structure"
  698. "with smartreflex\n", __func__);
  699. return;
  700. }
  701. sr_pmic_data = pmic_data;
  702. }
  703. /* PM Debug Fs enteries to enable disable smartreflex. */
  704. static int omap_sr_autocomp_show(void *data, u64 *val)
  705. {
  706. struct omap_sr *sr_info = (struct omap_sr *) data;
  707. if (!sr_info) {
  708. pr_warning("%s: omap_sr struct not found\n", __func__);
  709. return -EINVAL;
  710. }
  711. *val = sr_info->autocomp_active;
  712. return 0;
  713. }
  714. static int omap_sr_autocomp_store(void *data, u64 val)
  715. {
  716. struct omap_sr *sr_info = (struct omap_sr *) data;
  717. if (!sr_info) {
  718. pr_warning("%s: omap_sr struct not found\n", __func__);
  719. return -EINVAL;
  720. }
  721. /* Sanity check */
  722. if (val && (val != 1)) {
  723. pr_warning("%s: Invalid argument %lld\n", __func__, val);
  724. return -EINVAL;
  725. }
  726. /* control enable/disable only if there is a delta in value */
  727. if (sr_info->autocomp_active != val) {
  728. if (!val)
  729. sr_stop_vddautocomp(sr_info);
  730. else
  731. sr_start_vddautocomp(sr_info);
  732. }
  733. return 0;
  734. }
  735. DEFINE_SIMPLE_ATTRIBUTE(pm_sr_fops, omap_sr_autocomp_show,
  736. omap_sr_autocomp_store, "%llu\n");
  737. static int __init omap_sr_probe(struct platform_device *pdev)
  738. {
  739. struct omap_sr *sr_info = kzalloc(sizeof(struct omap_sr), GFP_KERNEL);
  740. struct omap_sr_data *pdata = pdev->dev.platform_data;
  741. struct resource *mem, *irq;
  742. struct dentry *nvalue_dir;
  743. struct omap_volt_data *volt_data;
  744. int i, ret = 0;
  745. char *name;
  746. if (!sr_info) {
  747. dev_err(&pdev->dev, "%s: unable to allocate sr_info\n",
  748. __func__);
  749. return -ENOMEM;
  750. }
  751. if (!pdata) {
  752. dev_err(&pdev->dev, "%s: platform data missing\n", __func__);
  753. ret = -EINVAL;
  754. goto err_free_devinfo;
  755. }
  756. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  757. if (!mem) {
  758. dev_err(&pdev->dev, "%s: no mem resource\n", __func__);
  759. ret = -ENODEV;
  760. goto err_free_devinfo;
  761. }
  762. mem = request_mem_region(mem->start, resource_size(mem),
  763. dev_name(&pdev->dev));
  764. if (!mem) {
  765. dev_err(&pdev->dev, "%s: no mem region\n", __func__);
  766. ret = -EBUSY;
  767. goto err_free_devinfo;
  768. }
  769. irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  770. pm_runtime_enable(&pdev->dev);
  771. pm_runtime_irq_safe(&pdev->dev);
  772. sr_info->pdev = pdev;
  773. sr_info->srid = pdev->id;
  774. sr_info->voltdm = pdata->voltdm;
  775. sr_info->nvalue_table = pdata->nvalue_table;
  776. sr_info->nvalue_count = pdata->nvalue_count;
  777. sr_info->senn_mod = pdata->senn_mod;
  778. sr_info->senp_mod = pdata->senp_mod;
  779. sr_info->autocomp_active = false;
  780. sr_info->ip_type = pdata->ip_type;
  781. sr_info->base = ioremap(mem->start, resource_size(mem));
  782. if (!sr_info->base) {
  783. dev_err(&pdev->dev, "%s: ioremap fail\n", __func__);
  784. ret = -ENOMEM;
  785. goto err_release_region;
  786. }
  787. if (irq)
  788. sr_info->irq = irq->start;
  789. sr_set_clk_length(sr_info);
  790. sr_set_regfields(sr_info);
  791. list_add(&sr_info->node, &sr_list);
  792. /*
  793. * Call into late init to do intializations that require
  794. * both sr driver and sr class driver to be initiallized.
  795. */
  796. if (sr_class) {
  797. ret = sr_late_init(sr_info);
  798. if (ret) {
  799. pr_warning("%s: Error in SR late init\n", __func__);
  800. goto err_iounmap;
  801. }
  802. }
  803. dev_info(&pdev->dev, "%s: SmartReflex driver initialized\n", __func__);
  804. if (!sr_dbg_dir) {
  805. sr_dbg_dir = debugfs_create_dir("smartreflex", NULL);
  806. if (!sr_dbg_dir) {
  807. ret = PTR_ERR(sr_dbg_dir);
  808. pr_err("%s:sr debugfs dir creation failed(%d)\n",
  809. __func__, ret);
  810. goto err_iounmap;
  811. }
  812. }
  813. name = kasprintf(GFP_KERNEL, "sr_%s", sr_info->voltdm->name);
  814. if (!name) {
  815. dev_err(&pdev->dev, "%s: Unable to alloc debugfs name\n",
  816. __func__);
  817. ret = -ENOMEM;
  818. goto err_iounmap;
  819. }
  820. sr_info->dbg_dir = debugfs_create_dir(name, sr_dbg_dir);
  821. kfree(name);
  822. if (IS_ERR(sr_info->dbg_dir)) {
  823. dev_err(&pdev->dev, "%s: Unable to create debugfs directory\n",
  824. __func__);
  825. ret = PTR_ERR(sr_info->dbg_dir);
  826. goto err_iounmap;
  827. }
  828. (void) debugfs_create_file("autocomp", S_IRUGO | S_IWUSR,
  829. sr_info->dbg_dir, (void *)sr_info, &pm_sr_fops);
  830. (void) debugfs_create_x32("errweight", S_IRUGO, sr_info->dbg_dir,
  831. &sr_info->err_weight);
  832. (void) debugfs_create_x32("errmaxlimit", S_IRUGO, sr_info->dbg_dir,
  833. &sr_info->err_maxlimit);
  834. (void) debugfs_create_x32("errminlimit", S_IRUGO, sr_info->dbg_dir,
  835. &sr_info->err_minlimit);
  836. nvalue_dir = debugfs_create_dir("nvalue", sr_info->dbg_dir);
  837. if (IS_ERR(nvalue_dir)) {
  838. dev_err(&pdev->dev, "%s: Unable to create debugfs directory"
  839. "for n-values\n", __func__);
  840. ret = PTR_ERR(nvalue_dir);
  841. goto err_debugfs;
  842. }
  843. omap_voltage_get_volttable(sr_info->voltdm, &volt_data);
  844. if (!volt_data) {
  845. dev_warn(&pdev->dev, "%s: No Voltage table for the"
  846. " corresponding vdd vdd_%s. Cannot create debugfs"
  847. "entries for n-values\n",
  848. __func__, sr_info->voltdm->name);
  849. ret = -ENODATA;
  850. goto err_debugfs;
  851. }
  852. for (i = 0; i < sr_info->nvalue_count; i++) {
  853. char name[NVALUE_NAME_LEN + 1];
  854. snprintf(name, sizeof(name), "volt_%d",
  855. volt_data[i].volt_nominal);
  856. (void) debugfs_create_x32(name, S_IRUGO | S_IWUSR, nvalue_dir,
  857. &(sr_info->nvalue_table[i].nvalue));
  858. }
  859. return ret;
  860. err_debugfs:
  861. debugfs_remove_recursive(sr_info->dbg_dir);
  862. err_iounmap:
  863. list_del(&sr_info->node);
  864. iounmap(sr_info->base);
  865. err_release_region:
  866. release_mem_region(mem->start, resource_size(mem));
  867. err_free_devinfo:
  868. kfree(sr_info);
  869. return ret;
  870. }
  871. static int __devexit omap_sr_remove(struct platform_device *pdev)
  872. {
  873. struct omap_sr_data *pdata = pdev->dev.platform_data;
  874. struct omap_sr *sr_info;
  875. struct resource *mem;
  876. if (!pdata) {
  877. dev_err(&pdev->dev, "%s: platform data missing\n", __func__);
  878. return -EINVAL;
  879. }
  880. sr_info = _sr_lookup(pdata->voltdm);
  881. if (IS_ERR(sr_info)) {
  882. dev_warn(&pdev->dev, "%s: omap_sr struct not found\n",
  883. __func__);
  884. return -EINVAL;
  885. }
  886. if (sr_info->autocomp_active)
  887. sr_stop_vddautocomp(sr_info);
  888. if (sr_info->dbg_dir)
  889. debugfs_remove_recursive(sr_info->dbg_dir);
  890. list_del(&sr_info->node);
  891. iounmap(sr_info->base);
  892. kfree(sr_info);
  893. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  894. release_mem_region(mem->start, resource_size(mem));
  895. return 0;
  896. }
  897. static struct platform_driver smartreflex_driver = {
  898. .remove = __devexit_p(omap_sr_remove),
  899. .driver = {
  900. .name = "smartreflex",
  901. },
  902. };
  903. static int __init sr_init(void)
  904. {
  905. int ret = 0;
  906. /*
  907. * sr_init is a late init. If by then a pmic specific API is not
  908. * registered either there is no need for anything to be done on
  909. * the PMIC side or somebody has forgotten to register a PMIC
  910. * handler. Warn for the second condition.
  911. */
  912. if (sr_pmic_data && sr_pmic_data->sr_pmic_init)
  913. sr_pmic_data->sr_pmic_init();
  914. else
  915. pr_warning("%s: No PMIC hook to init smartreflex\n", __func__);
  916. ret = platform_driver_probe(&smartreflex_driver, omap_sr_probe);
  917. if (ret) {
  918. pr_err("%s: platform driver register failed for SR\n",
  919. __func__);
  920. return ret;
  921. }
  922. return 0;
  923. }
  924. static void __exit sr_exit(void)
  925. {
  926. platform_driver_unregister(&smartreflex_driver);
  927. }
  928. late_initcall(sr_init);
  929. module_exit(sr_exit);
  930. MODULE_DESCRIPTION("OMAP Smartreflex Driver");
  931. MODULE_LICENSE("GPL");
  932. MODULE_ALIAS("platform:" DRIVER_NAME);
  933. MODULE_AUTHOR("Texas Instruments Inc");