smartreflex.c 29 KB

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