blob: 6fb7ba272e0956f51ce5042d4d1c41eaaf89b777 [file] [log] [blame]
Guodong Xu8bdf87b2014-09-01 16:28:34 +08001/*
Guodong Xuec588712017-07-20 15:32:42 +08002 * Device driver for Hi6421 PMIC
Guodong Xu8bdf87b2014-09-01 16:28:34 +08003 *
4 * Copyright (c) <2011-2014> HiSilicon Technologies Co., Ltd.
5 * http://www.hisilicon.com
Guodong Xuec588712017-07-20 15:32:42 +08006 * Copyright (c) <2013-2017> Linaro Ltd.
Guodong Xu8bdf87b2014-09-01 16:28:34 +08007 * http://www.linaro.org
8 *
9 * Author: Guodong Xu <guodong.xu@linaro.org>
10 *
11 * This program is free software; you can redistribute it and/or modify
Guodong Xu876368c2017-07-20 15:32:40 +080012 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
Guodong Xu8bdf87b2014-09-01 16:28:34 +080014 */
15
16#include <linux/device.h>
17#include <linux/err.h>
18#include <linux/mfd/core.h>
Guodong Xuec588712017-07-20 15:32:42 +080019#include <linux/mfd/hi6421-pmic.h>
Guodong Xu8bdf87b2014-09-01 16:28:34 +080020#include <linux/module.h>
Guodong Xuec588712017-07-20 15:32:42 +080021#include <linux/of_device.h>
Guodong Xu8bdf87b2014-09-01 16:28:34 +080022#include <linux/platform_device.h>
23#include <linux/regmap.h>
Guodong Xu8bdf87b2014-09-01 16:28:34 +080024
25static const struct mfd_cell hi6421_devs[] = {
26 { .name = "hi6421-regulator", },
27};
28
Guodong Xuec588712017-07-20 15:32:42 +080029static const struct mfd_cell hi6421v530_devs[] = {
30 { .name = "hi6421v530-regulator", },
31};
32
Krzysztof Kozlowski7f9e3fe2015-01-05 10:01:21 +010033static const struct regmap_config hi6421_regmap_config = {
Guodong Xu8bdf87b2014-09-01 16:28:34 +080034 .reg_bits = 32,
35 .reg_stride = 4,
36 .val_bits = 8,
37 .max_register = HI6421_REG_TO_BUS_ADDR(HI6421_REG_MAX),
38};
39
Guodong Xuec588712017-07-20 15:32:42 +080040static const struct of_device_id of_hi6421_pmic_match[] = {
41 {
42 .compatible = "hisilicon,hi6421-pmic",
43 .data = (void *)HI6421
44 },
45 {
46 .compatible = "hisilicon,hi6421v530-pmic",
47 .data = (void *)HI6421_V530
48 },
49 { },
50};
51MODULE_DEVICE_TABLE(of, of_hi6421_pmic_match);
52
Guodong Xu8bdf87b2014-09-01 16:28:34 +080053static int hi6421_pmic_probe(struct platform_device *pdev)
54{
55 struct hi6421_pmic *pmic;
56 struct resource *res;
Guodong Xuec588712017-07-20 15:32:42 +080057 const struct of_device_id *id;
58 const struct mfd_cell *subdevs;
59 enum hi6421_type type;
Guodong Xu8bdf87b2014-09-01 16:28:34 +080060 void __iomem *base;
Guodong Xuec588712017-07-20 15:32:42 +080061 int n_subdevs, ret;
62
63 id = of_match_device(of_hi6421_pmic_match, &pdev->dev);
64 if (!id)
65 return -EINVAL;
66 type = (enum hi6421_type)id->data;
Guodong Xu8bdf87b2014-09-01 16:28:34 +080067
68 pmic = devm_kzalloc(&pdev->dev, sizeof(*pmic), GFP_KERNEL);
69 if (!pmic)
70 return -ENOMEM;
71
72 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
73 base = devm_ioremap_resource(&pdev->dev, res);
74 if (IS_ERR(base))
75 return PTR_ERR(base);
76
77 pmic->regmap = devm_regmap_init_mmio_clk(&pdev->dev, NULL, base,
78 &hi6421_regmap_config);
79 if (IS_ERR(pmic->regmap)) {
Guodong Xu568e5472017-07-20 15:32:41 +080080 dev_err(&pdev->dev, "Failed to initialise Regmap: %ld\n",
81 PTR_ERR(pmic->regmap));
Guodong Xu8bdf87b2014-09-01 16:28:34 +080082 return PTR_ERR(pmic->regmap);
83 }
84
Guodong Xuec588712017-07-20 15:32:42 +080085 platform_set_drvdata(pdev, pmic);
86
87 switch (type) {
88 case HI6421:
89 /* set over-current protection debounce 8ms */
90 regmap_update_bits(pmic->regmap, HI6421_OCP_DEB_CTRL_REG,
Guodong Xu8bdf87b2014-09-01 16:28:34 +080091 (HI6421_OCP_DEB_SEL_MASK
92 | HI6421_OCP_EN_DEBOUNCE_MASK
93 | HI6421_OCP_AUTO_STOP_MASK),
94 (HI6421_OCP_DEB_SEL_8MS
95 | HI6421_OCP_EN_DEBOUNCE_ENABLE));
96
Guodong Xuec588712017-07-20 15:32:42 +080097 subdevs = hi6421_devs;
98 n_subdevs = ARRAY_SIZE(hi6421_devs);
99 break;
100 case HI6421_V530:
101 subdevs = hi6421v530_devs;
102 n_subdevs = ARRAY_SIZE(hi6421v530_devs);
103 break;
104 default:
105 dev_err(&pdev->dev, "Unknown device type %d\n",
106 (unsigned int)type);
107 return -EINVAL;
108 }
Guodong Xu8bdf87b2014-09-01 16:28:34 +0800109
Guodong Xuec588712017-07-20 15:32:42 +0800110 ret = devm_mfd_add_devices(&pdev->dev, PLATFORM_DEVID_NONE,
111 subdevs, n_subdevs, NULL, 0, NULL);
Guodong Xu8bdf87b2014-09-01 16:28:34 +0800112 if (ret) {
Guodong Xu568e5472017-07-20 15:32:41 +0800113 dev_err(&pdev->dev, "Failed to add child devices: %d\n", ret);
Guodong Xu8bdf87b2014-09-01 16:28:34 +0800114 return ret;
115 }
116
117 return 0;
118}
119
Guodong Xu8bdf87b2014-09-01 16:28:34 +0800120static struct platform_driver hi6421_pmic_driver = {
121 .driver = {
Guodong Xuec588712017-07-20 15:32:42 +0800122 .name = "hi6421_pmic",
123 .of_match_table = of_hi6421_pmic_match,
Guodong Xu8bdf87b2014-09-01 16:28:34 +0800124 },
125 .probe = hi6421_pmic_probe,
Guodong Xu8bdf87b2014-09-01 16:28:34 +0800126};
127module_platform_driver(hi6421_pmic_driver);
128
129MODULE_AUTHOR("Guodong Xu <guodong.xu@linaro.org>");
130MODULE_DESCRIPTION("Hi6421 PMIC driver");
131MODULE_LICENSE("GPL v2");