blob: 6f28bba81d13cc44e0a96ae687b5693fd6e6cdc8 [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;
Marek Vasut51bd6942010-06-13 17:25:51 +020034 struct mutex mtx;
35};
36
Axel Lina9b28992012-05-16 10:10:22 +080037static int isl6271a_get_voltage_sel(struct regulator_dev *dev)
Marek Vasut51bd6942010-06-13 17:25:51 +020038{
39 struct isl_pmic *pmic = rdev_get_drvdata(dev);
Axel Lina9b28992012-05-16 10:10:22 +080040 int idx;
Marek Vasut51bd6942010-06-13 17:25:51 +020041
42 mutex_lock(&pmic->mtx);
43
44 idx = i2c_smbus_read_byte(pmic->client);
Axel Lina9b28992012-05-16 10:10:22 +080045 if (idx < 0)
Marek Vasut51bd6942010-06-13 17:25:51 +020046 dev_err(&pmic->client->dev, "Error getting voltage\n");
Marek Vasut51bd6942010-06-13 17:25:51 +020047
Marek Vasut51bd6942010-06-13 17:25:51 +020048 mutex_unlock(&pmic->mtx);
Axel Lina9b28992012-05-16 10:10:22 +080049 return idx;
Marek Vasut51bd6942010-06-13 17:25:51 +020050}
51
Axel Lindde2b722012-05-16 10:11:13 +080052static int isl6271a_set_voltage_sel(struct regulator_dev *dev,
53 unsigned selector)
Marek Vasut51bd6942010-06-13 17:25:51 +020054{
55 struct isl_pmic *pmic = rdev_get_drvdata(dev);
Axel Lindde2b722012-05-16 10:11:13 +080056 int err;
Mark Brown3a93f2a2010-11-10 14:38:29 +000057
Marek Vasut51bd6942010-06-13 17:25:51 +020058 mutex_lock(&pmic->mtx);
59
Axel Lindde2b722012-05-16 10:11:13 +080060 err = i2c_smbus_write_byte(pmic->client, selector);
Marek Vasut51bd6942010-06-13 17:25:51 +020061 if (err < 0)
62 dev_err(&pmic->client->dev, "Error setting voltage\n");
63
64 mutex_unlock(&pmic->mtx);
65 return err;
66}
67
Axel Lin1dceee52019-01-28 22:19:21 +080068static const struct regulator_ops isl_core_ops = {
Axel Lina9b28992012-05-16 10:10:22 +080069 .get_voltage_sel = isl6271a_get_voltage_sel,
Axel Lindde2b722012-05-16 10:11:13 +080070 .set_voltage_sel = isl6271a_set_voltage_sel,
Axel Lin2b7a7a42012-05-16 10:09:27 +080071 .list_voltage = regulator_list_voltage_linear,
Axel Lindde2b722012-05-16 10:11:13 +080072 .map_voltage = regulator_map_voltage_linear,
Marek Vasut51bd6942010-06-13 17:25:51 +020073};
74
Axel Lin1dceee52019-01-28 22:19:21 +080075static const struct regulator_ops isl_fixed_ops = {
Axel Lin985f7692012-06-07 10:06:40 +080076 .list_voltage = regulator_list_voltage_linear,
Marek Vasut51bd6942010-06-13 17:25:51 +020077};
78
Axel Lin4b65e152012-04-05 11:58:06 +080079static const struct regulator_desc isl_rd[] = {
Marek Vasut51bd6942010-06-13 17:25:51 +020080 {
81 .name = "Core Buck",
82 .id = 0,
83 .n_voltages = 16,
84 .ops = &isl_core_ops,
85 .type = REGULATOR_VOLTAGE,
86 .owner = THIS_MODULE,
Axel Lin2b7a7a42012-05-16 10:09:27 +080087 .min_uV = ISL6271A_VOLTAGE_MIN,
88 .uV_step = ISL6271A_VOLTAGE_STEP,
Marek Vasut51bd6942010-06-13 17:25:51 +020089 }, {
90 .name = "LDO1",
91 .id = 1,
92 .n_voltages = 1,
93 .ops = &isl_fixed_ops,
94 .type = REGULATOR_VOLTAGE,
95 .owner = THIS_MODULE,
Axel Lin985f7692012-06-07 10:06:40 +080096 .min_uV = 1100000,
Marek Vasut51bd6942010-06-13 17:25:51 +020097 }, {
98 .name = "LDO2",
99 .id = 2,
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 = 1300000,
Marek Vasut51bd6942010-06-13 17:25:51 +0200105 },
106};
107
Bill Pembertona5023572012-11-19 13:22:22 -0500108static int isl6271a_probe(struct i2c_client *i2c,
Marek Vasut51bd6942010-06-13 17:25:51 +0200109 const struct i2c_device_id *id)
110{
Axel Lina6e58292019-01-28 22:19:20 +0800111 struct regulator_dev *rdev;
Mark Brownc1727082012-04-04 00:50:22 +0100112 struct regulator_config config = { };
Jingoo Handff91d02013-07-30 17:20:47 +0900113 struct regulator_init_data *init_data = dev_get_platdata(&i2c->dev);
Marek Vasut51bd6942010-06-13 17:25:51 +0200114 struct isl_pmic *pmic;
Sachin Kamat0ab6e8c2013-09-04 12:00:58 +0530115 int i;
Marek Vasut51bd6942010-06-13 17:25:51 +0200116
117 if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
118 return -EIO;
119
Axel Linef6bd5a2012-04-11 23:05:49 +0800120 pmic = devm_kzalloc(&i2c->dev, sizeof(struct isl_pmic), GFP_KERNEL);
Marek Vasut51bd6942010-06-13 17:25:51 +0200121 if (!pmic)
122 return -ENOMEM;
123
124 pmic->client = i2c;
125
126 mutex_init(&pmic->mtx);
127
128 for (i = 0; i < 3; i++) {
Mark Brownc1727082012-04-04 00:50:22 +0100129 config.dev = &i2c->dev;
130 if (i == 0)
131 config.init_data = init_data;
132 else
Sachin Kamat85bea1b2013-05-08 16:09:05 +0530133 config.init_data = NULL;
Mark Brownc1727082012-04-04 00:50:22 +0100134 config.driver_data = pmic;
135
Axel Lina6e58292019-01-28 22:19:20 +0800136 rdev = devm_regulator_register(&i2c->dev, &isl_rd[i], &config);
137 if (IS_ERR(rdev)) {
Marek Vasut51bd6942010-06-13 17:25:51 +0200138 dev_err(&i2c->dev, "failed to register %s\n", id->name);
Axel Lina6e58292019-01-28 22:19:20 +0800139 return PTR_ERR(rdev);
Marek Vasut51bd6942010-06-13 17:25:51 +0200140 }
141 }
142
143 i2c_set_clientdata(i2c, pmic);
144
145 return 0;
Marek Vasut51bd6942010-06-13 17:25:51 +0200146}
147
148static const struct i2c_device_id isl6271a_id[] = {
149 {.name = "isl6271a", 0 },
150 { },
151};
152
153MODULE_DEVICE_TABLE(i2c, isl6271a_id);
154
155static struct i2c_driver isl6271a_i2c_driver = {
156 .driver = {
157 .name = "isl6271a",
Marek Vasut51bd6942010-06-13 17:25:51 +0200158 },
159 .probe = isl6271a_probe,
Marek Vasut51bd6942010-06-13 17:25:51 +0200160 .id_table = isl6271a_id,
161};
162
163static int __init isl6271a_init(void)
164{
165 return i2c_add_driver(&isl6271a_i2c_driver);
166}
167
168static void __exit isl6271a_cleanup(void)
169{
170 i2c_del_driver(&isl6271a_i2c_driver);
171}
172
173subsys_initcall(isl6271a_init);
174module_exit(isl6271a_cleanup);
175
176MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
177MODULE_DESCRIPTION("Intersil ISL6271A voltage regulator driver");
178MODULE_LICENSE("GPL v2");