Merge tag 'trace-fixes-v3.10-rc3-v3' of git://git.kernel.org/pub/scm/linux/kernel...
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / regulator / isl6271a-regulator.c
CommitLineData
51bd6943
MV
1/*
2 * isl6271a-regulator.c
3 *
4 * Support for Intersil ISL6271A voltage regulator
5 *
6 * Copyright (C) 2010 Marek Vasut <marek.vasut@gmail.com>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation version 2.
11 *
12 * This program is distributed "as is" WITHOUT ANY WARRANTY of any kind,
13 * whether express or implied; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 */
17
18#include <linux/kernel.h>
19#include <linux/module.h>
20#include <linux/init.h>
21#include <linux/err.h>
22#include <linux/platform_device.h>
23#include <linux/regulator/driver.h>
24#include <linux/i2c.h>
51bd6943
MV
25#include <linux/slab.h>
26
27#define ISL6271A_VOLTAGE_MIN 850000
28#define ISL6271A_VOLTAGE_MAX 1600000
29#define ISL6271A_VOLTAGE_STEP 50000
30
31/* PMIC details */
32struct isl_pmic {
33 struct i2c_client *client;
34 struct regulator_dev *rdev[3];
35 struct mutex mtx;
36};
37
a9b2899b 38static int isl6271a_get_voltage_sel(struct regulator_dev *dev)
51bd6943
MV
39{
40 struct isl_pmic *pmic = rdev_get_drvdata(dev);
a9b2899b 41 int idx;
51bd6943
MV
42
43 mutex_lock(&pmic->mtx);
44
45 idx = i2c_smbus_read_byte(pmic->client);
a9b2899b 46 if (idx < 0)
51bd6943 47 dev_err(&pmic->client->dev, "Error getting voltage\n");
51bd6943 48
51bd6943 49 mutex_unlock(&pmic->mtx);
a9b2899b 50 return idx;
51bd6943
MV
51}
52
dde2b724
AL
53static int isl6271a_set_voltage_sel(struct regulator_dev *dev,
54 unsigned selector)
51bd6943
MV
55{
56 struct isl_pmic *pmic = rdev_get_drvdata(dev);
dde2b724 57 int err;
3a93f2a9 58
51bd6943
MV
59 mutex_lock(&pmic->mtx);
60
dde2b724 61 err = i2c_smbus_write_byte(pmic->client, selector);
51bd6943
MV
62 if (err < 0)
63 dev_err(&pmic->client->dev, "Error setting voltage\n");
64
65 mutex_unlock(&pmic->mtx);
66 return err;
67}
68
51bd6943 69static struct regulator_ops isl_core_ops = {
a9b2899b 70 .get_voltage_sel = isl6271a_get_voltage_sel,
dde2b724 71 .set_voltage_sel = isl6271a_set_voltage_sel,
2b7a7a4e 72 .list_voltage = regulator_list_voltage_linear,
dde2b724 73 .map_voltage = regulator_map_voltage_linear,
51bd6943
MV
74};
75
51bd6943 76static struct regulator_ops isl_fixed_ops = {
985f769c 77 .list_voltage = regulator_list_voltage_linear,
51bd6943
MV
78};
79
4b65e159 80static const struct regulator_desc isl_rd[] = {
51bd6943
MV
81 {
82 .name = "Core Buck",
83 .id = 0,
84 .n_voltages = 16,
85 .ops = &isl_core_ops,
86 .type = REGULATOR_VOLTAGE,
87 .owner = THIS_MODULE,
2b7a7a4e
AL
88 .min_uV = ISL6271A_VOLTAGE_MIN,
89 .uV_step = ISL6271A_VOLTAGE_STEP,
51bd6943
MV
90 }, {
91 .name = "LDO1",
92 .id = 1,
93 .n_voltages = 1,
94 .ops = &isl_fixed_ops,
95 .type = REGULATOR_VOLTAGE,
96 .owner = THIS_MODULE,
985f769c 97 .min_uV = 1100000,
51bd6943
MV
98 }, {
99 .name = "LDO2",
100 .id = 2,
101 .n_voltages = 1,
102 .ops = &isl_fixed_ops,
103 .type = REGULATOR_VOLTAGE,
104 .owner = THIS_MODULE,
985f769c 105 .min_uV = 1300000,
51bd6943
MV
106 },
107};
108
a5023574 109static int isl6271a_probe(struct i2c_client *i2c,
51bd6943
MV
110 const struct i2c_device_id *id)
111{
c172708d 112 struct regulator_config config = { };
51bd6943
MV
113 struct regulator_init_data *init_data = i2c->dev.platform_data;
114 struct isl_pmic *pmic;
115 int err, i;
116
117 if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
118 return -EIO;
119
ef6bd5a3 120 pmic = devm_kzalloc(&i2c->dev, sizeof(struct isl_pmic), GFP_KERNEL);
51bd6943
MV
121 if (!pmic)
122 return -ENOMEM;
123
124 pmic->client = i2c;
125
126 mutex_init(&pmic->mtx);
127
128 for (i = 0; i < 3; i++) {
c172708d
MB
129 config.dev = &i2c->dev;
130 if (i == 0)
131 config.init_data = init_data;
132 else
133 config.init_data = 0;
134 config.driver_data = pmic;
135
136 pmic->rdev[i] = regulator_register(&isl_rd[i], &config);
51bd6943
MV
137 if (IS_ERR(pmic->rdev[i])) {
138 dev_err(&i2c->dev, "failed to register %s\n", id->name);
fa63bd4a 139 err = PTR_ERR(pmic->rdev[i]);
51bd6943
MV
140 goto error;
141 }
142 }
143
144 i2c_set_clientdata(i2c, pmic);
145
146 return 0;
147
148error:
149 while (--i >= 0)
150 regulator_unregister(pmic->rdev[i]);
51bd6943
MV
151 return err;
152}
153
8dc995f5 154static int isl6271a_remove(struct i2c_client *i2c)
51bd6943
MV
155{
156 struct isl_pmic *pmic = i2c_get_clientdata(i2c);
157 int i;
158
51bd6943
MV
159 for (i = 0; i < 3; i++)
160 regulator_unregister(pmic->rdev[i]);
51bd6943
MV
161 return 0;
162}
163
164static const struct i2c_device_id isl6271a_id[] = {
165 {.name = "isl6271a", 0 },
166 { },
167};
168
169MODULE_DEVICE_TABLE(i2c, isl6271a_id);
170
171static struct i2c_driver isl6271a_i2c_driver = {
172 .driver = {
173 .name = "isl6271a",
174 .owner = THIS_MODULE,
175 },
176 .probe = isl6271a_probe,
5eb9f2b9 177 .remove = isl6271a_remove,
51bd6943
MV
178 .id_table = isl6271a_id,
179};
180
181static int __init isl6271a_init(void)
182{
183 return i2c_add_driver(&isl6271a_i2c_driver);
184}
185
186static void __exit isl6271a_cleanup(void)
187{
188 i2c_del_driver(&isl6271a_i2c_driver);
189}
190
191subsys_initcall(isl6271a_init);
192module_exit(isl6271a_cleanup);
193
194MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
195MODULE_DESCRIPTION("Intersil ISL6271A voltage regulator driver");
196MODULE_LICENSE("GPL v2");