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