1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Device driver for Hi6421 PMIC
4  *
5  * Copyright (c) <2011-2014> HiSilicon Technologies Co., Ltd.
6  *              http://www.hisilicon.com
7  * Copyright (c) <2013-2017> Linaro Ltd.
8  *              https://www.linaro.org
9  *
10  * Author: Guodong Xu <guodong.xu@linaro.org>
11  */
12 
13 #include <linux/device.h>
14 #include <linux/err.h>
15 #include <linux/mfd/core.h>
16 #include <linux/mfd/hi6421-pmic.h>
17 #include <linux/module.h>
18 #include <linux/of_device.h>
19 #include <linux/platform_device.h>
20 #include <linux/regmap.h>
21 
22 static const struct mfd_cell hi6421_devs[] = {
23 	{ .name = "hi6421-regulator", },
24 };
25 
26 static const struct mfd_cell hi6421v530_devs[] = {
27 	{ .name = "hi6421v530-regulator", },
28 };
29 
30 static const struct regmap_config hi6421_regmap_config = {
31 	.reg_bits = 32,
32 	.reg_stride = 4,
33 	.val_bits = 8,
34 	.max_register = HI6421_REG_TO_BUS_ADDR(HI6421_REG_MAX),
35 };
36 
37 static const struct of_device_id of_hi6421_pmic_match[] = {
38 	{
39 		.compatible = "hisilicon,hi6421-pmic",
40 		.data = (void *)HI6421
41 	},
42 	{
43 		.compatible = "hisilicon,hi6421v530-pmic",
44 		.data = (void *)HI6421_V530
45 	},
46 	{ },
47 };
48 MODULE_DEVICE_TABLE(of, of_hi6421_pmic_match);
49 
50 static int hi6421_pmic_probe(struct platform_device *pdev)
51 {
52 	struct hi6421_pmic *pmic;
53 	const struct of_device_id *id;
54 	const struct mfd_cell *subdevs;
55 	enum hi6421_type type;
56 	void __iomem *base;
57 	int n_subdevs, ret;
58 
59 	id = of_match_device(of_hi6421_pmic_match, &pdev->dev);
60 	if (!id)
61 		return -EINVAL;
62 	type = (uintptr_t)id->data;
63 
64 	pmic = devm_kzalloc(&pdev->dev, sizeof(*pmic), GFP_KERNEL);
65 	if (!pmic)
66 		return -ENOMEM;
67 
68 	base = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
69 	if (IS_ERR(base))
70 		return PTR_ERR(base);
71 
72 	pmic->regmap = devm_regmap_init_mmio_clk(&pdev->dev, NULL, base,
73 						 &hi6421_regmap_config);
74 	if (IS_ERR(pmic->regmap)) {
75 		dev_err(&pdev->dev, "Failed to initialise Regmap: %ld\n",
76 						PTR_ERR(pmic->regmap));
77 		return PTR_ERR(pmic->regmap);
78 	}
79 
80 	platform_set_drvdata(pdev, pmic);
81 
82 	switch (type) {
83 	case HI6421:
84 		/* set over-current protection debounce 8ms */
85 		regmap_update_bits(pmic->regmap, HI6421_OCP_DEB_CTRL_REG,
86 				(HI6421_OCP_DEB_SEL_MASK
87 				 | HI6421_OCP_EN_DEBOUNCE_MASK
88 				 | HI6421_OCP_AUTO_STOP_MASK),
89 				(HI6421_OCP_DEB_SEL_8MS
90 				 | HI6421_OCP_EN_DEBOUNCE_ENABLE));
91 
92 		subdevs = hi6421_devs;
93 		n_subdevs = ARRAY_SIZE(hi6421_devs);
94 		break;
95 	case HI6421_V530:
96 		subdevs = hi6421v530_devs;
97 		n_subdevs = ARRAY_SIZE(hi6421v530_devs);
98 		break;
99 	default:
100 		dev_err(&pdev->dev, "Unknown device type %d\n",
101 						(unsigned int)type);
102 		return -EINVAL;
103 	}
104 
105 	ret = devm_mfd_add_devices(&pdev->dev, PLATFORM_DEVID_NONE,
106 				   subdevs, n_subdevs, NULL, 0, NULL);
107 	if (ret) {
108 		dev_err(&pdev->dev, "Failed to add child devices: %d\n", ret);
109 		return ret;
110 	}
111 
112 	return 0;
113 }
114 
115 static struct platform_driver hi6421_pmic_driver = {
116 	.driver = {
117 		.name = "hi6421_pmic",
118 		.of_match_table = of_hi6421_pmic_match,
119 	},
120 	.probe	= hi6421_pmic_probe,
121 };
122 module_platform_driver(hi6421_pmic_driver);
123 
124 MODULE_AUTHOR("Guodong Xu <guodong.xu@linaro.org>");
125 MODULE_DESCRIPTION("Hi6421 PMIC driver");
126 MODULE_LICENSE("GPL v2");
127