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