blob: 4a11b82a6d573d61f5ca1e45c88bbb3634e487c1 [file] [log] [blame]
Marek Vasut51bd6942010-06-13 17:25:51 +02001/*
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>
Marek Vasut51bd6942010-06-13 17:25:51 +020025#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
Axel Lina9b28992012-05-16 10:10:22 +080038static int isl6271a_get_voltage_sel(struct regulator_dev *dev)
Marek Vasut51bd6942010-06-13 17:25:51 +020039{
40 struct isl_pmic *pmic = rdev_get_drvdata(dev);
Axel Lina9b28992012-05-16 10:10:22 +080041 int idx;
Marek Vasut51bd6942010-06-13 17:25:51 +020042
43 mutex_lock(&pmic->mtx);
44
45 idx = i2c_smbus_read_byte(pmic->client);
Axel Lina9b28992012-05-16 10:10:22 +080046 if (idx < 0)
Marek Vasut51bd6942010-06-13 17:25:51 +020047 dev_err(&pmic->client->dev, "Error getting voltage\n");
Marek Vasut51bd6942010-06-13 17:25:51 +020048
Marek Vasut51bd6942010-06-13 17:25:51 +020049 mutex_unlock(&pmic->mtx);
Axel Lina9b28992012-05-16 10:10:22 +080050 return idx;
Marek Vasut51bd6942010-06-13 17:25:51 +020051}
52
Axel Lindde2b722012-05-16 10:11:13 +080053static int isl6271a_set_voltage_sel(struct regulator_dev *dev,
54 unsigned selector)
Marek Vasut51bd6942010-06-13 17:25:51 +020055{
56 struct isl_pmic *pmic = rdev_get_drvdata(dev);
Axel Lindde2b722012-05-16 10:11:13 +080057 int err;
Mark Brown3a93f2a2010-11-10 14:38:29 +000058
Marek Vasut51bd6942010-06-13 17:25:51 +020059 mutex_lock(&pmic->mtx);
60
Axel Lindde2b722012-05-16 10:11:13 +080061 err = i2c_smbus_write_byte(pmic->client, selector);
Marek Vasut51bd6942010-06-13 17:25:51 +020062 if (err < 0)
63 dev_err(&pmic->client->dev, "Error setting voltage\n");
64
65 mutex_unlock(&pmic->mtx);
66 return err;
67}
68
Marek Vasut51bd6942010-06-13 17:25:51 +020069static struct regulator_ops isl_core_ops = {
Axel Lina9b28992012-05-16 10:10:22 +080070 .get_voltage_sel = isl6271a_get_voltage_sel,
Axel Lindde2b722012-05-16 10:11:13 +080071 .set_voltage_sel = isl6271a_set_voltage_sel,
Axel Lin2b7a7a42012-05-16 10:09:27 +080072 .list_voltage = regulator_list_voltage_linear,
Axel Lindde2b722012-05-16 10:11:13 +080073 .map_voltage = regulator_map_voltage_linear,
Marek Vasut51bd6942010-06-13 17:25:51 +020074};
75
76static int isl6271a_get_fixed_voltage(struct regulator_dev *dev)
77{
Axel Lin985f7692012-06-07 10:06:40 +080078 return dev->desc->min_uV;
Marek Vasut51bd6942010-06-13 17:25:51 +020079}
80
81static struct regulator_ops isl_fixed_ops = {
82 .get_voltage = isl6271a_get_fixed_voltage,
Axel Lin985f7692012-06-07 10:06:40 +080083 .list_voltage = regulator_list_voltage_linear,
84 .map_voltage = regulator_map_voltage_linear,
Marek Vasut51bd6942010-06-13 17:25:51 +020085};
86
Axel Lin4b65e152012-04-05 11:58:06 +080087static const struct regulator_desc isl_rd[] = {
Marek Vasut51bd6942010-06-13 17:25:51 +020088 {
89 .name = "Core Buck",
90 .id = 0,
91 .n_voltages = 16,
92 .ops = &isl_core_ops,
93 .type = REGULATOR_VOLTAGE,
94 .owner = THIS_MODULE,
Axel Lin2b7a7a42012-05-16 10:09:27 +080095 .min_uV = ISL6271A_VOLTAGE_MIN,
96 .uV_step = ISL6271A_VOLTAGE_STEP,
Marek Vasut51bd6942010-06-13 17:25:51 +020097 }, {
98 .name = "LDO1",
99 .id = 1,
100 .n_voltages = 1,
101 .ops = &isl_fixed_ops,
102 .type = REGULATOR_VOLTAGE,
103 .owner = THIS_MODULE,
Axel Lin985f7692012-06-07 10:06:40 +0800104 .min_uV = 1100000,
Marek Vasut51bd6942010-06-13 17:25:51 +0200105 }, {
106 .name = "LDO2",
107 .id = 2,
108 .n_voltages = 1,
109 .ops = &isl_fixed_ops,
110 .type = REGULATOR_VOLTAGE,
111 .owner = THIS_MODULE,
Axel Lin985f7692012-06-07 10:06:40 +0800112 .min_uV = 1300000,
Marek Vasut51bd6942010-06-13 17:25:51 +0200113 },
114};
115
116static int __devinit isl6271a_probe(struct i2c_client *i2c,
117 const struct i2c_device_id *id)
118{
Mark Brownc1727082012-04-04 00:50:22 +0100119 struct regulator_config config = { };
Marek Vasut51bd6942010-06-13 17:25:51 +0200120 struct regulator_init_data *init_data = i2c->dev.platform_data;
121 struct isl_pmic *pmic;
122 int err, i;
123
124 if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
125 return -EIO;
126
Axel Linef6bd5a2012-04-11 23:05:49 +0800127 pmic = devm_kzalloc(&i2c->dev, sizeof(struct isl_pmic), GFP_KERNEL);
Marek Vasut51bd6942010-06-13 17:25:51 +0200128 if (!pmic)
129 return -ENOMEM;
130
131 pmic->client = i2c;
132
133 mutex_init(&pmic->mtx);
134
135 for (i = 0; i < 3; i++) {
Mark Brownc1727082012-04-04 00:50:22 +0100136 config.dev = &i2c->dev;
137 if (i == 0)
138 config.init_data = init_data;
139 else
140 config.init_data = 0;
141 config.driver_data = pmic;
142
143 pmic->rdev[i] = regulator_register(&isl_rd[i], &config);
Marek Vasut51bd6942010-06-13 17:25:51 +0200144 if (IS_ERR(pmic->rdev[i])) {
145 dev_err(&i2c->dev, "failed to register %s\n", id->name);
roel kluinfa63bd42010-12-31 16:26:47 +0100146 err = PTR_ERR(pmic->rdev[i]);
Marek Vasut51bd6942010-06-13 17:25:51 +0200147 goto error;
148 }
149 }
150
151 i2c_set_clientdata(i2c, pmic);
152
153 return 0;
154
155error:
156 while (--i >= 0)
157 regulator_unregister(pmic->rdev[i]);
Marek Vasut51bd6942010-06-13 17:25:51 +0200158 return err;
159}
160
161static int __devexit isl6271a_remove(struct i2c_client *i2c)
162{
163 struct isl_pmic *pmic = i2c_get_clientdata(i2c);
164 int i;
165
Marek Vasut51bd6942010-06-13 17:25:51 +0200166 for (i = 0; i < 3; i++)
167 regulator_unregister(pmic->rdev[i]);
Marek Vasut51bd6942010-06-13 17:25:51 +0200168 return 0;
169}
170
171static const struct i2c_device_id isl6271a_id[] = {
172 {.name = "isl6271a", 0 },
173 { },
174};
175
176MODULE_DEVICE_TABLE(i2c, isl6271a_id);
177
178static struct i2c_driver isl6271a_i2c_driver = {
179 .driver = {
180 .name = "isl6271a",
181 .owner = THIS_MODULE,
182 },
183 .probe = isl6271a_probe,
184 .remove = __devexit_p(isl6271a_remove),
185 .id_table = isl6271a_id,
186};
187
188static int __init isl6271a_init(void)
189{
190 return i2c_add_driver(&isl6271a_i2c_driver);
191}
192
193static void __exit isl6271a_cleanup(void)
194{
195 i2c_del_driver(&isl6271a_i2c_driver);
196}
197
198subsys_initcall(isl6271a_init);
199module_exit(isl6271a_cleanup);
200
201MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
202MODULE_DESCRIPTION("Intersil ISL6271A voltage regulator driver");
203MODULE_LICENSE("GPL v2");