169dc16c3SMark Brown /*
269dc16c3SMark Brown  * wm8994-regulator.c  --  Regulator driver for the WM8994
369dc16c3SMark Brown  *
469dc16c3SMark Brown  * Copyright 2009 Wolfson Microelectronics PLC.
569dc16c3SMark Brown  *
669dc16c3SMark Brown  * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
769dc16c3SMark Brown  *
869dc16c3SMark Brown  *  This program is free software; you can redistribute  it and/or modify it
969dc16c3SMark Brown  *  under  the terms of  the GNU General  Public License as published by the
1069dc16c3SMark Brown  *  Free Software Foundation;  either version 2 of the  License, or (at your
1169dc16c3SMark Brown  *  option) any later version.
1269dc16c3SMark Brown  */
1369dc16c3SMark Brown 
1469dc16c3SMark Brown #include <linux/module.h>
1569dc16c3SMark Brown #include <linux/moduleparam.h>
1669dc16c3SMark Brown #include <linux/init.h>
1769dc16c3SMark Brown #include <linux/bitops.h>
1869dc16c3SMark Brown #include <linux/err.h>
1969dc16c3SMark Brown #include <linux/platform_device.h>
2069dc16c3SMark Brown #include <linux/regulator/driver.h>
21e3d27775SMark Brown #include <linux/regulator/machine.h>
2269dc16c3SMark Brown #include <linux/gpio.h>
235a0e3ad6STejun Heo #include <linux/slab.h>
2469dc16c3SMark Brown 
2569dc16c3SMark Brown #include <linux/mfd/wm8994/core.h>
2669dc16c3SMark Brown #include <linux/mfd/wm8994/registers.h>
2769dc16c3SMark Brown #include <linux/mfd/wm8994/pdata.h>
2869dc16c3SMark Brown 
2969dc16c3SMark Brown struct wm8994_ldo {
3069dc16c3SMark Brown 	struct regulator_dev *regulator;
3169dc16c3SMark Brown 	struct wm8994 *wm8994;
32e3d27775SMark Brown 	struct regulator_consumer_supply supply;
33e3d27775SMark Brown 	struct regulator_init_data init_data;
3469dc16c3SMark Brown };
3569dc16c3SMark Brown 
3669dc16c3SMark Brown #define WM8994_LDO1_MAX_SELECTOR 0x7
3769dc16c3SMark Brown #define WM8994_LDO2_MAX_SELECTOR 0x3
3869dc16c3SMark Brown 
3969dc16c3SMark Brown static struct regulator_ops wm8994_ldo1_ops = {
40f2d103adSAxel Lin 	.list_voltage = regulator_list_voltage_linear,
41f2d103adSAxel Lin 	.map_voltage = regulator_map_voltage_linear,
42633b6fcdSMark Brown 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
43633b6fcdSMark Brown 	.set_voltage_sel = regulator_set_voltage_sel_regmap,
4469dc16c3SMark Brown };
4569dc16c3SMark Brown 
4669dc16c3SMark Brown static int wm8994_ldo2_list_voltage(struct regulator_dev *rdev,
4769dc16c3SMark Brown 				    unsigned int selector)
4869dc16c3SMark Brown {
495a7743edSMark Brown 	struct wm8994_ldo *ldo = rdev_get_drvdata(rdev);
505a7743edSMark Brown 
5169dc16c3SMark Brown 	if (selector > WM8994_LDO2_MAX_SELECTOR)
5269dc16c3SMark Brown 		return -EINVAL;
5369dc16c3SMark Brown 
545a7743edSMark Brown 	switch (ldo->wm8994->type) {
555a7743edSMark Brown 	case WM8994:
5669dc16c3SMark Brown 		return (selector * 100000) + 900000;
575a7743edSMark Brown 	case WM8958:
585a7743edSMark Brown 		return (selector * 100000) + 1000000;
59a1ff89efSMark Brown 	case WM1811:
60a1ff89efSMark Brown 		switch (selector) {
61a1ff89efSMark Brown 		case 0:
62a1ff89efSMark Brown 			return -EINVAL;
63a1ff89efSMark Brown 		default:
64a1ff89efSMark Brown 			return (selector * 100000) + 950000;
65a1ff89efSMark Brown 		}
66a1ff89efSMark Brown 		break;
675a7743edSMark Brown 	default:
685a7743edSMark Brown 		return -EINVAL;
695a7743edSMark Brown 	}
7069dc16c3SMark Brown }
7169dc16c3SMark Brown 
7269dc16c3SMark Brown static struct regulator_ops wm8994_ldo2_ops = {
7369dc16c3SMark Brown 	.list_voltage = wm8994_ldo2_list_voltage,
74633b6fcdSMark Brown 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
75633b6fcdSMark Brown 	.set_voltage_sel = regulator_set_voltage_sel_regmap,
7669dc16c3SMark Brown };
7769dc16c3SMark Brown 
7825e4d602SMark Brown static const struct regulator_desc wm8994_ldo_desc[] = {
7969dc16c3SMark Brown 	{
8069dc16c3SMark Brown 		.name = "LDO1",
8169dc16c3SMark Brown 		.id = 1,
8269dc16c3SMark Brown 		.type = REGULATOR_VOLTAGE,
8369dc16c3SMark Brown 		.n_voltages = WM8994_LDO1_MAX_SELECTOR + 1,
84633b6fcdSMark Brown 		.vsel_reg = WM8994_LDO_1,
85633b6fcdSMark Brown 		.vsel_mask = WM8994_LDO1_VSEL_MASK,
8669dc16c3SMark Brown 		.ops = &wm8994_ldo1_ops,
87f2d103adSAxel Lin 		.min_uV = 2400000,
88f2d103adSAxel Lin 		.uV_step = 100000,
8900581b30SMark Brown 		.enable_time = 3000,
9069dc16c3SMark Brown 		.owner = THIS_MODULE,
9169dc16c3SMark Brown 	},
9269dc16c3SMark Brown 	{
9369dc16c3SMark Brown 		.name = "LDO2",
9469dc16c3SMark Brown 		.id = 2,
9569dc16c3SMark Brown 		.type = REGULATOR_VOLTAGE,
9669dc16c3SMark Brown 		.n_voltages = WM8994_LDO2_MAX_SELECTOR + 1,
97633b6fcdSMark Brown 		.vsel_reg = WM8994_LDO_2,
98633b6fcdSMark Brown 		.vsel_mask = WM8994_LDO2_VSEL_MASK,
9969dc16c3SMark Brown 		.ops = &wm8994_ldo2_ops,
10000581b30SMark Brown 		.enable_time = 3000,
10169dc16c3SMark Brown 		.owner = THIS_MODULE,
10269dc16c3SMark Brown 	},
10369dc16c3SMark Brown };
10469dc16c3SMark Brown 
105e3d27775SMark Brown static const struct regulator_consumer_supply wm8994_ldo_consumer[] = {
106e3d27775SMark Brown 	{ .supply = "AVDD1" },
107e3d27775SMark Brown 	{ .supply = "DCVDD" },
108e3d27775SMark Brown };
109e3d27775SMark Brown 
110e3d27775SMark Brown static const struct regulator_init_data wm8994_ldo_default[] = {
111e3d27775SMark Brown 	{
112e3d27775SMark Brown 		.constraints = {
113e3d27775SMark Brown 			.valid_ops_mask = REGULATOR_CHANGE_STATUS,
114e3d27775SMark Brown 		},
115e3d27775SMark Brown 		.num_consumer_supplies = 1,
116e3d27775SMark Brown 	},
117e3d27775SMark Brown 	{
118e3d27775SMark Brown 		.constraints = {
119e3d27775SMark Brown 			.valid_ops_mask = REGULATOR_CHANGE_STATUS,
120e3d27775SMark Brown 		},
121e3d27775SMark Brown 		.num_consumer_supplies = 1,
122e3d27775SMark Brown 	},
123e3d27775SMark Brown };
124e3d27775SMark Brown 
125a5023574SBill Pemberton static int wm8994_ldo_probe(struct platform_device *pdev)
12669dc16c3SMark Brown {
12769dc16c3SMark Brown 	struct wm8994 *wm8994 = dev_get_drvdata(pdev->dev.parent);
128dff91d0bSJingoo Han 	struct wm8994_pdata *pdata = dev_get_platdata(wm8994->dev);
12969dc16c3SMark Brown 	int id = pdev->id % ARRAY_SIZE(pdata->ldo);
130c172708dSMark Brown 	struct regulator_config config = { };
13169dc16c3SMark Brown 	struct wm8994_ldo *ldo;
13269dc16c3SMark Brown 	int ret;
13369dc16c3SMark Brown 
13469dc16c3SMark Brown 	dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
13569dc16c3SMark Brown 
136b9e0348fSMark Brown 	ldo = devm_kzalloc(&pdev->dev, sizeof(struct wm8994_ldo), GFP_KERNEL);
1374deaf85aSSachin Kamat 	if (!ldo)
13869dc16c3SMark Brown 		return -ENOMEM;
13969dc16c3SMark Brown 
14069dc16c3SMark Brown 	ldo->wm8994 = wm8994;
141e3d27775SMark Brown 	ldo->supply = wm8994_ldo_consumer[id];
142e3d27775SMark Brown 	ldo->supply.dev_name = dev_name(wm8994->dev);
14369dc16c3SMark Brown 
1445d0526eaSMark Brown 	config.dev = wm8994->dev;
145c172708dSMark Brown 	config.driver_data = ldo;
146633b6fcdSMark Brown 	config.regmap = wm8994->regmap;
147e3d27775SMark Brown 	config.init_data = &ldo->init_data;
148e3d27775SMark Brown 	if (pdata)
149495fd8c8SMark Brown 		config.ena_gpio = pdata->ldo[id].enable;
1503ea93896SSylwester Nawrocki 	else if (wm8994->dev->of_node)
1513ea93896SSylwester Nawrocki 		config.ena_gpio = wm8994->pdata.ldo[id].enable;
152e3d27775SMark Brown 
153e3d27775SMark Brown 	/* Use default constraints if none set up */
1543ea93896SSylwester Nawrocki 	if (!pdata || !pdata->ldo[id].init_data || wm8994->dev->of_node) {
155e3d27775SMark Brown 		dev_dbg(wm8994->dev, "Using default init data, supply %s %s\n",
156e3d27775SMark Brown 			ldo->supply.dev_name, ldo->supply.supply);
157e3d27775SMark Brown 
158e3d27775SMark Brown 		ldo->init_data = wm8994_ldo_default[id];
159e3d27775SMark Brown 		ldo->init_data.consumer_supplies = &ldo->supply;
160e3d27775SMark Brown 		if (!config.ena_gpio)
161e3d27775SMark Brown 			ldo->init_data.constraints.valid_ops_mask = 0;
162e3d27775SMark Brown 	} else {
163e3d27775SMark Brown 		ldo->init_data = *pdata->ldo[id].init_data;
164495fd8c8SMark Brown 	}
165c172708dSMark Brown 
1669e2bfbbdSMark Brown 	ldo->regulator = devm_regulator_register(&pdev->dev,
1679e2bfbbdSMark Brown 						 &wm8994_ldo_desc[id],
1689e2bfbbdSMark Brown 						 &config);
16969dc16c3SMark Brown 	if (IS_ERR(ldo->regulator)) {
17069dc16c3SMark Brown 		ret = PTR_ERR(ldo->regulator);
17169dc16c3SMark Brown 		dev_err(wm8994->dev, "Failed to register LDO%d: %d\n",
17269dc16c3SMark Brown 			id + 1, ret);
173495fd8c8SMark Brown 		goto err;
17469dc16c3SMark Brown 	}
17569dc16c3SMark Brown 
17669dc16c3SMark Brown 	platform_set_drvdata(pdev, ldo);
17769dc16c3SMark Brown 
17869dc16c3SMark Brown 	return 0;
17969dc16c3SMark Brown 
18069dc16c3SMark Brown err:
18169dc16c3SMark Brown 	return ret;
18269dc16c3SMark Brown }
18369dc16c3SMark Brown 
18469dc16c3SMark Brown static struct platform_driver wm8994_ldo_driver = {
18569dc16c3SMark Brown 	.probe = wm8994_ldo_probe,
18669dc16c3SMark Brown 	.driver		= {
18769dc16c3SMark Brown 		.name	= "wm8994-ldo",
188598b3578SDmitry Torokhov 		.owner	= THIS_MODULE,
18969dc16c3SMark Brown 	},
19069dc16c3SMark Brown };
19169dc16c3SMark Brown 
19205650216SMark Brown module_platform_driver(wm8994_ldo_driver);
19369dc16c3SMark Brown 
19469dc16c3SMark Brown /* Module information */
19569dc16c3SMark Brown MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
19669dc16c3SMark Brown MODULE_DESCRIPTION("WM8994 LDO driver");
19769dc16c3SMark Brown MODULE_LICENSE("GPL");
19869dc16c3SMark Brown MODULE_ALIAS("platform:wm8994-ldo");
199