Axel Lin | c5e911a | 2019-04-29 19:35:42 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | // |
| 3 | // Copyright (C) 2012 ARM Limited |
Pawel Moll | 31e5408 | 2012-09-24 18:56:54 +0100 | [diff] [blame] | 4 | |
| 5 | #define DRVNAME "vexpress-regulator" |
| 6 | #define pr_fmt(fmt) DRVNAME ": " fmt |
| 7 | |
| 8 | #include <linux/device.h> |
| 9 | #include <linux/err.h> |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/of_device.h> |
| 12 | #include <linux/regulator/driver.h> |
| 13 | #include <linux/regulator/machine.h> |
| 14 | #include <linux/regulator/of_regulator.h> |
| 15 | #include <linux/vexpress.h> |
| 16 | |
Pawel Moll | 31e5408 | 2012-09-24 18:56:54 +0100 | [diff] [blame] | 17 | static int vexpress_regulator_get_voltage(struct regulator_dev *regdev) |
| 18 | { |
Axel Lin | eeb1b23 | 2019-04-29 19:35:41 +0800 | [diff] [blame] | 19 | unsigned int uV; |
| 20 | int err = regmap_read(regdev->regmap, 0, &uV); |
Pawel Moll | 31e5408 | 2012-09-24 18:56:54 +0100 | [diff] [blame] | 21 | |
| 22 | return err ? err : uV; |
| 23 | } |
| 24 | |
| 25 | static int vexpress_regulator_set_voltage(struct regulator_dev *regdev, |
| 26 | int min_uV, int max_uV, unsigned *selector) |
| 27 | { |
Axel Lin | eeb1b23 | 2019-04-29 19:35:41 +0800 | [diff] [blame] | 28 | return regmap_write(regdev->regmap, 0, min_uV); |
Pawel Moll | 31e5408 | 2012-09-24 18:56:54 +0100 | [diff] [blame] | 29 | } |
| 30 | |
Axel Lin | ab54a4d | 2019-04-11 00:19:37 +0800 | [diff] [blame] | 31 | static const struct regulator_ops vexpress_regulator_ops_ro = { |
Pawel Moll | 31e5408 | 2012-09-24 18:56:54 +0100 | [diff] [blame] | 32 | .get_voltage = vexpress_regulator_get_voltage, |
| 33 | }; |
| 34 | |
Axel Lin | ab54a4d | 2019-04-11 00:19:37 +0800 | [diff] [blame] | 35 | static const struct regulator_ops vexpress_regulator_ops = { |
Pawel Moll | 31e5408 | 2012-09-24 18:56:54 +0100 | [diff] [blame] | 36 | .get_voltage = vexpress_regulator_get_voltage, |
| 37 | .set_voltage = vexpress_regulator_set_voltage, |
| 38 | }; |
| 39 | |
| 40 | static int vexpress_regulator_probe(struct platform_device *pdev) |
| 41 | { |
Axel Lin | eeb1b23 | 2019-04-29 19:35:41 +0800 | [diff] [blame] | 42 | struct regulator_desc *desc; |
Pawel Moll | 31e5408 | 2012-09-24 18:56:54 +0100 | [diff] [blame] | 43 | struct regulator_init_data *init_data; |
| 44 | struct regulator_config config = { }; |
Axel Lin | eeb1b23 | 2019-04-29 19:35:41 +0800 | [diff] [blame] | 45 | struct regulator_dev *rdev; |
| 46 | struct regmap *regmap; |
Pawel Moll | 31e5408 | 2012-09-24 18:56:54 +0100 | [diff] [blame] | 47 | |
Axel Lin | eeb1b23 | 2019-04-29 19:35:41 +0800 | [diff] [blame] | 48 | desc = devm_kzalloc(&pdev->dev, sizeof(*desc), GFP_KERNEL); |
| 49 | if (!desc) |
Pawel Moll | 3b9334a | 2014-04-30 16:46:29 +0100 | [diff] [blame] | 50 | return -ENOMEM; |
Pawel Moll | 31e5408 | 2012-09-24 18:56:54 +0100 | [diff] [blame] | 51 | |
Axel Lin | eeb1b23 | 2019-04-29 19:35:41 +0800 | [diff] [blame] | 52 | regmap = devm_regmap_init_vexpress_config(&pdev->dev); |
| 53 | if (IS_ERR(regmap)) |
| 54 | return PTR_ERR(regmap); |
Pawel Moll | 31e5408 | 2012-09-24 18:56:54 +0100 | [diff] [blame] | 55 | |
Axel Lin | eeb1b23 | 2019-04-29 19:35:41 +0800 | [diff] [blame] | 56 | desc->name = dev_name(&pdev->dev); |
| 57 | desc->type = REGULATOR_VOLTAGE; |
| 58 | desc->owner = THIS_MODULE; |
| 59 | desc->continuous_voltage_range = true; |
Pawel Moll | 31e5408 | 2012-09-24 18:56:54 +0100 | [diff] [blame] | 60 | |
Javier Martinez Canillas | 072e78b | 2014-11-10 14:43:53 +0100 | [diff] [blame] | 61 | init_data = of_get_regulator_init_data(&pdev->dev, pdev->dev.of_node, |
Axel Lin | eeb1b23 | 2019-04-29 19:35:41 +0800 | [diff] [blame] | 62 | desc); |
Pawel Moll | 3b9334a | 2014-04-30 16:46:29 +0100 | [diff] [blame] | 63 | if (!init_data) |
| 64 | return -EINVAL; |
Pawel Moll | 31e5408 | 2012-09-24 18:56:54 +0100 | [diff] [blame] | 65 | |
| 66 | init_data->constraints.apply_uV = 0; |
| 67 | if (init_data->constraints.min_uV && init_data->constraints.max_uV) |
Axel Lin | eeb1b23 | 2019-04-29 19:35:41 +0800 | [diff] [blame] | 68 | desc->ops = &vexpress_regulator_ops; |
Pawel Moll | 31e5408 | 2012-09-24 18:56:54 +0100 | [diff] [blame] | 69 | else |
Axel Lin | eeb1b23 | 2019-04-29 19:35:41 +0800 | [diff] [blame] | 70 | desc->ops = &vexpress_regulator_ops_ro; |
Pawel Moll | 31e5408 | 2012-09-24 18:56:54 +0100 | [diff] [blame] | 71 | |
Axel Lin | eeb1b23 | 2019-04-29 19:35:41 +0800 | [diff] [blame] | 72 | config.regmap = regmap; |
Pawel Moll | 31e5408 | 2012-09-24 18:56:54 +0100 | [diff] [blame] | 73 | config.dev = &pdev->dev; |
| 74 | config.init_data = init_data; |
Pawel Moll | 31e5408 | 2012-09-24 18:56:54 +0100 | [diff] [blame] | 75 | config.of_node = pdev->dev.of_node; |
| 76 | |
Axel Lin | eeb1b23 | 2019-04-29 19:35:41 +0800 | [diff] [blame] | 77 | rdev = devm_regulator_register(&pdev->dev, desc, &config); |
zhengbin | 1d7c4c1 | 2019-11-18 18:59:22 +0800 | [diff] [blame] | 78 | return PTR_ERR_OR_ZERO(rdev); |
Pawel Moll | 31e5408 | 2012-09-24 18:56:54 +0100 | [diff] [blame] | 79 | } |
| 80 | |
Jingoo Han | 1439afd | 2014-05-07 17:09:12 +0900 | [diff] [blame] | 81 | static const struct of_device_id vexpress_regulator_of_match[] = { |
Pawel Moll | 31e5408 | 2012-09-24 18:56:54 +0100 | [diff] [blame] | 82 | { .compatible = "arm,vexpress-volt", }, |
Axel Lin | 9f4e45f | 2012-10-17 09:00:20 +0800 | [diff] [blame] | 83 | { } |
Pawel Moll | 31e5408 | 2012-09-24 18:56:54 +0100 | [diff] [blame] | 84 | }; |
Luis de Bethencourt | 7209fee | 2015-09-18 19:09:51 +0200 | [diff] [blame] | 85 | MODULE_DEVICE_TABLE(of, vexpress_regulator_of_match); |
Pawel Moll | 31e5408 | 2012-09-24 18:56:54 +0100 | [diff] [blame] | 86 | |
| 87 | static struct platform_driver vexpress_regulator_driver = { |
| 88 | .probe = vexpress_regulator_probe, |
Pawel Moll | 31e5408 | 2012-09-24 18:56:54 +0100 | [diff] [blame] | 89 | .driver = { |
| 90 | .name = DRVNAME, |
Pawel Moll | 31e5408 | 2012-09-24 18:56:54 +0100 | [diff] [blame] | 91 | .of_match_table = vexpress_regulator_of_match, |
| 92 | }, |
| 93 | }; |
| 94 | |
| 95 | module_platform_driver(vexpress_regulator_driver); |
| 96 | |
| 97 | MODULE_AUTHOR("Pawel Moll <pawel.moll@arm.com>"); |
| 98 | MODULE_DESCRIPTION("Versatile Express regulator"); |
| 99 | MODULE_LICENSE("GPL"); |
| 100 | MODULE_ALIAS("platform:vexpress-regulator"); |