smartreflex.c 29 KB

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