mem-acc-regulator.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534
  1. /* Copyright (c) 2014-2017, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #define pr_fmt(fmt) "ACC: %s: " fmt, __func__
  13. #include <linux/module.h>
  14. #include <linux/mutex.h>
  15. #include <linux/types.h>
  16. #include <linux/init.h>
  17. #include <linux/slab.h>
  18. #include <linux/err.h>
  19. #include <linux/of.h>
  20. #include <linux/of_device.h>
  21. #include <linux/io.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/regulator/driver.h>
  24. #include <linux/regulator/machine.h>
  25. #include <linux/regulator/of_regulator.h>
  26. #include <linux/string.h>
  27. #include <soc/qcom/scm.h>
  28. #define MEM_ACC_DEFAULT_SEL_SIZE 2
  29. #define BYTES_PER_FUSE_ROW 8
  30. /* mem-acc config flags */
  31. enum {
  32. MEM_ACC_USE_CORNER_ACC_MAP = BIT(0),
  33. MEM_ACC_USE_ADDR_VAL_MAP = BIT(1),
  34. };
  35. #define FUSE_MAP_NO_MATCH (-1)
  36. #define FUSE_PARAM_MATCH_ANY (-1)
  37. #define PARAM_MATCH_ANY (-1)
  38. enum {
  39. MEMORY_L1,
  40. MEMORY_L2,
  41. MEMORY_MAX,
  42. };
  43. #define MEM_ACC_TYPE_MAX 6
  44. /**
  45. * struct acc_reg_value - Acc register configuration structure
  46. * @addr_index: An index in to phys_reg_addr_list and remap_reg_addr_list
  47. * to get the ACC register physical address and remapped address.
  48. * @reg_val: Value to program in to the register mapped by addr_index.
  49. */
  50. struct acc_reg_value {
  51. u32 addr_index;
  52. u32 reg_val;
  53. };
  54. struct corner_acc_reg_config {
  55. struct acc_reg_value *reg_config_list;
  56. int max_reg_config_len;
  57. };
  58. struct mem_acc_regulator {
  59. struct device *dev;
  60. struct regulator_desc rdesc;
  61. struct regulator_dev *rdev;
  62. int corner;
  63. bool mem_acc_supported[MEMORY_MAX];
  64. bool mem_acc_custom_supported[MEMORY_MAX];
  65. u32 *acc_sel_mask[MEMORY_MAX];
  66. u32 *acc_sel_bit_pos[MEMORY_MAX];
  67. u32 acc_sel_bit_size[MEMORY_MAX];
  68. u32 num_acc_sel[MEMORY_MAX];
  69. u32 *acc_en_bit_pos;
  70. u32 num_acc_en;
  71. u32 *corner_acc_map;
  72. u32 num_corners;
  73. u32 override_fuse_value;
  74. int override_map_match;
  75. int override_map_count;
  76. void __iomem *acc_sel_base[MEMORY_MAX];
  77. void __iomem *acc_en_base;
  78. phys_addr_t acc_sel_addr[MEMORY_MAX];
  79. phys_addr_t acc_en_addr;
  80. u32 flags;
  81. void __iomem *acc_custom_addr[MEMORY_MAX];
  82. u32 *acc_custom_data[MEMORY_MAX];
  83. phys_addr_t mem_acc_type_addr[MEM_ACC_TYPE_MAX];
  84. u32 *mem_acc_type_data;
  85. /* eFuse parameters */
  86. phys_addr_t efuse_addr;
  87. void __iomem *efuse_base;
  88. u32 num_acc_reg;
  89. u32 *phys_reg_addr_list;
  90. void __iomem **remap_reg_addr_list;
  91. struct corner_acc_reg_config *corner_acc_reg_config;
  92. u32 *override_acc_range_fuse_list;
  93. int override_acc_range_fuse_num;
  94. };
  95. static DEFINE_MUTEX(mem_acc_memory_mutex);
  96. static u64 mem_acc_read_efuse_row(struct mem_acc_regulator *mem_acc_vreg,
  97. u32 row_num, bool use_tz_api)
  98. {
  99. int rc;
  100. u64 efuse_bits;
  101. struct scm_desc desc = {0};
  102. struct mem_acc_read_req {
  103. u32 row_address;
  104. int addr_type;
  105. } req;
  106. struct mem_acc_read_rsp {
  107. u32 row_data[2];
  108. u32 status;
  109. } rsp;
  110. if (!use_tz_api) {
  111. efuse_bits = readq_relaxed(mem_acc_vreg->efuse_base
  112. + row_num * BYTES_PER_FUSE_ROW);
  113. return efuse_bits;
  114. }
  115. desc.args[0] = req.row_address = mem_acc_vreg->efuse_addr +
  116. row_num * BYTES_PER_FUSE_ROW;
  117. desc.args[1] = req.addr_type = 0;
  118. desc.arginfo = SCM_ARGS(2);
  119. efuse_bits = 0;
  120. if (!is_scm_armv8()) {
  121. rc = scm_call(SCM_SVC_FUSE, SCM_FUSE_READ,
  122. &req, sizeof(req), &rsp, sizeof(rsp));
  123. } else {
  124. rc = scm_call2(SCM_SIP_FNID(SCM_SVC_FUSE, SCM_FUSE_READ),
  125. &desc);
  126. rsp.row_data[0] = desc.ret[0];
  127. rsp.row_data[1] = desc.ret[1];
  128. rsp.status = desc.ret[2];
  129. }
  130. if (rc) {
  131. pr_err("read row %d failed, err code = %d", row_num, rc);
  132. } else {
  133. efuse_bits = ((u64)(rsp.row_data[1]) << 32) +
  134. (u64)rsp.row_data[0];
  135. }
  136. return efuse_bits;
  137. }
  138. static inline u32 apc_to_acc_corner(struct mem_acc_regulator *mem_acc_vreg,
  139. int corner)
  140. {
  141. /*
  142. * corner_acc_map maps the corner from index 0 and APC corner value
  143. * starts from the value 1
  144. */
  145. return mem_acc_vreg->corner_acc_map[corner - 1];
  146. }
  147. static void __update_acc_sel(struct mem_acc_regulator *mem_acc_vreg,
  148. int corner, int mem_type)
  149. {
  150. u32 acc_data, acc_data_old, i, bit, acc_corner;
  151. acc_data = readl_relaxed(mem_acc_vreg->acc_sel_base[mem_type]);
  152. acc_data_old = acc_data;
  153. for (i = 0; i < mem_acc_vreg->num_acc_sel[mem_type]; i++) {
  154. bit = mem_acc_vreg->acc_sel_bit_pos[mem_type][i];
  155. acc_data &= ~mem_acc_vreg->acc_sel_mask[mem_type][i];
  156. acc_corner = apc_to_acc_corner(mem_acc_vreg, corner);
  157. acc_data |= (acc_corner << bit) &
  158. mem_acc_vreg->acc_sel_mask[mem_type][i];
  159. }
  160. pr_debug("corner=%d old_acc_sel=0x%02x new_acc_sel=0x%02x mem_type=%d\n",
  161. corner, acc_data_old, acc_data, mem_type);
  162. writel_relaxed(acc_data, mem_acc_vreg->acc_sel_base[mem_type]);
  163. }
  164. static void __update_acc_type(struct mem_acc_regulator *mem_acc_vreg,
  165. int corner)
  166. {
  167. int i, rc;
  168. for (i = 0; i < MEM_ACC_TYPE_MAX; i++) {
  169. if (mem_acc_vreg->mem_acc_type_addr[i]) {
  170. rc = scm_io_write(mem_acc_vreg->mem_acc_type_addr[i],
  171. mem_acc_vreg->mem_acc_type_data[corner - 1 + i *
  172. mem_acc_vreg->num_corners]);
  173. if (rc)
  174. pr_err("scm_io_write: %pa failure rc:%d\n",
  175. &(mem_acc_vreg->mem_acc_type_addr[i]),
  176. rc);
  177. }
  178. }
  179. }
  180. static void __update_acc_custom(struct mem_acc_regulator *mem_acc_vreg,
  181. int corner, int mem_type)
  182. {
  183. writel_relaxed(
  184. mem_acc_vreg->acc_custom_data[mem_type][corner-1],
  185. mem_acc_vreg->acc_custom_addr[mem_type]);
  186. pr_debug("corner=%d mem_type=%d custom_data=0x%2x\n", corner,
  187. mem_type, mem_acc_vreg->acc_custom_data[mem_type][corner-1]);
  188. }
  189. static void update_acc_sel(struct mem_acc_regulator *mem_acc_vreg, int corner)
  190. {
  191. int i;
  192. for (i = 0; i < MEMORY_MAX; i++) {
  193. if (mem_acc_vreg->mem_acc_supported[i])
  194. __update_acc_sel(mem_acc_vreg, corner, i);
  195. if (mem_acc_vreg->mem_acc_custom_supported[i])
  196. __update_acc_custom(mem_acc_vreg, corner, i);
  197. }
  198. if (mem_acc_vreg->mem_acc_type_data)
  199. __update_acc_type(mem_acc_vreg, corner);
  200. }
  201. static void update_acc_reg(struct mem_acc_regulator *mem_acc_vreg, int corner)
  202. {
  203. struct corner_acc_reg_config *corner_acc_reg_config;
  204. struct acc_reg_value *reg_config_list;
  205. int i, index;
  206. u32 addr_index, reg_val;
  207. corner_acc_reg_config =
  208. &mem_acc_vreg->corner_acc_reg_config[mem_acc_vreg->corner];
  209. reg_config_list = corner_acc_reg_config->reg_config_list;
  210. for (i = 0; i < corner_acc_reg_config->max_reg_config_len; i++) {
  211. /*
  212. * Use (corner - 1) in the below equation as
  213. * the reg_config_list[] stores the values starting from
  214. * index '0' where as the minimum corner value allowed
  215. * in regulator framework is '1'.
  216. */
  217. index = (corner - 1) * corner_acc_reg_config->max_reg_config_len
  218. + i;
  219. addr_index = reg_config_list[index].addr_index;
  220. reg_val = reg_config_list[index].reg_val;
  221. if (addr_index == PARAM_MATCH_ANY)
  222. break;
  223. writel_relaxed(reg_val,
  224. mem_acc_vreg->remap_reg_addr_list[addr_index]);
  225. /* make sure write complete */
  226. mb();
  227. pr_debug("corner=%d register:0x%x value:0x%x\n", corner,
  228. mem_acc_vreg->phys_reg_addr_list[addr_index], reg_val);
  229. }
  230. }
  231. static int mem_acc_regulator_set_voltage(struct regulator_dev *rdev,
  232. int corner, int corner_max, unsigned int *selector)
  233. {
  234. struct mem_acc_regulator *mem_acc_vreg = rdev_get_drvdata(rdev);
  235. int i;
  236. if (corner > mem_acc_vreg->num_corners) {
  237. pr_err("Invalid corner=%d requested\n", corner);
  238. return -EINVAL;
  239. }
  240. pr_debug("old corner=%d, new corner=%d\n",
  241. mem_acc_vreg->corner, corner);
  242. if (corner == mem_acc_vreg->corner)
  243. return 0;
  244. /* go up or down one level at a time */
  245. mutex_lock(&mem_acc_memory_mutex);
  246. if (mem_acc_vreg->flags & MEM_ACC_USE_ADDR_VAL_MAP) {
  247. update_acc_reg(mem_acc_vreg, corner);
  248. } else if (mem_acc_vreg->flags & MEM_ACC_USE_CORNER_ACC_MAP) {
  249. if (corner > mem_acc_vreg->corner) {
  250. for (i = mem_acc_vreg->corner + 1; i <= corner; i++) {
  251. pr_debug("UP: to corner %d\n", i);
  252. update_acc_sel(mem_acc_vreg, i);
  253. }
  254. } else {
  255. for (i = mem_acc_vreg->corner - 1; i >= corner; i--) {
  256. pr_debug("DOWN: to corner %d\n", i);
  257. update_acc_sel(mem_acc_vreg, i);
  258. }
  259. }
  260. }
  261. mutex_unlock(&mem_acc_memory_mutex);
  262. pr_debug("new voltage corner set %d\n", corner);
  263. mem_acc_vreg->corner = corner;
  264. return 0;
  265. }
  266. static int mem_acc_regulator_get_voltage(struct regulator_dev *rdev)
  267. {
  268. struct mem_acc_regulator *mem_acc_vreg = rdev_get_drvdata(rdev);
  269. return mem_acc_vreg->corner;
  270. }
  271. static struct regulator_ops mem_acc_corner_ops = {
  272. .set_voltage = mem_acc_regulator_set_voltage,
  273. .get_voltage = mem_acc_regulator_get_voltage,
  274. };
  275. static int __mem_acc_sel_init(struct mem_acc_regulator *mem_acc_vreg,
  276. int mem_type)
  277. {
  278. int i;
  279. u32 bit, mask;
  280. mem_acc_vreg->acc_sel_mask[mem_type] = devm_kzalloc(mem_acc_vreg->dev,
  281. mem_acc_vreg->num_acc_sel[mem_type] * sizeof(u32), GFP_KERNEL);
  282. if (!mem_acc_vreg->acc_sel_mask[mem_type])
  283. return -ENOMEM;
  284. for (i = 0; i < mem_acc_vreg->num_acc_sel[mem_type]; i++) {
  285. bit = mem_acc_vreg->acc_sel_bit_pos[mem_type][i];
  286. mask = BIT(mem_acc_vreg->acc_sel_bit_size[mem_type]) - 1;
  287. mem_acc_vreg->acc_sel_mask[mem_type][i] = mask << bit;
  288. }
  289. return 0;
  290. }
  291. static int mem_acc_sel_init(struct mem_acc_regulator *mem_acc_vreg)
  292. {
  293. int i, rc;
  294. for (i = 0; i < MEMORY_MAX; i++) {
  295. if (mem_acc_vreg->mem_acc_supported[i]) {
  296. rc = __mem_acc_sel_init(mem_acc_vreg, i);
  297. if (rc) {
  298. pr_err("Unable to initialize mem_type=%d rc=%d\n",
  299. i, rc);
  300. return rc;
  301. }
  302. }
  303. }
  304. return 0;
  305. }
  306. static void mem_acc_en_init(struct mem_acc_regulator *mem_acc_vreg)
  307. {
  308. int i, bit;
  309. u32 acc_data;
  310. acc_data = readl_relaxed(mem_acc_vreg->acc_en_base);
  311. pr_debug("init: acc_en_register=%x\n", acc_data);
  312. for (i = 0; i < mem_acc_vreg->num_acc_en; i++) {
  313. bit = mem_acc_vreg->acc_en_bit_pos[i];
  314. acc_data |= BIT(bit);
  315. }
  316. pr_debug("final: acc_en_register=%x\n", acc_data);
  317. writel_relaxed(acc_data, mem_acc_vreg->acc_en_base);
  318. }
  319. static int populate_acc_data(struct mem_acc_regulator *mem_acc_vreg,
  320. const char *prop_name, u32 **value, u32 *len)
  321. {
  322. int rc;
  323. if (!of_get_property(mem_acc_vreg->dev->of_node, prop_name, len)) {
  324. pr_err("Unable to find %s property\n", prop_name);
  325. return -EINVAL;
  326. }
  327. *len /= sizeof(u32);
  328. if (!(*len)) {
  329. pr_err("Incorrect entries in %s\n", prop_name);
  330. return -EINVAL;
  331. }
  332. *value = devm_kzalloc(mem_acc_vreg->dev, (*len) * sizeof(u32),
  333. GFP_KERNEL);
  334. if (!(*value)) {
  335. pr_err("Unable to allocate memory for %s\n", prop_name);
  336. return -ENOMEM;
  337. }
  338. pr_debug("Found %s, data-length = %d\n", prop_name, *len);
  339. rc = of_property_read_u32_array(mem_acc_vreg->dev->of_node,
  340. prop_name, *value, *len);
  341. if (rc) {
  342. pr_err("Unable to populate %s rc=%d\n", prop_name, rc);
  343. return rc;
  344. }
  345. return 0;
  346. }
  347. static int mem_acc_sel_setup(struct mem_acc_regulator *mem_acc_vreg,
  348. struct resource *res, int mem_type)
  349. {
  350. int len, rc;
  351. char *mem_select_str;
  352. char *mem_select_size_str;
  353. mem_acc_vreg->acc_sel_addr[mem_type] = res->start;
  354. len = res->end - res->start + 1;
  355. pr_debug("'acc_sel_addr' = %pa mem_type=%d (len=%d)\n",
  356. &res->start, mem_type, len);
  357. mem_acc_vreg->acc_sel_base[mem_type] = devm_ioremap(mem_acc_vreg->dev,
  358. mem_acc_vreg->acc_sel_addr[mem_type], len);
  359. if (!mem_acc_vreg->acc_sel_base[mem_type]) {
  360. pr_err("Unable to map 'acc_sel_addr' %pa for mem_type=%d\n",
  361. &mem_acc_vreg->acc_sel_addr[mem_type], mem_type);
  362. return -EINVAL;
  363. }
  364. switch (mem_type) {
  365. case MEMORY_L1:
  366. mem_select_str = "qcom,acc-sel-l1-bit-pos";
  367. mem_select_size_str = "qcom,acc-sel-l1-bit-size";
  368. break;
  369. case MEMORY_L2:
  370. mem_select_str = "qcom,acc-sel-l2-bit-pos";
  371. mem_select_size_str = "qcom,acc-sel-l2-bit-size";
  372. break;
  373. default:
  374. pr_err("Invalid memory type: %d\n", mem_type);
  375. return -EINVAL;
  376. }
  377. mem_acc_vreg->acc_sel_bit_size[mem_type] = MEM_ACC_DEFAULT_SEL_SIZE;
  378. of_property_read_u32(mem_acc_vreg->dev->of_node, mem_select_size_str,
  379. &mem_acc_vreg->acc_sel_bit_size[mem_type]);
  380. rc = populate_acc_data(mem_acc_vreg, mem_select_str,
  381. &mem_acc_vreg->acc_sel_bit_pos[mem_type],
  382. &mem_acc_vreg->num_acc_sel[mem_type]);
  383. if (rc)
  384. pr_err("Unable to populate '%s' rc=%d\n", mem_select_str, rc);
  385. return rc;
  386. }
  387. static int mem_acc_efuse_init(struct platform_device *pdev,
  388. struct mem_acc_regulator *mem_acc_vreg)
  389. {
  390. struct resource *res;
  391. int len;
  392. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "efuse_addr");
  393. if (!res || !res->start) {
  394. mem_acc_vreg->efuse_base = NULL;
  395. pr_debug("'efuse_addr' resource missing or not used.\n");
  396. return 0;
  397. }
  398. mem_acc_vreg->efuse_addr = res->start;
  399. len = res->end - res->start + 1;
  400. pr_info("efuse_addr = %pa (len=0x%x)\n", &res->start, len);
  401. mem_acc_vreg->efuse_base = devm_ioremap(&pdev->dev,
  402. mem_acc_vreg->efuse_addr, len);
  403. if (!mem_acc_vreg->efuse_base) {
  404. pr_err("Unable to map efuse_addr %pa\n",
  405. &mem_acc_vreg->efuse_addr);
  406. return -EINVAL;
  407. }
  408. return 0;
  409. }
  410. static int mem_acc_custom_data_init(struct platform_device *pdev,
  411. struct mem_acc_regulator *mem_acc_vreg,
  412. int mem_type)
  413. {
  414. struct resource *res;
  415. char *custom_apc_addr_str, *custom_apc_data_str;
  416. int len, rc = 0;
  417. switch (mem_type) {
  418. case MEMORY_L1:
  419. custom_apc_addr_str = "acc-l1-custom";
  420. custom_apc_data_str = "qcom,l1-acc-custom-data";
  421. break;
  422. case MEMORY_L2:
  423. custom_apc_addr_str = "acc-l2-custom";
  424. custom_apc_data_str = "qcom,l2-acc-custom-data";
  425. break;
  426. default:
  427. pr_err("Invalid memory type: %d\n", mem_type);
  428. return -EINVAL;
  429. }
  430. if (!of_find_property(mem_acc_vreg->dev->of_node,
  431. custom_apc_data_str, NULL)) {
  432. pr_debug("%s custom_data not specified\n", custom_apc_data_str);
  433. return 0;
  434. }
  435. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  436. custom_apc_addr_str);
  437. if (!res || !res->start) {
  438. pr_debug("%s resource missing\n", custom_apc_addr_str);
  439. return -EINVAL;
  440. }
  441. len = res->end - res->start + 1;
  442. mem_acc_vreg->acc_custom_addr[mem_type] =
  443. devm_ioremap(mem_acc_vreg->dev, res->start, len);
  444. if (!mem_acc_vreg->acc_custom_addr[mem_type]) {
  445. pr_err("Unable to map %s %pa\n",
  446. custom_apc_addr_str, &res->start);
  447. return -EINVAL;
  448. }
  449. rc = populate_acc_data(mem_acc_vreg, custom_apc_data_str,
  450. &mem_acc_vreg->acc_custom_data[mem_type], &len);
  451. if (rc) {
  452. pr_err("Unable to find %s rc=%d\n", custom_apc_data_str, rc);
  453. return rc;
  454. }
  455. if (mem_acc_vreg->num_corners != len) {
  456. pr_err("Custom data is not present for all the corners\n");
  457. return -EINVAL;
  458. }
  459. mem_acc_vreg->mem_acc_custom_supported[mem_type] = true;
  460. return 0;
  461. }
  462. static int override_mem_acc_custom_data(struct mem_acc_regulator *mem_acc_vreg,
  463. int mem_type)
  464. {
  465. char *custom_apc_data_str;
  466. int len, rc = 0, i;
  467. int tuple_count, tuple_match;
  468. u32 index = 0, value = 0;
  469. switch (mem_type) {
  470. case MEMORY_L1:
  471. custom_apc_data_str = "qcom,override-l1-acc-custom-data";
  472. break;
  473. case MEMORY_L2:
  474. custom_apc_data_str = "qcom,override-l2-acc-custom-data";
  475. break;
  476. default:
  477. pr_err("Invalid memory type: %d\n", mem_type);
  478. return -EINVAL;
  479. }
  480. if (!of_find_property(mem_acc_vreg->dev->of_node,
  481. custom_apc_data_str, &len)) {
  482. pr_debug("%s not specified\n", custom_apc_data_str);
  483. return 0;
  484. }
  485. if (mem_acc_vreg->override_map_count) {
  486. if (mem_acc_vreg->override_map_match == FUSE_MAP_NO_MATCH)
  487. return 0;
  488. tuple_count = mem_acc_vreg->override_map_count;
  489. tuple_match = mem_acc_vreg->override_map_match;
  490. } else {
  491. tuple_count = 1;
  492. tuple_match = 0;
  493. }
  494. if (len != mem_acc_vreg->num_corners * tuple_count * sizeof(u32)) {
  495. pr_err("%s length=%d is invalid\n", custom_apc_data_str, len);
  496. return -EINVAL;
  497. }
  498. for (i = 0; i < mem_acc_vreg->num_corners; i++) {
  499. index = (tuple_match * mem_acc_vreg->num_corners) + i;
  500. rc = of_property_read_u32_index(mem_acc_vreg->dev->of_node,
  501. custom_apc_data_str, index, &value);
  502. if (rc) {
  503. pr_err("Unable read %s index %u, rc=%d\n",
  504. custom_apc_data_str, index, rc);
  505. return rc;
  506. }
  507. mem_acc_vreg->acc_custom_data[mem_type][i] = value;
  508. }
  509. return 0;
  510. }
  511. static int mem_acc_override_corner_map(struct mem_acc_regulator *mem_acc_vreg)
  512. {
  513. int len = 0, i, rc;
  514. int tuple_count, tuple_match;
  515. u32 index = 0, value = 0;
  516. char *prop_str = "qcom,override-corner-acc-map";
  517. if (!of_find_property(mem_acc_vreg->dev->of_node, prop_str, &len))
  518. return 0;
  519. if (mem_acc_vreg->override_map_count) {
  520. if (mem_acc_vreg->override_map_match == FUSE_MAP_NO_MATCH)
  521. return 0;
  522. tuple_count = mem_acc_vreg->override_map_count;
  523. tuple_match = mem_acc_vreg->override_map_match;
  524. } else {
  525. tuple_count = 1;
  526. tuple_match = 0;
  527. }
  528. if (len != mem_acc_vreg->num_corners * tuple_count * sizeof(u32)) {
  529. pr_err("%s length=%d is invalid\n", prop_str, len);
  530. return -EINVAL;
  531. }
  532. for (i = 0; i < mem_acc_vreg->num_corners; i++) {
  533. index = (tuple_match * mem_acc_vreg->num_corners) + i;
  534. rc = of_property_read_u32_index(mem_acc_vreg->dev->of_node,
  535. prop_str, index, &value);
  536. if (rc) {
  537. pr_err("Unable read %s index %u, rc=%d\n",
  538. prop_str, index, rc);
  539. return rc;
  540. }
  541. mem_acc_vreg->corner_acc_map[i] = value;
  542. }
  543. return 0;
  544. }
  545. static void mem_acc_read_efuse_param(struct mem_acc_regulator *mem_acc_vreg,
  546. u32 *fuse_sel, int *val)
  547. {
  548. u64 fuse_bits;
  549. fuse_bits = mem_acc_read_efuse_row(mem_acc_vreg, fuse_sel[0],
  550. fuse_sel[3]);
  551. /*
  552. * fuse_sel[1] = LSB position in row (shift)
  553. * fuse_sel[2] = num of bits (mask)
  554. */
  555. *val = (fuse_bits >> fuse_sel[1]) & ((1 << fuse_sel[2]) - 1);
  556. }
  557. #define FUSE_TUPLE_SIZE 4
  558. static int mem_acc_parse_override_fuse_version_map(
  559. struct mem_acc_regulator *mem_acc_vreg)
  560. {
  561. struct device_node *of_node = mem_acc_vreg->dev->of_node;
  562. int i, rc, tuple_size;
  563. int len = 0;
  564. u32 *tmp;
  565. u32 fuse_sel[4];
  566. char *prop_str;
  567. prop_str = "qcom,override-acc-fuse-sel";
  568. rc = of_property_read_u32_array(of_node, prop_str, fuse_sel,
  569. FUSE_TUPLE_SIZE);
  570. if (rc < 0) {
  571. pr_err("Read failed - %s rc=%d\n", prop_str, rc);
  572. return rc;
  573. }
  574. mem_acc_read_efuse_param(mem_acc_vreg, fuse_sel,
  575. &mem_acc_vreg->override_fuse_value);
  576. prop_str = "qcom,override-fuse-version-map";
  577. if (!of_find_property(of_node, prop_str, &len))
  578. return -EINVAL;
  579. tuple_size = 1;
  580. mem_acc_vreg->override_map_count = len / (sizeof(u32) * tuple_size);
  581. if (len == 0 || len % (sizeof(u32) * tuple_size)) {
  582. pr_err("%s length=%d is invalid\n", prop_str, len);
  583. return -EINVAL;
  584. }
  585. tmp = kzalloc(len, GFP_KERNEL);
  586. if (!tmp)
  587. return -ENOMEM;
  588. rc = of_property_read_u32_array(of_node, prop_str, tmp,
  589. mem_acc_vreg->override_map_count * tuple_size);
  590. if (rc) {
  591. pr_err("could not read %s rc=%d\n", prop_str, rc);
  592. goto done;
  593. }
  594. for (i = 0; i < mem_acc_vreg->override_map_count; i++) {
  595. if (tmp[i * tuple_size] != mem_acc_vreg->override_fuse_value
  596. && tmp[i * tuple_size] != FUSE_PARAM_MATCH_ANY) {
  597. continue;
  598. } else {
  599. mem_acc_vreg->override_map_match = i;
  600. break;
  601. }
  602. }
  603. if (mem_acc_vreg->override_map_match != FUSE_MAP_NO_MATCH)
  604. pr_info("override_fuse_val=%d, %s tuple match found: %d\n",
  605. mem_acc_vreg->override_fuse_value, prop_str,
  606. mem_acc_vreg->override_map_match);
  607. else
  608. pr_err("%s tuple match not found\n", prop_str);
  609. done:
  610. kfree(tmp);
  611. return rc;
  612. }
  613. static int mem_acc_parse_override_fuse_version_range(
  614. struct mem_acc_regulator *mem_acc_vreg)
  615. {
  616. struct device_node *of_node = mem_acc_vreg->dev->of_node;
  617. int i, j, rc, size, row_size;
  618. int num_fuse_sel, len = 0;
  619. u32 *tmp = NULL;
  620. char *prop_str;
  621. u32 *fuse_val, *fuse_sel;
  622. char *buf = NULL;
  623. int pos = 0, buflen;
  624. prop_str = "qcom,override-acc-range-fuse-list";
  625. if (!of_find_property(of_node, prop_str, &len)) {
  626. pr_err("%s property is missing\n", prop_str);
  627. return -EINVAL;
  628. }
  629. size = len / sizeof(u32);
  630. if (len == 0 || (size % FUSE_TUPLE_SIZE)) {
  631. pr_err("%s property length (%d) is invalid\n", prop_str, len);
  632. return -EINVAL;
  633. }
  634. num_fuse_sel = size / FUSE_TUPLE_SIZE;
  635. fuse_val = devm_kcalloc(mem_acc_vreg->dev, num_fuse_sel,
  636. sizeof(*fuse_val), GFP_KERNEL);
  637. if (!fuse_val)
  638. return -ENOMEM;
  639. mem_acc_vreg->override_acc_range_fuse_list = fuse_val;
  640. mem_acc_vreg->override_acc_range_fuse_num = num_fuse_sel;
  641. fuse_sel = kzalloc(len, GFP_KERNEL);
  642. if (!fuse_sel) {
  643. rc = -ENOMEM;
  644. goto done;
  645. }
  646. rc = of_property_read_u32_array(of_node, prop_str, fuse_sel,
  647. size);
  648. if (rc) {
  649. pr_err("%s read failed, rc=%d\n", prop_str, rc);
  650. goto done;
  651. }
  652. for (i = 0; i < num_fuse_sel; i++) {
  653. mem_acc_read_efuse_param(mem_acc_vreg, &fuse_sel[i * 4],
  654. &fuse_val[i]);
  655. }
  656. prop_str = "qcom,override-fuse-range-map";
  657. if (!of_find_property(of_node, prop_str, &len))
  658. goto done;
  659. row_size = num_fuse_sel * 2;
  660. mem_acc_vreg->override_map_count = len / (sizeof(u32) * row_size);
  661. if (len == 0 || len % (sizeof(u32) * row_size)) {
  662. pr_err("%s length=%d is invalid\n", prop_str, len);
  663. rc = -EINVAL;
  664. goto done;
  665. }
  666. tmp = kzalloc(len, GFP_KERNEL);
  667. if (!tmp) {
  668. rc = -ENOMEM;
  669. goto done;
  670. }
  671. rc = of_property_read_u32_array(of_node, prop_str, tmp,
  672. mem_acc_vreg->override_map_count * row_size);
  673. if (rc) {
  674. pr_err("could not read %s rc=%d\n", prop_str, rc);
  675. goto done;
  676. }
  677. for (i = 0; i < mem_acc_vreg->override_map_count; i++) {
  678. for (j = 0; j < num_fuse_sel; j++) {
  679. if (tmp[i * row_size + j * 2] > fuse_val[j]
  680. || tmp[i * row_size + j * 2 + 1] < fuse_val[j])
  681. break;
  682. }
  683. if (j == num_fuse_sel) {
  684. mem_acc_vreg->override_map_match = i;
  685. break;
  686. }
  687. }
  688. /*
  689. * Log register and value mapping since they are useful for
  690. * baseline MEM ACC logging.
  691. */
  692. buflen = num_fuse_sel * sizeof("fuse_selxxxx = XXXX ");
  693. buf = kzalloc(buflen, GFP_KERNEL);
  694. if (!buf)
  695. goto done;
  696. for (j = 0; j < num_fuse_sel; j++)
  697. pos += scnprintf(buf + pos, buflen - pos, "fuse_sel%d = %d ",
  698. j, fuse_val[j]);
  699. buf[pos] = '\0';
  700. if (mem_acc_vreg->override_map_match != FUSE_MAP_NO_MATCH)
  701. pr_info("%s %s tuple match found: %d\n", buf, prop_str,
  702. mem_acc_vreg->override_map_match);
  703. else
  704. pr_err("%s %s tuple match not found\n", buf, prop_str);
  705. done:
  706. kfree(fuse_sel);
  707. kfree(tmp);
  708. kfree(buf);
  709. return rc;
  710. }
  711. #define MAX_CHARS_PER_INT 20
  712. static int mem_acc_reg_addr_val_dump(struct mem_acc_regulator *mem_acc_vreg,
  713. struct corner_acc_reg_config *corner_acc_reg_config,
  714. u32 corner)
  715. {
  716. int i, k, index, pos = 0;
  717. u32 addr_index;
  718. size_t buflen;
  719. char *buf;
  720. struct acc_reg_value *reg_config_list =
  721. corner_acc_reg_config->reg_config_list;
  722. int max_reg_config_len = corner_acc_reg_config->max_reg_config_len;
  723. int num_corners = mem_acc_vreg->num_corners;
  724. /*
  725. * Log register and value mapping since they are useful for
  726. * baseline MEM ACC logging.
  727. */
  728. buflen = max_reg_config_len * (MAX_CHARS_PER_INT + 6) * sizeof(*buf);
  729. buf = kzalloc(buflen, GFP_KERNEL);
  730. if (buf == NULL) {
  731. pr_err("Could not allocate memory for acc register and value logging\n");
  732. return -ENOMEM;
  733. }
  734. for (i = 0; i < num_corners; i++) {
  735. if (corner == i + 1)
  736. continue;
  737. pr_debug("Corner: %d --> %d:\n", corner, i + 1);
  738. pos = 0;
  739. for (k = 0; k < max_reg_config_len; k++) {
  740. index = i * max_reg_config_len + k;
  741. addr_index = reg_config_list[index].addr_index;
  742. if (addr_index == PARAM_MATCH_ANY)
  743. break;
  744. pos += scnprintf(buf + pos, buflen - pos,
  745. "<0x%x 0x%x> ",
  746. mem_acc_vreg->phys_reg_addr_list[addr_index],
  747. reg_config_list[index].reg_val);
  748. }
  749. buf[pos] = '\0';
  750. pr_debug("%s\n", buf);
  751. }
  752. kfree(buf);
  753. return 0;
  754. }
  755. static int mem_acc_get_reg_addr_val(struct device_node *of_node,
  756. const char *prop_str, struct acc_reg_value *reg_config_list,
  757. int list_offset, int list_size, u32 max_reg_index)
  758. {
  759. int i, index, rc = 0;
  760. for (i = 0; i < list_size / 2; i++) {
  761. index = (list_offset * list_size) + i * 2;
  762. rc = of_property_read_u32_index(of_node, prop_str, index,
  763. &reg_config_list[i].addr_index);
  764. rc |= of_property_read_u32_index(of_node, prop_str, index + 1,
  765. &reg_config_list[i].reg_val);
  766. if (rc) {
  767. pr_err("could not read %s at tuple %u: rc=%d\n",
  768. prop_str, index, rc);
  769. return rc;
  770. }
  771. if (reg_config_list[i].addr_index == PARAM_MATCH_ANY)
  772. continue;
  773. if ((!reg_config_list[i].addr_index) ||
  774. reg_config_list[i].addr_index > max_reg_index) {
  775. pr_err("Invalid register index %u in %s at tuple %u\n",
  776. reg_config_list[i].addr_index, prop_str, index);
  777. return -EINVAL;
  778. }
  779. }
  780. return rc;
  781. }
  782. static int mem_acc_override_reg_addr_val_init(
  783. struct mem_acc_regulator *mem_acc_vreg)
  784. {
  785. struct device_node *of_node = mem_acc_vreg->dev->of_node;
  786. struct corner_acc_reg_config *corner_acc_reg_config;
  787. struct acc_reg_value *override_reg_config_list;
  788. int i, tuple_count, tuple_match, len = 0, rc = 0;
  789. u32 list_size, override_max_reg_config_len;
  790. char prop_str[40];
  791. struct property *prop;
  792. int num_corners = mem_acc_vreg->num_corners;
  793. if (!mem_acc_vreg->corner_acc_reg_config)
  794. return 0;
  795. if (mem_acc_vreg->override_map_count) {
  796. if (mem_acc_vreg->override_map_match == FUSE_MAP_NO_MATCH)
  797. return 0;
  798. tuple_count = mem_acc_vreg->override_map_count;
  799. tuple_match = mem_acc_vreg->override_map_match;
  800. } else {
  801. tuple_count = 1;
  802. tuple_match = 0;
  803. }
  804. corner_acc_reg_config = mem_acc_vreg->corner_acc_reg_config;
  805. for (i = 1; i <= num_corners; i++) {
  806. snprintf(prop_str, sizeof(prop_str),
  807. "qcom,override-corner%d-addr-val-map", i);
  808. prop = of_find_property(of_node, prop_str, &len);
  809. list_size = len / (tuple_count * sizeof(u32));
  810. if (!prop) {
  811. pr_debug("%s property not specified\n", prop_str);
  812. continue;
  813. }
  814. if ((!list_size) || list_size < (num_corners * 2)) {
  815. pr_err("qcom,override-corner%d-addr-val-map property is missed or invalid length: len=%d\n",
  816. i, len);
  817. return -EINVAL;
  818. }
  819. override_max_reg_config_len = list_size / (num_corners * 2);
  820. override_reg_config_list =
  821. corner_acc_reg_config[i].reg_config_list;
  822. if (corner_acc_reg_config[i].max_reg_config_len
  823. != override_max_reg_config_len) {
  824. /* Free already allocate memory */
  825. devm_kfree(mem_acc_vreg->dev, override_reg_config_list);
  826. /* Allocated memory for new requirement */
  827. override_reg_config_list =
  828. devm_kcalloc(mem_acc_vreg->dev,
  829. override_max_reg_config_len * num_corners,
  830. sizeof(*override_reg_config_list), GFP_KERNEL);
  831. if (!override_reg_config_list)
  832. return -ENOMEM;
  833. corner_acc_reg_config[i].max_reg_config_len =
  834. override_max_reg_config_len;
  835. corner_acc_reg_config[i].reg_config_list =
  836. override_reg_config_list;
  837. }
  838. rc = mem_acc_get_reg_addr_val(of_node, prop_str,
  839. override_reg_config_list, tuple_match,
  840. list_size, mem_acc_vreg->num_acc_reg);
  841. if (rc) {
  842. pr_err("Failed to read %s property: rc=%d\n",
  843. prop_str, rc);
  844. return rc;
  845. }
  846. rc = mem_acc_reg_addr_val_dump(mem_acc_vreg,
  847. &corner_acc_reg_config[i], i);
  848. if (rc) {
  849. pr_err("could not dump acc address-value dump for corner=%d: rc=%d\n",
  850. i, rc);
  851. return rc;
  852. }
  853. }
  854. return rc;
  855. }
  856. static int mem_acc_parse_override_config(struct mem_acc_regulator *mem_acc_vreg)
  857. {
  858. struct device_node *of_node = mem_acc_vreg->dev->of_node;
  859. int i, rc = 0;
  860. /* Specify default no match case. */
  861. mem_acc_vreg->override_map_match = FUSE_MAP_NO_MATCH;
  862. mem_acc_vreg->override_map_count = 0;
  863. if (of_find_property(of_node, "qcom,override-fuse-range-map",
  864. NULL)) {
  865. rc = mem_acc_parse_override_fuse_version_range(mem_acc_vreg);
  866. if (rc) {
  867. pr_err("parsing qcom,override-fuse-range-map property failed, rc=%d\n",
  868. rc);
  869. return rc;
  870. }
  871. } else if (of_find_property(of_node, "qcom,override-fuse-version-map",
  872. NULL)) {
  873. rc = mem_acc_parse_override_fuse_version_map(mem_acc_vreg);
  874. if (rc) {
  875. pr_err("parsing qcom,override-fuse-version-map property failed, rc=%d\n",
  876. rc);
  877. return rc;
  878. }
  879. } else {
  880. /* No override fuse configuration defined in device node */
  881. return 0;
  882. }
  883. if (mem_acc_vreg->override_map_match == FUSE_MAP_NO_MATCH)
  884. return 0;
  885. rc = mem_acc_override_corner_map(mem_acc_vreg);
  886. if (rc) {
  887. pr_err("Unable to override corner map rc=%d\n", rc);
  888. return rc;
  889. }
  890. rc = mem_acc_override_reg_addr_val_init(mem_acc_vreg);
  891. if (rc) {
  892. pr_err("Unable to override reg_config_list init rc=%d\n",
  893. rc);
  894. return rc;
  895. }
  896. for (i = 0; i < MEMORY_MAX; i++) {
  897. rc = override_mem_acc_custom_data(mem_acc_vreg, i);
  898. if (rc) {
  899. pr_err("Unable to override custom data for mem_type=%d rc=%d\n",
  900. i, rc);
  901. return rc;
  902. }
  903. }
  904. return rc;
  905. }
  906. static int mem_acc_init_reg_config(struct mem_acc_regulator *mem_acc_vreg)
  907. {
  908. struct device_node *of_node = mem_acc_vreg->dev->of_node;
  909. int i, size, len = 0, rc = 0;
  910. u32 addr_index, reg_val, index;
  911. char *prop_str = "qcom,acc-init-reg-config";
  912. if (!of_find_property(of_node, prop_str, &len)) {
  913. /* Initial acc register configuration not specified */
  914. return rc;
  915. }
  916. size = len / sizeof(u32);
  917. if ((!size) || (size % 2)) {
  918. pr_err("%s specified with invalid length: %d\n",
  919. prop_str, size);
  920. return -EINVAL;
  921. }
  922. for (i = 0; i < size / 2; i++) {
  923. index = i * 2;
  924. rc = of_property_read_u32_index(of_node, prop_str, index,
  925. &addr_index);
  926. rc |= of_property_read_u32_index(of_node, prop_str, index + 1,
  927. &reg_val);
  928. if (rc) {
  929. pr_err("could not read %s at tuple %u: rc=%d\n",
  930. prop_str, index, rc);
  931. return rc;
  932. }
  933. if ((!addr_index) || addr_index > mem_acc_vreg->num_acc_reg) {
  934. pr_err("Invalid register index %u in %s at tuple %u\n",
  935. addr_index, prop_str, index);
  936. return -EINVAL;
  937. }
  938. writel_relaxed(reg_val,
  939. mem_acc_vreg->remap_reg_addr_list[addr_index]);
  940. /* make sure write complete */
  941. mb();
  942. pr_debug("acc initial config: register:0x%x value:0x%x\n",
  943. mem_acc_vreg->phys_reg_addr_list[addr_index], reg_val);
  944. }
  945. return rc;
  946. }
  947. static int mem_acc_get_reg_addr(struct mem_acc_regulator *mem_acc_vreg)
  948. {
  949. struct device_node *of_node = mem_acc_vreg->dev->of_node;
  950. void __iomem **remap_reg_addr_list;
  951. u32 *phys_reg_addr_list;
  952. int i, num_acc_reg, len = 0, rc = 0;
  953. if (!of_find_property(of_node, "qcom,acc-reg-addr-list", &len)) {
  954. /* acc register address list not specified */
  955. return rc;
  956. }
  957. num_acc_reg = len / sizeof(u32);
  958. if (!num_acc_reg) {
  959. pr_err("qcom,acc-reg-addr-list has invalid len = %d\n", len);
  960. return -EINVAL;
  961. }
  962. phys_reg_addr_list = devm_kcalloc(mem_acc_vreg->dev, num_acc_reg + 1,
  963. sizeof(*phys_reg_addr_list), GFP_KERNEL);
  964. if (!phys_reg_addr_list)
  965. return -ENOMEM;
  966. remap_reg_addr_list = devm_kcalloc(mem_acc_vreg->dev, num_acc_reg + 1,
  967. sizeof(*remap_reg_addr_list), GFP_KERNEL);
  968. if (!remap_reg_addr_list)
  969. return -ENOMEM;
  970. rc = of_property_read_u32_array(of_node, "qcom,acc-reg-addr-list",
  971. &phys_reg_addr_list[1], num_acc_reg);
  972. if (rc) {
  973. pr_err("Read- qcom,acc-reg-addr-list failed: rc=%d\n", rc);
  974. return rc;
  975. }
  976. for (i = 1; i <= num_acc_reg; i++) {
  977. remap_reg_addr_list[i] = devm_ioremap(mem_acc_vreg->dev,
  978. phys_reg_addr_list[i], 0x4);
  979. if (!remap_reg_addr_list[i]) {
  980. pr_err("Unable to map register address 0x%x\n",
  981. phys_reg_addr_list[i]);
  982. return -EINVAL;
  983. }
  984. }
  985. mem_acc_vreg->num_acc_reg = num_acc_reg;
  986. mem_acc_vreg->phys_reg_addr_list = phys_reg_addr_list;
  987. mem_acc_vreg->remap_reg_addr_list = remap_reg_addr_list;
  988. return rc;
  989. }
  990. static int mem_acc_reg_config_init(struct mem_acc_regulator *mem_acc_vreg)
  991. {
  992. struct device_node *of_node = mem_acc_vreg->dev->of_node;
  993. struct acc_reg_value *reg_config_list;
  994. int len, size, rc, i, num_corners;
  995. struct property *prop;
  996. char prop_str[30];
  997. struct corner_acc_reg_config *corner_acc_reg_config;
  998. rc = of_property_read_u32(of_node, "qcom,num-acc-corners",
  999. &num_corners);
  1000. if (rc) {
  1001. pr_err("could not read qcom,num-acc-corners: rc=%d\n", rc);
  1002. return rc;
  1003. }
  1004. mem_acc_vreg->num_corners = num_corners;
  1005. rc = of_property_read_u32(of_node, "qcom,boot-acc-corner",
  1006. &mem_acc_vreg->corner);
  1007. if (rc) {
  1008. pr_err("could not read qcom,boot-acc-corner: rc=%d\n", rc);
  1009. return rc;
  1010. }
  1011. pr_debug("boot acc corner = %d\n", mem_acc_vreg->corner);
  1012. corner_acc_reg_config = devm_kcalloc(mem_acc_vreg->dev, num_corners + 1,
  1013. sizeof(*corner_acc_reg_config),
  1014. GFP_KERNEL);
  1015. if (!corner_acc_reg_config)
  1016. return -ENOMEM;
  1017. for (i = 1; i <= num_corners; i++) {
  1018. snprintf(prop_str, sizeof(prop_str),
  1019. "qcom,corner%d-reg-config", i);
  1020. prop = of_find_property(of_node, prop_str, &len);
  1021. size = len / sizeof(u32);
  1022. if ((!prop) || (!size) || size < (num_corners * 2)) {
  1023. pr_err("%s property is missed or invalid length: len=%d\n",
  1024. prop_str, len);
  1025. return -EINVAL;
  1026. }
  1027. reg_config_list = devm_kcalloc(mem_acc_vreg->dev, size / 2,
  1028. sizeof(*reg_config_list), GFP_KERNEL);
  1029. if (!reg_config_list)
  1030. return -ENOMEM;
  1031. rc = mem_acc_get_reg_addr_val(of_node, prop_str,
  1032. reg_config_list, 0, size,
  1033. mem_acc_vreg->num_acc_reg);
  1034. if (rc) {
  1035. pr_err("Failed to read %s property: rc=%d\n",
  1036. prop_str, rc);
  1037. return rc;
  1038. }
  1039. corner_acc_reg_config[i].max_reg_config_len =
  1040. size / (num_corners * 2);
  1041. corner_acc_reg_config[i].reg_config_list = reg_config_list;
  1042. rc = mem_acc_reg_addr_val_dump(mem_acc_vreg,
  1043. &corner_acc_reg_config[i], i);
  1044. if (rc) {
  1045. pr_err("could not dump acc address-value dump for corner=%d: rc=%d\n",
  1046. i, rc);
  1047. return rc;
  1048. }
  1049. }
  1050. mem_acc_vreg->corner_acc_reg_config = corner_acc_reg_config;
  1051. mem_acc_vreg->flags |= MEM_ACC_USE_ADDR_VAL_MAP;
  1052. return rc;
  1053. }
  1054. #define MEM_TYPE_STRING_LEN 20
  1055. static int mem_acc_init(struct platform_device *pdev,
  1056. struct mem_acc_regulator *mem_acc_vreg)
  1057. {
  1058. struct device_node *of_node = pdev->dev.of_node;
  1059. struct resource *res;
  1060. int len, rc, i, j;
  1061. bool acc_type_present = false;
  1062. char tmps[MEM_TYPE_STRING_LEN];
  1063. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "acc-en");
  1064. if (!res || !res->start) {
  1065. pr_debug("'acc-en' resource missing or not used.\n");
  1066. } else {
  1067. mem_acc_vreg->acc_en_addr = res->start;
  1068. len = res->end - res->start + 1;
  1069. pr_debug("'acc_en_addr' = %pa (len=0x%x)\n", &res->start, len);
  1070. mem_acc_vreg->acc_en_base = devm_ioremap(mem_acc_vreg->dev,
  1071. mem_acc_vreg->acc_en_addr, len);
  1072. if (!mem_acc_vreg->acc_en_base) {
  1073. pr_err("Unable to map 'acc_en_addr' %pa\n",
  1074. &mem_acc_vreg->acc_en_addr);
  1075. return -EINVAL;
  1076. }
  1077. rc = populate_acc_data(mem_acc_vreg, "qcom,acc-en-bit-pos",
  1078. &mem_acc_vreg->acc_en_bit_pos,
  1079. &mem_acc_vreg->num_acc_en);
  1080. if (rc) {
  1081. pr_err("Unable to populate 'qcom,acc-en-bit-pos' rc=%d\n",
  1082. rc);
  1083. return rc;
  1084. }
  1085. }
  1086. rc = mem_acc_efuse_init(pdev, mem_acc_vreg);
  1087. if (rc) {
  1088. pr_err("Wrong eFuse address specified: rc=%d\n", rc);
  1089. return rc;
  1090. }
  1091. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "acc-sel-l1");
  1092. if (!res || !res->start) {
  1093. pr_debug("'acc-sel-l1' resource missing or not used.\n");
  1094. } else {
  1095. rc = mem_acc_sel_setup(mem_acc_vreg, res, MEMORY_L1);
  1096. if (rc) {
  1097. pr_err("Unable to setup mem-acc for mem_type=%d rc=%d\n",
  1098. MEMORY_L1, rc);
  1099. return rc;
  1100. }
  1101. mem_acc_vreg->mem_acc_supported[MEMORY_L1] = true;
  1102. }
  1103. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "acc-sel-l2");
  1104. if (!res || !res->start) {
  1105. pr_debug("'acc-sel-l2' resource missing or not used.\n");
  1106. } else {
  1107. rc = mem_acc_sel_setup(mem_acc_vreg, res, MEMORY_L2);
  1108. if (rc) {
  1109. pr_err("Unable to setup mem-acc for mem_type=%d rc=%d\n",
  1110. MEMORY_L2, rc);
  1111. return rc;
  1112. }
  1113. mem_acc_vreg->mem_acc_supported[MEMORY_L2] = true;
  1114. }
  1115. for (i = 0; i < MEM_ACC_TYPE_MAX; i++) {
  1116. snprintf(tmps, MEM_TYPE_STRING_LEN, "mem-acc-type%d", i + 1);
  1117. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, tmps);
  1118. if (!res || !res->start) {
  1119. pr_debug("'%s' resource missing or not used.\n", tmps);
  1120. } else {
  1121. mem_acc_vreg->mem_acc_type_addr[i] = res->start;
  1122. acc_type_present = true;
  1123. }
  1124. }
  1125. rc = mem_acc_get_reg_addr(mem_acc_vreg);
  1126. if (rc) {
  1127. pr_err("Unable to get acc register addresses: rc=%d\n", rc);
  1128. return rc;
  1129. }
  1130. if (mem_acc_vreg->phys_reg_addr_list) {
  1131. rc = mem_acc_reg_config_init(mem_acc_vreg);
  1132. if (rc) {
  1133. pr_err("acc register address-value map failed: rc=%d\n",
  1134. rc);
  1135. return rc;
  1136. }
  1137. }
  1138. if (of_find_property(of_node, "qcom,corner-acc-map", NULL)) {
  1139. rc = populate_acc_data(mem_acc_vreg, "qcom,corner-acc-map",
  1140. &mem_acc_vreg->corner_acc_map,
  1141. &mem_acc_vreg->num_corners);
  1142. /* Check if at least one valid mem-acc config. is specified */
  1143. for (i = 0; i < MEMORY_MAX; i++) {
  1144. if (mem_acc_vreg->mem_acc_supported[i])
  1145. break;
  1146. }
  1147. if (i == MEMORY_MAX && !acc_type_present) {
  1148. pr_err("No mem-acc configuration specified\n");
  1149. return -EINVAL;
  1150. }
  1151. mem_acc_vreg->flags |= MEM_ACC_USE_CORNER_ACC_MAP;
  1152. }
  1153. if ((mem_acc_vreg->flags & MEM_ACC_USE_CORNER_ACC_MAP) &&
  1154. (mem_acc_vreg->flags & MEM_ACC_USE_ADDR_VAL_MAP)) {
  1155. pr_err("Invalid configuration, both qcom,corner-acc-map and qcom,cornerX-addr-val-map specified\n");
  1156. return -EINVAL;
  1157. }
  1158. pr_debug("num_corners = %d\n", mem_acc_vreg->num_corners);
  1159. if (mem_acc_vreg->num_acc_en)
  1160. mem_acc_en_init(mem_acc_vreg);
  1161. if (mem_acc_vreg->phys_reg_addr_list) {
  1162. rc = mem_acc_init_reg_config(mem_acc_vreg);
  1163. if (rc) {
  1164. pr_err("acc initial register configuration failed: rc=%d\n",
  1165. rc);
  1166. return rc;
  1167. }
  1168. }
  1169. rc = mem_acc_sel_init(mem_acc_vreg);
  1170. if (rc) {
  1171. pr_err("Unable to initialize mem_acc_sel reg rc=%d\n", rc);
  1172. return rc;
  1173. }
  1174. for (i = 0; i < MEMORY_MAX; i++) {
  1175. rc = mem_acc_custom_data_init(pdev, mem_acc_vreg, i);
  1176. if (rc) {
  1177. pr_err("Unable to initialize custom data for mem_type=%d rc=%d\n",
  1178. i, rc);
  1179. return rc;
  1180. }
  1181. }
  1182. rc = mem_acc_parse_override_config(mem_acc_vreg);
  1183. if (rc) {
  1184. pr_err("Unable to parse mem acc override configuration, rc=%d\n",
  1185. rc);
  1186. return rc;
  1187. }
  1188. if (acc_type_present) {
  1189. mem_acc_vreg->mem_acc_type_data = devm_kzalloc(
  1190. mem_acc_vreg->dev, mem_acc_vreg->num_corners *
  1191. MEM_ACC_TYPE_MAX * sizeof(u32), GFP_KERNEL);
  1192. if (!mem_acc_vreg->mem_acc_type_data) {
  1193. pr_err("Unable to allocate memory for mem_acc_type\n");
  1194. return -ENOMEM;
  1195. }
  1196. for (i = 0; i < MEM_ACC_TYPE_MAX; i++) {
  1197. if (mem_acc_vreg->mem_acc_type_addr[i]) {
  1198. snprintf(tmps, MEM_TYPE_STRING_LEN,
  1199. "qcom,mem-acc-type%d", i + 1);
  1200. j = i * mem_acc_vreg->num_corners;
  1201. rc = of_property_read_u32_array(
  1202. mem_acc_vreg->dev->of_node,
  1203. tmps,
  1204. &mem_acc_vreg->mem_acc_type_data[j],
  1205. mem_acc_vreg->num_corners);
  1206. if (rc) {
  1207. pr_err("Unable to get property %s rc=%d\n",
  1208. tmps, rc);
  1209. return rc;
  1210. }
  1211. }
  1212. }
  1213. }
  1214. return 0;
  1215. }
  1216. static int mem_acc_regulator_probe(struct platform_device *pdev)
  1217. {
  1218. struct regulator_config reg_config = {};
  1219. struct mem_acc_regulator *mem_acc_vreg;
  1220. struct regulator_desc *rdesc;
  1221. struct regulator_init_data *init_data;
  1222. int rc;
  1223. if (!pdev->dev.of_node) {
  1224. pr_err("Device tree node is missing\n");
  1225. return -EINVAL;
  1226. }
  1227. init_data = of_get_regulator_init_data(&pdev->dev, pdev->dev.of_node,
  1228. NULL);
  1229. if (!init_data) {
  1230. pr_err("regulator init data is missing\n");
  1231. return -EINVAL;
  1232. }
  1233. init_data->constraints.input_uV = init_data->constraints.max_uV;
  1234. init_data->constraints.valid_ops_mask |= REGULATOR_CHANGE_VOLTAGE;
  1235. mem_acc_vreg = devm_kzalloc(&pdev->dev, sizeof(*mem_acc_vreg),
  1236. GFP_KERNEL);
  1237. if (!mem_acc_vreg)
  1238. return -ENOMEM;
  1239. mem_acc_vreg->dev = &pdev->dev;
  1240. rc = mem_acc_init(pdev, mem_acc_vreg);
  1241. if (rc) {
  1242. pr_err("Unable to initialize mem_acc configuration rc=%d\n",
  1243. rc);
  1244. return rc;
  1245. }
  1246. rdesc = &mem_acc_vreg->rdesc;
  1247. rdesc->owner = THIS_MODULE;
  1248. rdesc->type = REGULATOR_VOLTAGE;
  1249. rdesc->ops = &mem_acc_corner_ops;
  1250. rdesc->name = init_data->constraints.name;
  1251. reg_config.dev = &pdev->dev;
  1252. reg_config.init_data = init_data;
  1253. reg_config.driver_data = mem_acc_vreg;
  1254. reg_config.of_node = pdev->dev.of_node;
  1255. mem_acc_vreg->rdev = regulator_register(rdesc, &reg_config);
  1256. if (IS_ERR(mem_acc_vreg->rdev)) {
  1257. rc = PTR_ERR(mem_acc_vreg->rdev);
  1258. if (rc != -EPROBE_DEFER)
  1259. pr_err("regulator_register failed: rc=%d\n", rc);
  1260. return rc;
  1261. }
  1262. platform_set_drvdata(pdev, mem_acc_vreg);
  1263. return 0;
  1264. }
  1265. static int mem_acc_regulator_remove(struct platform_device *pdev)
  1266. {
  1267. struct mem_acc_regulator *mem_acc_vreg = platform_get_drvdata(pdev);
  1268. regulator_unregister(mem_acc_vreg->rdev);
  1269. return 0;
  1270. }
  1271. static const struct of_device_id mem_acc_regulator_match_table[] = {
  1272. { .compatible = "qcom,mem-acc-regulator", },
  1273. {}
  1274. };
  1275. static struct platform_driver mem_acc_regulator_driver = {
  1276. .probe = mem_acc_regulator_probe,
  1277. .remove = mem_acc_regulator_remove,
  1278. .driver = {
  1279. .name = "qcom,mem-acc-regulator",
  1280. .of_match_table = mem_acc_regulator_match_table,
  1281. .owner = THIS_MODULE,
  1282. },
  1283. };
  1284. int __init mem_acc_regulator_init(void)
  1285. {
  1286. return platform_driver_register(&mem_acc_regulator_driver);
  1287. }
  1288. postcore_initcall(mem_acc_regulator_init);
  1289. static void __exit mem_acc_regulator_exit(void)
  1290. {
  1291. platform_driver_unregister(&mem_acc_regulator_driver);
  1292. }
  1293. module_exit(mem_acc_regulator_exit);
  1294. MODULE_DESCRIPTION("MEM-ACC-SEL regulator driver");
  1295. MODULE_LICENSE("GPL v2");