blob: e73418b181052c327b0be7889885ccaa33b9e019 [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001// SPDX-License-Identifier: GPL-2.0
2//
3// Copyright (c) 2019 MediaTek Inc.
4
5#include <linux/mfd/mt6397/core.h>
6#include <linux/module.h>
7#include <linux/of_device.h>
8#include <linux/platform_device.h>
9#include <linux/regmap.h>
10
11struct mt63xx_consumer_data {
12 struct mutex lock;
13 struct regmap *regmap;
14 unsigned int reg_value;
15};
16
17static ssize_t pmic_access_show(struct device *dev,
18 struct device_attribute *attr,
19 char *buf)
20{
21 struct mt63xx_consumer_data *data = dev_get_drvdata(dev);
22
23 pr_info("[%s] 0x%x\n", __func__, data->reg_value);
24 return sprintf(buf, "0x%x\n", data->reg_value);
25}
26
27static ssize_t pmic_access_store(struct device *dev,
28 struct device_attribute *attr,
29 const char *buf,
30 size_t size)
31{
32 struct mt63xx_consumer_data *data;
33 int ret = 0;
34 char *pvalue = NULL, *addr, *val;
35 unsigned int reg_val = 0;
36 unsigned int reg_adr = 0;
37
38 if (dev) {
39 data = dev_get_drvdata(dev);
40 if (!data)
41 return -ENODEV;
42 } else
43 return -ENODEV;
44
45 if (buf != NULL && size != 0) {
46 pr_info("[%s] size is %d, buf is %s\n", __func__,
47 (int)size, buf);
48
49 pvalue = (char *)buf;
50 addr = strsep(&pvalue, " ");
51 val = strsep(&pvalue, " ");
52 if (addr)
53 ret = kstrtou32(addr, 16, (unsigned int *)&reg_adr);
54 mutex_lock(&data->lock);
55 if (val) {
56 ret = kstrtou32(val, 16, (unsigned int *)&reg_val);
57 ret = regmap_write(data->regmap, reg_adr, reg_val);
58 } else {
59 ret = regmap_read(data->regmap,
60 reg_adr, &data->reg_value);
61 }
62 mutex_unlock(&data->lock);
63 pr_info("%s PMIC Reg[0x%x]=0x%x!\n",
64 (val ? "write" : "read"), reg_adr,
65 (val ? reg_val : data->reg_value));
66 }
67 return size;
68}
69static DEVICE_ATTR_RW(pmic_access);
70
71static int mt63xx_debug_probe(struct platform_device *pdev)
72{
73 struct mt6397_chip *chip = dev_get_drvdata(pdev->dev.parent);
74 struct mt63xx_consumer_data *drvdata;
75
76 drvdata = devm_kzalloc(&pdev->dev, sizeof(struct mt63xx_consumer_data),
77 GFP_KERNEL);
78 if (!drvdata)
79 return -ENOMEM;
80
81 mutex_init(&drvdata->lock);
82 drvdata->regmap = chip->regmap;
83 platform_set_drvdata(pdev, drvdata);
84
85 /* Create sysfs entry */
86 device_create_file(&pdev->dev, &dev_attr_pmic_access);
87
88 return 0;
89}
90
91static int mt63xx_debug_remove(struct platform_device *pdev)
92{
93 device_remove_file(&pdev->dev, &dev_attr_pmic_access);
94
95 return 0;
96}
97
98static const struct of_device_id mt63xx_debug_of_match[] = {
99 {
100 .compatible = "mediatek,mt63xx-debug",
101 }, {
102 /* sentinel */
103 }
104};
105MODULE_DEVICE_TABLE(of, mt63xx_debug_of_match);
106
107static struct platform_driver mt63xx_debug_driver = {
108 .driver = {
109 .name = "mt63xx-debug",
110 .of_match_table = mt63xx_debug_of_match,
111 },
112 .probe = mt63xx_debug_probe,
113 .remove = mt63xx_debug_remove,
114};
115
116module_platform_driver(mt63xx_debug_driver);
117
118MODULE_AUTHOR("Wen Su <wen.su@mediatek.com>");
119MODULE_DESCRIPTION("Debug driver for MediaTek MT63xx PMIC");
120MODULE_LICENSE("GPL");