Pawel Moll | 31e5408 | 2012-09-24 18:56:54 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This program is free software; you can redistribute it and/or modify |
| 3 | * it under the terms of the GNU General Public License version 2 as |
| 4 | * published by the Free Software Foundation. |
| 5 | * |
| 6 | * This program is distributed in the hope that it will be useful, |
| 7 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 8 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 9 | * GNU General Public License for more details. |
| 10 | * |
| 11 | * Copyright (C) 2012 ARM Limited |
| 12 | */ |
| 13 | |
| 14 | #define DRVNAME "vexpress-regulator" |
| 15 | #define pr_fmt(fmt) DRVNAME ": " fmt |
| 16 | |
| 17 | #include <linux/device.h> |
| 18 | #include <linux/err.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/of_device.h> |
| 21 | #include <linux/regulator/driver.h> |
| 22 | #include <linux/regulator/machine.h> |
| 23 | #include <linux/regulator/of_regulator.h> |
| 24 | #include <linux/vexpress.h> |
| 25 | |
Pawel Moll | 31e5408 | 2012-09-24 18:56:54 +0100 | [diff] [blame] | 26 | static int vexpress_regulator_get_voltage(struct regulator_dev *regdev) |
| 27 | { |
Axel Lin | eeb1b23 | 2019-04-29 19:35:41 +0800 | [diff] [blame^] | 28 | unsigned int uV; |
| 29 | int err = regmap_read(regdev->regmap, 0, &uV); |
Pawel Moll | 31e5408 | 2012-09-24 18:56:54 +0100 | [diff] [blame] | 30 | |
| 31 | return err ? err : uV; |
| 32 | } |
| 33 | |
| 34 | static int vexpress_regulator_set_voltage(struct regulator_dev *regdev, |
| 35 | int min_uV, int max_uV, unsigned *selector) |
| 36 | { |
Axel Lin | eeb1b23 | 2019-04-29 19:35:41 +0800 | [diff] [blame^] | 37 | return regmap_write(regdev->regmap, 0, min_uV); |
Pawel Moll | 31e5408 | 2012-09-24 18:56:54 +0100 | [diff] [blame] | 38 | } |
| 39 | |
Axel Lin | ab54a4d | 2019-04-11 00:19:37 +0800 | [diff] [blame] | 40 | static const struct regulator_ops vexpress_regulator_ops_ro = { |
Pawel Moll | 31e5408 | 2012-09-24 18:56:54 +0100 | [diff] [blame] | 41 | .get_voltage = vexpress_regulator_get_voltage, |
| 42 | }; |
| 43 | |
Axel Lin | ab54a4d | 2019-04-11 00:19:37 +0800 | [diff] [blame] | 44 | static const struct regulator_ops vexpress_regulator_ops = { |
Pawel Moll | 31e5408 | 2012-09-24 18:56:54 +0100 | [diff] [blame] | 45 | .get_voltage = vexpress_regulator_get_voltage, |
| 46 | .set_voltage = vexpress_regulator_set_voltage, |
| 47 | }; |
| 48 | |
| 49 | static int vexpress_regulator_probe(struct platform_device *pdev) |
| 50 | { |
Axel Lin | eeb1b23 | 2019-04-29 19:35:41 +0800 | [diff] [blame^] | 51 | struct regulator_desc *desc; |
Pawel Moll | 31e5408 | 2012-09-24 18:56:54 +0100 | [diff] [blame] | 52 | struct regulator_init_data *init_data; |
| 53 | struct regulator_config config = { }; |
Axel Lin | eeb1b23 | 2019-04-29 19:35:41 +0800 | [diff] [blame^] | 54 | struct regulator_dev *rdev; |
| 55 | struct regmap *regmap; |
Pawel Moll | 31e5408 | 2012-09-24 18:56:54 +0100 | [diff] [blame] | 56 | |
Axel Lin | eeb1b23 | 2019-04-29 19:35:41 +0800 | [diff] [blame^] | 57 | desc = devm_kzalloc(&pdev->dev, sizeof(*desc), GFP_KERNEL); |
| 58 | if (!desc) |
Pawel Moll | 3b9334a | 2014-04-30 16:46:29 +0100 | [diff] [blame] | 59 | return -ENOMEM; |
Pawel Moll | 31e5408 | 2012-09-24 18:56:54 +0100 | [diff] [blame] | 60 | |
Axel Lin | eeb1b23 | 2019-04-29 19:35:41 +0800 | [diff] [blame^] | 61 | regmap = devm_regmap_init_vexpress_config(&pdev->dev); |
| 62 | if (IS_ERR(regmap)) |
| 63 | return PTR_ERR(regmap); |
Pawel Moll | 31e5408 | 2012-09-24 18:56:54 +0100 | [diff] [blame] | 64 | |
Axel Lin | eeb1b23 | 2019-04-29 19:35:41 +0800 | [diff] [blame^] | 65 | desc->name = dev_name(&pdev->dev); |
| 66 | desc->type = REGULATOR_VOLTAGE; |
| 67 | desc->owner = THIS_MODULE; |
| 68 | desc->continuous_voltage_range = true; |
Pawel Moll | 31e5408 | 2012-09-24 18:56:54 +0100 | [diff] [blame] | 69 | |
Javier Martinez Canillas | 072e78b | 2014-11-10 14:43:53 +0100 | [diff] [blame] | 70 | 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^] | 71 | desc); |
Pawel Moll | 3b9334a | 2014-04-30 16:46:29 +0100 | [diff] [blame] | 72 | if (!init_data) |
| 73 | return -EINVAL; |
Pawel Moll | 31e5408 | 2012-09-24 18:56:54 +0100 | [diff] [blame] | 74 | |
| 75 | init_data->constraints.apply_uV = 0; |
| 76 | if (init_data->constraints.min_uV && init_data->constraints.max_uV) |
Axel Lin | eeb1b23 | 2019-04-29 19:35:41 +0800 | [diff] [blame^] | 77 | desc->ops = &vexpress_regulator_ops; |
Pawel Moll | 31e5408 | 2012-09-24 18:56:54 +0100 | [diff] [blame] | 78 | else |
Axel Lin | eeb1b23 | 2019-04-29 19:35:41 +0800 | [diff] [blame^] | 79 | desc->ops = &vexpress_regulator_ops_ro; |
Pawel Moll | 31e5408 | 2012-09-24 18:56:54 +0100 | [diff] [blame] | 80 | |
Axel Lin | eeb1b23 | 2019-04-29 19:35:41 +0800 | [diff] [blame^] | 81 | config.regmap = regmap; |
Pawel Moll | 31e5408 | 2012-09-24 18:56:54 +0100 | [diff] [blame] | 82 | config.dev = &pdev->dev; |
| 83 | config.init_data = init_data; |
Pawel Moll | 31e5408 | 2012-09-24 18:56:54 +0100 | [diff] [blame] | 84 | config.of_node = pdev->dev.of_node; |
| 85 | |
Axel Lin | eeb1b23 | 2019-04-29 19:35:41 +0800 | [diff] [blame^] | 86 | rdev = devm_regulator_register(&pdev->dev, desc, &config); |
| 87 | if (IS_ERR(rdev)) |
| 88 | return PTR_ERR(rdev); |
Pawel Moll | 31e5408 | 2012-09-24 18:56:54 +0100 | [diff] [blame] | 89 | |
| 90 | return 0; |
Pawel Moll | 31e5408 | 2012-09-24 18:56:54 +0100 | [diff] [blame] | 91 | } |
| 92 | |
Jingoo Han | 1439afd | 2014-05-07 17:09:12 +0900 | [diff] [blame] | 93 | static const struct of_device_id vexpress_regulator_of_match[] = { |
Pawel Moll | 31e5408 | 2012-09-24 18:56:54 +0100 | [diff] [blame] | 94 | { .compatible = "arm,vexpress-volt", }, |
Axel Lin | 9f4e45f | 2012-10-17 09:00:20 +0800 | [diff] [blame] | 95 | { } |
Pawel Moll | 31e5408 | 2012-09-24 18:56:54 +0100 | [diff] [blame] | 96 | }; |
Luis de Bethencourt | 7209fee | 2015-09-18 19:09:51 +0200 | [diff] [blame] | 97 | MODULE_DEVICE_TABLE(of, vexpress_regulator_of_match); |
Pawel Moll | 31e5408 | 2012-09-24 18:56:54 +0100 | [diff] [blame] | 98 | |
| 99 | static struct platform_driver vexpress_regulator_driver = { |
| 100 | .probe = vexpress_regulator_probe, |
Pawel Moll | 31e5408 | 2012-09-24 18:56:54 +0100 | [diff] [blame] | 101 | .driver = { |
| 102 | .name = DRVNAME, |
Pawel Moll | 31e5408 | 2012-09-24 18:56:54 +0100 | [diff] [blame] | 103 | .of_match_table = vexpress_regulator_of_match, |
| 104 | }, |
| 105 | }; |
| 106 | |
| 107 | module_platform_driver(vexpress_regulator_driver); |
| 108 | |
| 109 | MODULE_AUTHOR("Pawel Moll <pawel.moll@arm.com>"); |
| 110 | MODULE_DESCRIPTION("Versatile Express regulator"); |
| 111 | MODULE_LICENSE("GPL"); |
| 112 | MODULE_ALIAS("platform:vexpress-regulator"); |