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/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 DRIVER_NAME "smartreflex"
  29. #define SMARTREFLEX_NAME_LEN 32
  30. #define NVALUE_NAME_LEN 40
  31. #define SR_DISABLE_TIMEOUT 200
  32. /* sr_list contains all the instances of smartreflex module */
  33. static LIST_HEAD(sr_list);
  34. static struct omap_sr_class_data *sr_class;
  35. static struct omap_sr_pmic_data *sr_pmic_data;
  36. static struct dentry *sr_dbg_dir;
  37. static inline void sr_write_reg(struct omap_sr *sr, unsigned offset, u32 value)
  38. {
  39. __raw_writel(value, (sr->base + offset));
  40. }
  41. static inline void sr_modify_reg(struct omap_sr *sr, unsigned offset, u32 mask,
  42. u32 value)
  43. {
  44. u32 reg_val;
  45. /*
  46. * Smartreflex error config register is special as it contains
  47. * certain status bits which if written a 1 into means a clear
  48. * of those bits. So in order to make sure no accidental write of
  49. * 1 happens to those status bits, do a clear of them in the read
  50. * value. This mean this API doesn't rewrite values in these bits
  51. * if they are currently set, but does allow the caller to write
  52. * those bits.
  53. */
  54. if (sr->ip_type == SR_TYPE_V1 && offset == ERRCONFIG_V1)
  55. mask |= ERRCONFIG_STATUS_V1_MASK;
  56. else if (sr->ip_type == SR_TYPE_V2 && offset == ERRCONFIG_V2)
  57. mask |= ERRCONFIG_VPBOUNDINTST_V2;
  58. reg_val = __raw_readl(sr->base + offset);
  59. reg_val &= ~mask;
  60. value &= mask;
  61. reg_val |= value;
  62. __raw_writel(reg_val, (sr->base + offset));
  63. }
  64. static inline u32 sr_read_reg(struct omap_sr *sr, unsigned offset)
  65. {
  66. return __raw_readl(sr->base + offset);
  67. }
  68. static struct omap_sr *_sr_lookup(struct voltagedomain *voltdm)
  69. {
  70. struct omap_sr *sr_info;
  71. if (!voltdm) {
  72. pr_err("%s: Null voltage domain passed!\n", __func__);
  73. return ERR_PTR(-EINVAL);
  74. }
  75. list_for_each_entry(sr_info, &sr_list, node) {
  76. if (voltdm == sr_info->voltdm)
  77. return sr_info;
  78. }
  79. return ERR_PTR(-ENODATA);
  80. }
  81. static irqreturn_t sr_interrupt(int irq, void *data)
  82. {
  83. struct omap_sr *sr_info = data;
  84. u32 status = 0;
  85. switch (sr_info->ip_type) {
  86. case SR_TYPE_V1:
  87. /* Read the status bits */
  88. status = sr_read_reg(sr_info, ERRCONFIG_V1);
  89. /* Clear them by writing back */
  90. sr_write_reg(sr_info, ERRCONFIG_V1, status);
  91. break;
  92. case SR_TYPE_V2:
  93. /* Read the status bits */
  94. status = sr_read_reg(sr_info, IRQSTATUS);
  95. /* Clear them by writing back */
  96. sr_write_reg(sr_info, IRQSTATUS, status);
  97. break;
  98. default:
  99. dev_err(&sr_info->pdev->dev, "UNKNOWN IP type %d\n",
  100. sr_info->ip_type);
  101. return IRQ_NONE;
  102. }
  103. if (sr_class->notify)
  104. sr_class->notify(sr_info, status);
  105. return IRQ_HANDLED;
  106. }
  107. static void sr_set_clk_length(struct omap_sr *sr)
  108. {
  109. struct clk *fck;
  110. u32 fclk_speed;
  111. /* Try interconnect target module fck first if it already exists */
  112. fck = clk_get(sr->pdev->dev.parent, "fck");
  113. if (IS_ERR(fck)) {
  114. fck = clk_get(&sr->pdev->dev, "fck");
  115. if (IS_ERR(fck)) {
  116. dev_err(&sr->pdev->dev,
  117. "%s: unable to get fck for device %s\n",
  118. __func__, dev_name(&sr->pdev->dev));
  119. return;
  120. }
  121. }
  122. fclk_speed = clk_get_rate(fck);
  123. clk_put(fck);
  124. switch (fclk_speed) {
  125. case 12000000:
  126. sr->clk_length = SRCLKLENGTH_12MHZ_SYSCLK;
  127. break;
  128. case 13000000:
  129. sr->clk_length = SRCLKLENGTH_13MHZ_SYSCLK;
  130. break;
  131. case 19200000:
  132. sr->clk_length = SRCLKLENGTH_19MHZ_SYSCLK;
  133. break;
  134. case 26000000:
  135. sr->clk_length = SRCLKLENGTH_26MHZ_SYSCLK;
  136. break;
  137. case 38400000:
  138. sr->clk_length = SRCLKLENGTH_38MHZ_SYSCLK;
  139. break;
  140. default:
  141. dev_err(&sr->pdev->dev, "%s: Invalid fclk rate: %d\n",
  142. __func__, fclk_speed);
  143. break;
  144. }
  145. }
  146. static void sr_start_vddautocomp(struct omap_sr *sr)
  147. {
  148. if (!sr_class || !(sr_class->enable) || !(sr_class->configure)) {
  149. dev_warn(&sr->pdev->dev,
  150. "%s: smartreflex class driver not registered\n",
  151. __func__);
  152. return;
  153. }
  154. if (!sr_class->enable(sr))
  155. sr->autocomp_active = true;
  156. }
  157. static void sr_stop_vddautocomp(struct omap_sr *sr)
  158. {
  159. if (!sr_class || !(sr_class->disable)) {
  160. dev_warn(&sr->pdev->dev,
  161. "%s: smartreflex class driver not registered\n",
  162. __func__);
  163. return;
  164. }
  165. if (sr->autocomp_active) {
  166. sr_class->disable(sr, 1);
  167. sr->autocomp_active = false;
  168. }
  169. }
  170. /*
  171. * This function handles the initializations which have to be done
  172. * only when both sr device and class driver regiter has
  173. * completed. This will be attempted to be called from both sr class
  174. * driver register and sr device intializtion API's. Only one call
  175. * will ultimately succeed.
  176. *
  177. * Currently this function registers interrupt handler for a particular SR
  178. * if smartreflex class driver is already registered and has
  179. * requested for interrupts and the SR interrupt line in present.
  180. */
  181. static int sr_late_init(struct omap_sr *sr_info)
  182. {
  183. struct omap_sr_data *pdata = sr_info->pdev->dev.platform_data;
  184. int ret = 0;
  185. if (sr_class->notify && sr_class->notify_flags && sr_info->irq) {
  186. ret = devm_request_irq(&sr_info->pdev->dev, sr_info->irq,
  187. sr_interrupt, 0, sr_info->name, sr_info);
  188. if (ret)
  189. goto error;
  190. disable_irq(sr_info->irq);
  191. }
  192. if (pdata && pdata->enable_on_init)
  193. sr_start_vddautocomp(sr_info);
  194. return ret;
  195. error:
  196. list_del(&sr_info->node);
  197. dev_err(&sr_info->pdev->dev, "%s: ERROR in registering interrupt handler. Smartreflex will not function as desired\n",
  198. __func__);
  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 SmartReflex to perform AVS using the
  288. * error generator module.
  289. * @sr: SR module to be configured.
  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 omap_sr *sr)
  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. if (!sr) {
  305. pr_warn("%s: NULL omap_sr from %pS\n",
  306. __func__, (void *)_RET_IP_);
  307. return -EINVAL;
  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 module without specifying the ip\n",
  333. __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. * @sr: SR module to be configured.
  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 omap_sr *sr)
  359. {
  360. u32 errconfig_offs;
  361. u32 vpboundint_en, vpboundint_st;
  362. if (!sr) {
  363. pr_warn("%s: NULL omap_sr from %pS\n",
  364. __func__, (void *)_RET_IP_);
  365. return -EINVAL;
  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 module without specifying the ip\n",
  380. __func__);
  381. return -EINVAL;
  382. }
  383. /* Disable the Sensor and errorgen */
  384. sr_modify_reg(sr, SRCONFIG, SRCONFIG_SENENABLE | SRCONFIG_ERRGEN_EN, 0);
  385. /*
  386. * Disable the interrupts of ERROR module
  387. * NOTE: modify is a read, modify,write - an implicit OCP barrier
  388. * which is required is present here - sequencing is critical
  389. * at this point (after errgen is disabled, vpboundint disable)
  390. */
  391. sr_modify_reg(sr, errconfig_offs, vpboundint_en | vpboundint_st, 0);
  392. return 0;
  393. }
  394. /**
  395. * sr_configure_minmax() - Configures the SmartReflex to perform AVS using the
  396. * minmaxavg module.
  397. * @sr: SR module to be configured.
  398. *
  399. * This API is to be called from the smartreflex class driver to
  400. * configure the minmaxavg module inside the smartreflex module.
  401. * SR settings if using the ERROR module inside Smartreflex.
  402. * SR CLASS 3 by default uses only the ERROR module where as
  403. * SR CLASS 2 can choose between ERROR module and MINMAXAVG
  404. * module. Returns 0 on success and error value in case of failure.
  405. */
  406. int sr_configure_minmax(struct omap_sr *sr)
  407. {
  408. u32 sr_config, sr_avgwt;
  409. u32 senp_en = 0, senn_en = 0;
  410. u8 senp_shift, senn_shift;
  411. if (!sr) {
  412. pr_warn("%s: NULL omap_sr from %pS\n",
  413. __func__, (void *)_RET_IP_);
  414. return -EINVAL;
  415. }
  416. if (!sr->clk_length)
  417. sr_set_clk_length(sr);
  418. senp_en = sr->senp_mod;
  419. senn_en = sr->senn_mod;
  420. sr_config = (sr->clk_length << SRCONFIG_SRCLKLENGTH_SHIFT) |
  421. SRCONFIG_SENENABLE |
  422. (sr->accum_data << SRCONFIG_ACCUMDATA_SHIFT);
  423. switch (sr->ip_type) {
  424. case SR_TYPE_V1:
  425. sr_config |= SRCONFIG_DELAYCTRL;
  426. senn_shift = SRCONFIG_SENNENABLE_V1_SHIFT;
  427. senp_shift = SRCONFIG_SENPENABLE_V1_SHIFT;
  428. break;
  429. case SR_TYPE_V2:
  430. senn_shift = SRCONFIG_SENNENABLE_V2_SHIFT;
  431. senp_shift = SRCONFIG_SENPENABLE_V2_SHIFT;
  432. break;
  433. default:
  434. dev_err(&sr->pdev->dev, "%s: Trying to Configure smartreflex module without specifying the ip\n",
  435. __func__);
  436. return -EINVAL;
  437. }
  438. sr_config |= ((senn_en << senn_shift) | (senp_en << senp_shift));
  439. sr_write_reg(sr, SRCONFIG, sr_config);
  440. sr_avgwt = (sr->senp_avgweight << AVGWEIGHT_SENPAVGWEIGHT_SHIFT) |
  441. (sr->senn_avgweight << AVGWEIGHT_SENNAVGWEIGHT_SHIFT);
  442. sr_write_reg(sr, AVGWEIGHT, sr_avgwt);
  443. /*
  444. * Enabling the interrupts if MINMAXAVG module is used.
  445. * TODO: check if all the interrupts are mandatory
  446. */
  447. switch (sr->ip_type) {
  448. case SR_TYPE_V1:
  449. sr_modify_reg(sr, ERRCONFIG_V1,
  450. (ERRCONFIG_MCUACCUMINTEN | ERRCONFIG_MCUVALIDINTEN |
  451. ERRCONFIG_MCUBOUNDINTEN),
  452. (ERRCONFIG_MCUACCUMINTEN | ERRCONFIG_MCUACCUMINTST |
  453. ERRCONFIG_MCUVALIDINTEN | ERRCONFIG_MCUVALIDINTST |
  454. ERRCONFIG_MCUBOUNDINTEN | ERRCONFIG_MCUBOUNDINTST));
  455. break;
  456. case SR_TYPE_V2:
  457. sr_write_reg(sr, IRQSTATUS,
  458. IRQSTATUS_MCUACCUMINT | IRQSTATUS_MCVALIDINT |
  459. IRQSTATUS_MCBOUNDSINT | IRQSTATUS_MCUDISABLEACKINT);
  460. sr_write_reg(sr, IRQENABLE_SET,
  461. IRQENABLE_MCUACCUMINT | IRQENABLE_MCUVALIDINT |
  462. IRQENABLE_MCUBOUNDSINT | IRQENABLE_MCUDISABLEACKINT);
  463. break;
  464. default:
  465. dev_err(&sr->pdev->dev, "%s: Trying to Configure smartreflex module without specifying the ip\n",
  466. __func__);
  467. return -EINVAL;
  468. }
  469. return 0;
  470. }
  471. /**
  472. * sr_enable() - Enables the smartreflex module.
  473. * @sr: pointer to which the SR module to be configured belongs to.
  474. * @volt: The voltage at which the Voltage domain associated with
  475. * the smartreflex module is operating at.
  476. * This is required only to program the correct Ntarget value.
  477. *
  478. * This API is to be called from the smartreflex class driver to
  479. * enable a smartreflex module. Returns 0 on success. Returns error
  480. * value if the voltage passed is wrong or if ntarget value is wrong.
  481. */
  482. int sr_enable(struct omap_sr *sr, unsigned long volt)
  483. {
  484. struct omap_volt_data *volt_data;
  485. struct omap_sr_nvalue_table *nvalue_row;
  486. int ret;
  487. if (!sr) {
  488. pr_warn("%s: NULL omap_sr from %pS\n",
  489. __func__, (void *)_RET_IP_);
  490. return -EINVAL;
  491. }
  492. volt_data = omap_voltage_get_voltdata(sr->voltdm, volt);
  493. if (IS_ERR(volt_data)) {
  494. dev_warn(&sr->pdev->dev, "%s: Unable to get voltage table for nominal voltage %ld\n",
  495. __func__, volt);
  496. return PTR_ERR(volt_data);
  497. }
  498. nvalue_row = sr_retrieve_nvalue_row(sr, volt_data->sr_efuse_offs);
  499. if (!nvalue_row) {
  500. dev_warn(&sr->pdev->dev, "%s: failure getting SR data for this voltage %ld\n",
  501. __func__, volt);
  502. return -ENODATA;
  503. }
  504. /* errminlimit is opp dependent and hence linked to voltage */
  505. sr->err_minlimit = nvalue_row->errminlimit;
  506. pm_runtime_get_sync(&sr->pdev->dev);
  507. /* Check if SR is already enabled. If yes do nothing */
  508. if (sr_read_reg(sr, SRCONFIG) & SRCONFIG_SRENABLE)
  509. return 0;
  510. /* Configure SR */
  511. ret = sr_class->configure(sr);
  512. if (ret)
  513. return ret;
  514. sr_write_reg(sr, NVALUERECIPROCAL, nvalue_row->nvalue);
  515. /* SRCONFIG - enable SR */
  516. sr_modify_reg(sr, SRCONFIG, SRCONFIG_SRENABLE, SRCONFIG_SRENABLE);
  517. return 0;
  518. }
  519. /**
  520. * sr_disable() - Disables the smartreflex module.
  521. * @sr: pointer to which the SR module to be configured belongs to.
  522. *
  523. * This API is to be called from the smartreflex class driver to
  524. * disable a smartreflex module.
  525. */
  526. void sr_disable(struct omap_sr *sr)
  527. {
  528. if (!sr) {
  529. pr_warn("%s: NULL omap_sr from %pS\n",
  530. __func__, (void *)_RET_IP_);
  531. return;
  532. }
  533. /* Check if SR clocks are already disabled. If yes do nothing */
  534. if (pm_runtime_suspended(&sr->pdev->dev))
  535. return;
  536. /*
  537. * Disable SR if only it is indeed enabled. Else just
  538. * disable the clocks.
  539. */
  540. if (sr_read_reg(sr, SRCONFIG) & SRCONFIG_SRENABLE) {
  541. switch (sr->ip_type) {
  542. case SR_TYPE_V1:
  543. sr_v1_disable(sr);
  544. break;
  545. case SR_TYPE_V2:
  546. sr_v2_disable(sr);
  547. break;
  548. default:
  549. dev_err(&sr->pdev->dev, "UNKNOWN IP type %d\n",
  550. sr->ip_type);
  551. }
  552. }
  553. pm_runtime_put_sync_suspend(&sr->pdev->dev);
  554. }
  555. /**
  556. * sr_register_class() - API to register a smartreflex class parameters.
  557. * @class_data: The structure containing various sr class specific data.
  558. *
  559. * This API is to be called by the smartreflex class driver to register itself
  560. * with the smartreflex driver during init. Returns 0 on success else the
  561. * error value.
  562. */
  563. int sr_register_class(struct omap_sr_class_data *class_data)
  564. {
  565. struct omap_sr *sr_info;
  566. if (!class_data) {
  567. pr_warn("%s:, Smartreflex class data passed is NULL\n",
  568. __func__);
  569. return -EINVAL;
  570. }
  571. if (sr_class) {
  572. pr_warn("%s: Smartreflex class driver already registered\n",
  573. __func__);
  574. return -EBUSY;
  575. }
  576. sr_class = class_data;
  577. /*
  578. * Call into late init to do initializations that require
  579. * both sr driver and sr class driver to be initiallized.
  580. */
  581. list_for_each_entry(sr_info, &sr_list, node)
  582. sr_late_init(sr_info);
  583. return 0;
  584. }
  585. /**
  586. * omap_sr_enable() - API to enable SR clocks and to call into the
  587. * registered smartreflex class enable API.
  588. * @voltdm: VDD pointer to which the SR module to be configured belongs to.
  589. *
  590. * This API is to be called from the kernel in order to enable
  591. * a particular smartreflex module. This API will do the initial
  592. * configurations to turn on the smartreflex module and in turn call
  593. * into the registered smartreflex class enable API.
  594. */
  595. void omap_sr_enable(struct voltagedomain *voltdm)
  596. {
  597. struct omap_sr *sr = _sr_lookup(voltdm);
  598. if (IS_ERR(sr)) {
  599. pr_warn("%s: omap_sr struct for voltdm not found\n", __func__);
  600. return;
  601. }
  602. if (!sr->autocomp_active)
  603. return;
  604. if (!sr_class || !(sr_class->enable) || !(sr_class->configure)) {
  605. dev_warn(&sr->pdev->dev, "%s: smartreflex class driver not registered\n",
  606. __func__);
  607. return;
  608. }
  609. sr_class->enable(sr);
  610. }
  611. /**
  612. * omap_sr_disable() - API to disable SR without resetting the voltage
  613. * processor voltage
  614. * @voltdm: VDD pointer to which the SR module to be configured belongs to.
  615. *
  616. * This API is to be called from the kernel in order to disable
  617. * a particular smartreflex module. This API will in turn call
  618. * into the registered smartreflex class disable API. This API will tell
  619. * the smartreflex class disable not to reset the VP voltage after
  620. * disabling smartreflex.
  621. */
  622. void omap_sr_disable(struct voltagedomain *voltdm)
  623. {
  624. struct omap_sr *sr = _sr_lookup(voltdm);
  625. if (IS_ERR(sr)) {
  626. pr_warn("%s: omap_sr struct for voltdm not found\n", __func__);
  627. return;
  628. }
  629. if (!sr->autocomp_active)
  630. return;
  631. if (!sr_class || !(sr_class->disable)) {
  632. dev_warn(&sr->pdev->dev, "%s: smartreflex class driver not registered\n",
  633. __func__);
  634. return;
  635. }
  636. sr_class->disable(sr, 0);
  637. }
  638. /**
  639. * omap_sr_disable_reset_volt() - API to disable SR and reset the
  640. * voltage processor voltage
  641. * @voltdm: VDD pointer to which the SR module to be configured belongs to.
  642. *
  643. * This API is to be called from the kernel in order to disable
  644. * a particular smartreflex module. This API will in turn call
  645. * into the registered smartreflex class disable API. This API will tell
  646. * the smartreflex class disable to reset the VP voltage after
  647. * disabling smartreflex.
  648. */
  649. void omap_sr_disable_reset_volt(struct voltagedomain *voltdm)
  650. {
  651. struct omap_sr *sr = _sr_lookup(voltdm);
  652. if (IS_ERR(sr)) {
  653. pr_warn("%s: omap_sr struct for voltdm not found\n", __func__);
  654. return;
  655. }
  656. if (!sr->autocomp_active)
  657. return;
  658. if (!sr_class || !(sr_class->disable)) {
  659. dev_warn(&sr->pdev->dev, "%s: smartreflex class driver not registered\n",
  660. __func__);
  661. return;
  662. }
  663. sr_class->disable(sr, 1);
  664. }
  665. /**
  666. * omap_sr_register_pmic() - API to register pmic specific info.
  667. * @pmic_data: The structure containing pmic specific data.
  668. *
  669. * This API is to be called from the PMIC specific code to register with
  670. * smartreflex driver pmic specific info. Currently the only info required
  671. * is the smartreflex init on the PMIC side.
  672. */
  673. void omap_sr_register_pmic(struct omap_sr_pmic_data *pmic_data)
  674. {
  675. if (!pmic_data) {
  676. pr_warn("%s: Trying to register NULL PMIC data structure with smartreflex\n",
  677. __func__);
  678. return;
  679. }
  680. sr_pmic_data = pmic_data;
  681. }
  682. /* PM Debug FS entries to enable and disable smartreflex. */
  683. static int omap_sr_autocomp_show(void *data, u64 *val)
  684. {
  685. struct omap_sr *sr_info = data;
  686. if (!sr_info) {
  687. pr_warn("%s: omap_sr struct not found\n", __func__);
  688. return -EINVAL;
  689. }
  690. *val = sr_info->autocomp_active;
  691. return 0;
  692. }
  693. static int omap_sr_autocomp_store(void *data, u64 val)
  694. {
  695. struct omap_sr *sr_info = data;
  696. if (!sr_info) {
  697. pr_warn("%s: omap_sr struct not found\n", __func__);
  698. return -EINVAL;
  699. }
  700. /* Sanity check */
  701. if (val > 1) {
  702. pr_warn("%s: Invalid argument %lld\n", __func__, val);
  703. return -EINVAL;
  704. }
  705. /* control enable/disable only if there is a delta in value */
  706. if (sr_info->autocomp_active != val) {
  707. if (!val)
  708. sr_stop_vddautocomp(sr_info);
  709. else
  710. sr_start_vddautocomp(sr_info);
  711. }
  712. return 0;
  713. }
  714. DEFINE_SIMPLE_ATTRIBUTE(pm_sr_fops, omap_sr_autocomp_show,
  715. omap_sr_autocomp_store, "%llu\n");
  716. static int omap_sr_probe(struct platform_device *pdev)
  717. {
  718. struct omap_sr *sr_info;
  719. struct omap_sr_data *pdata = pdev->dev.platform_data;
  720. struct resource *mem, *irq;
  721. struct dentry *nvalue_dir;
  722. int i, ret = 0;
  723. sr_info = devm_kzalloc(&pdev->dev, sizeof(struct omap_sr), GFP_KERNEL);
  724. if (!sr_info)
  725. return -ENOMEM;
  726. sr_info->name = devm_kzalloc(&pdev->dev,
  727. SMARTREFLEX_NAME_LEN, GFP_KERNEL);
  728. if (!sr_info->name)
  729. return -ENOMEM;
  730. platform_set_drvdata(pdev, sr_info);
  731. if (!pdata) {
  732. dev_err(&pdev->dev, "%s: platform data missing\n", __func__);
  733. return -EINVAL;
  734. }
  735. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  736. sr_info->base = devm_ioremap_resource(&pdev->dev, mem);
  737. if (IS_ERR(sr_info->base)) {
  738. dev_err(&pdev->dev, "%s: ioremap fail\n", __func__);
  739. return PTR_ERR(sr_info->base);
  740. }
  741. irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  742. pm_runtime_enable(&pdev->dev);
  743. pm_runtime_irq_safe(&pdev->dev);
  744. snprintf(sr_info->name, SMARTREFLEX_NAME_LEN, "%s", pdata->name);
  745. sr_info->pdev = pdev;
  746. sr_info->srid = pdev->id;
  747. sr_info->voltdm = pdata->voltdm;
  748. sr_info->nvalue_table = pdata->nvalue_table;
  749. sr_info->nvalue_count = pdata->nvalue_count;
  750. sr_info->senn_mod = pdata->senn_mod;
  751. sr_info->senp_mod = pdata->senp_mod;
  752. sr_info->err_weight = pdata->err_weight;
  753. sr_info->err_maxlimit = pdata->err_maxlimit;
  754. sr_info->accum_data = pdata->accum_data;
  755. sr_info->senn_avgweight = pdata->senn_avgweight;
  756. sr_info->senp_avgweight = pdata->senp_avgweight;
  757. sr_info->autocomp_active = false;
  758. sr_info->ip_type = pdata->ip_type;
  759. if (irq)
  760. sr_info->irq = irq->start;
  761. sr_set_clk_length(sr_info);
  762. list_add(&sr_info->node, &sr_list);
  763. ret = pm_runtime_get_sync(&pdev->dev);
  764. if (ret < 0) {
  765. pm_runtime_put_noidle(&pdev->dev);
  766. goto err_list_del;
  767. }
  768. /*
  769. * Call into late init to do initializations that require
  770. * both sr driver and sr class driver to be initiallized.
  771. */
  772. if (sr_class) {
  773. ret = sr_late_init(sr_info);
  774. if (ret) {
  775. pr_warn("%s: Error in SR late init\n", __func__);
  776. goto err_list_del;
  777. }
  778. }
  779. dev_info(&pdev->dev, "%s: SmartReflex driver initialized\n", __func__);
  780. if (!sr_dbg_dir) {
  781. sr_dbg_dir = debugfs_create_dir("smartreflex", NULL);
  782. if (IS_ERR_OR_NULL(sr_dbg_dir)) {
  783. ret = PTR_ERR(sr_dbg_dir);
  784. pr_err("%s:sr debugfs dir creation failed(%d)\n",
  785. __func__, ret);
  786. goto err_list_del;
  787. }
  788. }
  789. sr_info->dbg_dir = debugfs_create_dir(sr_info->name, sr_dbg_dir);
  790. if (IS_ERR_OR_NULL(sr_info->dbg_dir)) {
  791. dev_err(&pdev->dev, "%s: Unable to create debugfs directory\n",
  792. __func__);
  793. ret = PTR_ERR(sr_info->dbg_dir);
  794. goto err_debugfs;
  795. }
  796. (void) debugfs_create_file("autocomp", S_IRUGO | S_IWUSR,
  797. sr_info->dbg_dir, (void *)sr_info, &pm_sr_fops);
  798. (void) debugfs_create_x32("errweight", S_IRUGO, sr_info->dbg_dir,
  799. &sr_info->err_weight);
  800. (void) debugfs_create_x32("errmaxlimit", S_IRUGO, sr_info->dbg_dir,
  801. &sr_info->err_maxlimit);
  802. nvalue_dir = debugfs_create_dir("nvalue", sr_info->dbg_dir);
  803. if (IS_ERR_OR_NULL(nvalue_dir)) {
  804. dev_err(&pdev->dev, "%s: Unable to create debugfs directory for n-values\n",
  805. __func__);
  806. ret = PTR_ERR(nvalue_dir);
  807. goto err_debugfs;
  808. }
  809. if (sr_info->nvalue_count == 0 || !sr_info->nvalue_table) {
  810. dev_warn(&pdev->dev, "%s: %s: No Voltage table for the corresponding vdd. Cannot create debugfs entries for n-values\n",
  811. __func__, sr_info->name);
  812. ret = -ENODATA;
  813. goto err_debugfs;
  814. }
  815. for (i = 0; i < sr_info->nvalue_count; i++) {
  816. char name[NVALUE_NAME_LEN + 1];
  817. snprintf(name, sizeof(name), "volt_%lu",
  818. sr_info->nvalue_table[i].volt_nominal);
  819. (void) debugfs_create_x32(name, S_IRUGO | S_IWUSR, nvalue_dir,
  820. &(sr_info->nvalue_table[i].nvalue));
  821. snprintf(name, sizeof(name), "errminlimit_%lu",
  822. sr_info->nvalue_table[i].volt_nominal);
  823. (void) debugfs_create_x32(name, S_IRUGO | S_IWUSR, nvalue_dir,
  824. &(sr_info->nvalue_table[i].errminlimit));
  825. }
  826. pm_runtime_put_sync(&pdev->dev);
  827. return ret;
  828. err_debugfs:
  829. debugfs_remove_recursive(sr_info->dbg_dir);
  830. err_list_del:
  831. list_del(&sr_info->node);
  832. pm_runtime_put_sync(&pdev->dev);
  833. return ret;
  834. }
  835. static int omap_sr_remove(struct platform_device *pdev)
  836. {
  837. struct omap_sr_data *pdata = pdev->dev.platform_data;
  838. struct omap_sr *sr_info;
  839. if (!pdata) {
  840. dev_err(&pdev->dev, "%s: platform data missing\n", __func__);
  841. return -EINVAL;
  842. }
  843. sr_info = _sr_lookup(pdata->voltdm);
  844. if (IS_ERR(sr_info)) {
  845. dev_warn(&pdev->dev, "%s: omap_sr struct not found\n",
  846. __func__);
  847. return PTR_ERR(sr_info);
  848. }
  849. if (sr_info->autocomp_active)
  850. sr_stop_vddautocomp(sr_info);
  851. if (sr_info->dbg_dir)
  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");