2 * arizona-micsupp.c -- Microphone supply for Arizona devices
4 * Copyright 2012 Wolfson Microelectronics PLC.
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/init.h>
17 #include <linux/bitops.h>
18 #include <linux/err.h>
19 #include <linux/platform_device.h>
20 #include <linux/regulator/driver.h>
21 #include <linux/regulator/machine.h>
22 #include <linux/gpio.h>
23 #include <linux/slab.h>
25 #include <linux/mfd/arizona/core.h>
26 #include <linux/mfd/arizona/pdata.h>
27 #include <linux/mfd/arizona/registers.h>
29 #define ARIZONA_MICSUPP_MAX_SELECTOR 0x1f
31 struct arizona_micsupp {
32 struct regulator_dev *regulator;
33 struct arizona *arizona;
35 struct regulator_consumer_supply supply;
36 struct regulator_init_data init_data;
39 static int arizona_micsupp_list_voltage(struct regulator_dev *rdev,
40 unsigned int selector)
42 if (selector > ARIZONA_MICSUPP_MAX_SELECTOR)
45 if (selector == ARIZONA_MICSUPP_MAX_SELECTOR)
48 return (selector * 50000) + 1700000;
51 static int arizona_micsupp_map_voltage(struct regulator_dev *rdev,
52 int min_uV, int max_uV)
61 selector = ARIZONA_MICSUPP_MAX_SELECTOR;
63 selector = DIV_ROUND_UP(min_uV - 1700000, 50000);
68 voltage = arizona_micsupp_list_voltage(rdev, selector);
69 if (voltage < min_uV || voltage > max_uV)
75 static struct regulator_ops arizona_micsupp_ops = {
76 .enable = regulator_enable_regmap,
77 .disable = regulator_disable_regmap,
78 .is_enabled = regulator_is_enabled_regmap,
80 .list_voltage = arizona_micsupp_list_voltage,
81 .map_voltage = arizona_micsupp_map_voltage,
83 .get_voltage_sel = regulator_get_voltage_sel_regmap,
84 .set_voltage_sel = regulator_set_voltage_sel_regmap,
86 .get_bypass = regulator_get_bypass_regmap,
87 .set_bypass = regulator_set_bypass_regmap,
90 static const struct regulator_desc arizona_micsupp = {
92 .supply_name = "CPVDD",
93 .type = REGULATOR_VOLTAGE,
94 .n_voltages = ARIZONA_MICSUPP_MAX_SELECTOR + 1,
95 .ops = &arizona_micsupp_ops,
97 .vsel_reg = ARIZONA_LDO2_CONTROL_1,
98 .vsel_mask = ARIZONA_LDO2_VSEL_MASK,
99 .enable_reg = ARIZONA_MIC_CHARGE_PUMP_1,
100 .enable_mask = ARIZONA_CPMIC_ENA,
101 .bypass_reg = ARIZONA_MIC_CHARGE_PUMP_1,
102 .bypass_mask = ARIZONA_CPMIC_BYPASS,
106 .owner = THIS_MODULE,
109 static const struct regulator_init_data arizona_micsupp_default = {
111 .valid_ops_mask = REGULATOR_CHANGE_STATUS |
112 REGULATOR_CHANGE_VOLTAGE,
117 .num_consumer_supplies = 1,
120 static int arizona_micsupp_probe(struct platform_device *pdev)
122 struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
123 struct regulator_config config = { };
124 struct arizona_micsupp *micsupp;
127 micsupp = devm_kzalloc(&pdev->dev, sizeof(*micsupp), GFP_KERNEL);
128 if (micsupp == NULL) {
129 dev_err(&pdev->dev, "Unable to allocate private data\n");
133 micsupp->arizona = arizona;
136 * Since the chip usually supplies itself we provide some
137 * default init_data for it. This will be overridden with
138 * platform data if provided.
140 micsupp->init_data = arizona_micsupp_default;
141 micsupp->init_data.consumer_supplies = &micsupp->supply;
142 micsupp->supply.supply = "MICVDD";
143 micsupp->supply.dev_name = dev_name(arizona->dev);
145 config.dev = arizona->dev;
146 config.driver_data = micsupp;
147 config.regmap = arizona->regmap;
149 if (arizona->pdata.micvdd)
150 config.init_data = arizona->pdata.micvdd;
152 config.init_data = &micsupp->init_data;
154 /* Default to regulated mode until the API supports bypass */
155 regmap_update_bits(arizona->regmap, ARIZONA_MIC_CHARGE_PUMP_1,
156 ARIZONA_CPMIC_BYPASS, 0);
158 micsupp->regulator = regulator_register(&arizona_micsupp, &config);
159 if (IS_ERR(micsupp->regulator)) {
160 ret = PTR_ERR(micsupp->regulator);
161 dev_err(arizona->dev, "Failed to register mic supply: %d\n",
166 platform_set_drvdata(pdev, micsupp);
171 static int arizona_micsupp_remove(struct platform_device *pdev)
173 struct arizona_micsupp *micsupp = platform_get_drvdata(pdev);
175 regulator_unregister(micsupp->regulator);
180 static struct platform_driver arizona_micsupp_driver = {
181 .probe = arizona_micsupp_probe,
182 .remove = arizona_micsupp_remove,
184 .name = "arizona-micsupp",
185 .owner = THIS_MODULE,
189 module_platform_driver(arizona_micsupp_driver);
191 /* Module information */
192 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
193 MODULE_DESCRIPTION("Arizona microphone supply driver");
194 MODULE_LICENSE("GPL");
195 MODULE_ALIAS("platform:arizona-micsupp");