Marek Vasut | 51bd694 | 2010-06-13 17:25:51 +0200 | [diff] [blame] | 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> |
Marek Vasut | 51bd694 | 2010-06-13 17:25:51 +0200 | [diff] [blame] | 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 */ |
| 32 | struct isl_pmic { |
| 33 | struct i2c_client *client; |
| 34 | struct regulator_dev *rdev[3]; |
| 35 | struct mutex mtx; |
| 36 | }; |
| 37 | |
Axel Lin | a9b2899 | 2012-05-16 10:10:22 +0800 | [diff] [blame] | 38 | static int isl6271a_get_voltage_sel(struct regulator_dev *dev) |
Marek Vasut | 51bd694 | 2010-06-13 17:25:51 +0200 | [diff] [blame] | 39 | { |
| 40 | struct isl_pmic *pmic = rdev_get_drvdata(dev); |
Axel Lin | a9b2899 | 2012-05-16 10:10:22 +0800 | [diff] [blame] | 41 | int idx; |
Marek Vasut | 51bd694 | 2010-06-13 17:25:51 +0200 | [diff] [blame] | 42 | |
| 43 | mutex_lock(&pmic->mtx); |
| 44 | |
| 45 | idx = i2c_smbus_read_byte(pmic->client); |
Axel Lin | a9b2899 | 2012-05-16 10:10:22 +0800 | [diff] [blame] | 46 | if (idx < 0) |
Marek Vasut | 51bd694 | 2010-06-13 17:25:51 +0200 | [diff] [blame] | 47 | dev_err(&pmic->client->dev, "Error getting voltage\n"); |
Marek Vasut | 51bd694 | 2010-06-13 17:25:51 +0200 | [diff] [blame] | 48 | |
Marek Vasut | 51bd694 | 2010-06-13 17:25:51 +0200 | [diff] [blame] | 49 | mutex_unlock(&pmic->mtx); |
Axel Lin | a9b2899 | 2012-05-16 10:10:22 +0800 | [diff] [blame] | 50 | return idx; |
Marek Vasut | 51bd694 | 2010-06-13 17:25:51 +0200 | [diff] [blame] | 51 | } |
| 52 | |
Axel Lin | dde2b72 | 2012-05-16 10:11:13 +0800 | [diff] [blame] | 53 | static int isl6271a_set_voltage_sel(struct regulator_dev *dev, |
| 54 | unsigned selector) |
Marek Vasut | 51bd694 | 2010-06-13 17:25:51 +0200 | [diff] [blame] | 55 | { |
| 56 | struct isl_pmic *pmic = rdev_get_drvdata(dev); |
Axel Lin | dde2b72 | 2012-05-16 10:11:13 +0800 | [diff] [blame] | 57 | int err; |
Mark Brown | 3a93f2a | 2010-11-10 14:38:29 +0000 | [diff] [blame] | 58 | |
Marek Vasut | 51bd694 | 2010-06-13 17:25:51 +0200 | [diff] [blame] | 59 | mutex_lock(&pmic->mtx); |
| 60 | |
Axel Lin | dde2b72 | 2012-05-16 10:11:13 +0800 | [diff] [blame] | 61 | err = i2c_smbus_write_byte(pmic->client, selector); |
Marek Vasut | 51bd694 | 2010-06-13 17:25:51 +0200 | [diff] [blame] | 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 | |
Marek Vasut | 51bd694 | 2010-06-13 17:25:51 +0200 | [diff] [blame] | 69 | static struct regulator_ops isl_core_ops = { |
Axel Lin | a9b2899 | 2012-05-16 10:10:22 +0800 | [diff] [blame] | 70 | .get_voltage_sel = isl6271a_get_voltage_sel, |
Axel Lin | dde2b72 | 2012-05-16 10:11:13 +0800 | [diff] [blame] | 71 | .set_voltage_sel = isl6271a_set_voltage_sel, |
Axel Lin | 2b7a7a4 | 2012-05-16 10:09:27 +0800 | [diff] [blame] | 72 | .list_voltage = regulator_list_voltage_linear, |
Axel Lin | dde2b72 | 2012-05-16 10:11:13 +0800 | [diff] [blame] | 73 | .map_voltage = regulator_map_voltage_linear, |
Marek Vasut | 51bd694 | 2010-06-13 17:25:51 +0200 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | static int isl6271a_get_fixed_voltage(struct regulator_dev *dev) |
| 77 | { |
| 78 | int id = rdev_get_id(dev); |
| 79 | return (id == 1) ? 1100000 : 1300000; |
| 80 | } |
| 81 | |
| 82 | static int isl6271a_list_fixed_voltage(struct regulator_dev *dev, unsigned selector) |
| 83 | { |
| 84 | int id = rdev_get_id(dev); |
| 85 | return (id == 1) ? 1100000 : 1300000; |
| 86 | } |
| 87 | |
| 88 | static struct regulator_ops isl_fixed_ops = { |
| 89 | .get_voltage = isl6271a_get_fixed_voltage, |
| 90 | .list_voltage = isl6271a_list_fixed_voltage, |
| 91 | }; |
| 92 | |
Axel Lin | 4b65e15 | 2012-04-05 11:58:06 +0800 | [diff] [blame] | 93 | static const struct regulator_desc isl_rd[] = { |
Marek Vasut | 51bd694 | 2010-06-13 17:25:51 +0200 | [diff] [blame] | 94 | { |
| 95 | .name = "Core Buck", |
| 96 | .id = 0, |
| 97 | .n_voltages = 16, |
| 98 | .ops = &isl_core_ops, |
| 99 | .type = REGULATOR_VOLTAGE, |
| 100 | .owner = THIS_MODULE, |
Axel Lin | 2b7a7a4 | 2012-05-16 10:09:27 +0800 | [diff] [blame] | 101 | .min_uV = ISL6271A_VOLTAGE_MIN, |
| 102 | .uV_step = ISL6271A_VOLTAGE_STEP, |
Marek Vasut | 51bd694 | 2010-06-13 17:25:51 +0200 | [diff] [blame] | 103 | }, { |
| 104 | .name = "LDO1", |
| 105 | .id = 1, |
| 106 | .n_voltages = 1, |
| 107 | .ops = &isl_fixed_ops, |
| 108 | .type = REGULATOR_VOLTAGE, |
| 109 | .owner = THIS_MODULE, |
| 110 | }, { |
| 111 | .name = "LDO2", |
| 112 | .id = 2, |
| 113 | .n_voltages = 1, |
| 114 | .ops = &isl_fixed_ops, |
| 115 | .type = REGULATOR_VOLTAGE, |
| 116 | .owner = THIS_MODULE, |
| 117 | }, |
| 118 | }; |
| 119 | |
| 120 | static int __devinit isl6271a_probe(struct i2c_client *i2c, |
| 121 | const struct i2c_device_id *id) |
| 122 | { |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 123 | struct regulator_config config = { }; |
Marek Vasut | 51bd694 | 2010-06-13 17:25:51 +0200 | [diff] [blame] | 124 | struct regulator_init_data *init_data = i2c->dev.platform_data; |
| 125 | struct isl_pmic *pmic; |
| 126 | int err, i; |
| 127 | |
| 128 | if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
| 129 | return -EIO; |
| 130 | |
Axel Lin | ef6bd5a | 2012-04-11 23:05:49 +0800 | [diff] [blame] | 131 | pmic = devm_kzalloc(&i2c->dev, sizeof(struct isl_pmic), GFP_KERNEL); |
Marek Vasut | 51bd694 | 2010-06-13 17:25:51 +0200 | [diff] [blame] | 132 | if (!pmic) |
| 133 | return -ENOMEM; |
| 134 | |
| 135 | pmic->client = i2c; |
| 136 | |
| 137 | mutex_init(&pmic->mtx); |
| 138 | |
| 139 | for (i = 0; i < 3; i++) { |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 140 | config.dev = &i2c->dev; |
| 141 | if (i == 0) |
| 142 | config.init_data = init_data; |
| 143 | else |
| 144 | config.init_data = 0; |
| 145 | config.driver_data = pmic; |
| 146 | |
| 147 | pmic->rdev[i] = regulator_register(&isl_rd[i], &config); |
Marek Vasut | 51bd694 | 2010-06-13 17:25:51 +0200 | [diff] [blame] | 148 | if (IS_ERR(pmic->rdev[i])) { |
| 149 | dev_err(&i2c->dev, "failed to register %s\n", id->name); |
roel kluin | fa63bd4 | 2010-12-31 16:26:47 +0100 | [diff] [blame] | 150 | err = PTR_ERR(pmic->rdev[i]); |
Marek Vasut | 51bd694 | 2010-06-13 17:25:51 +0200 | [diff] [blame] | 151 | goto error; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | i2c_set_clientdata(i2c, pmic); |
| 156 | |
| 157 | return 0; |
| 158 | |
| 159 | error: |
| 160 | while (--i >= 0) |
| 161 | regulator_unregister(pmic->rdev[i]); |
Marek Vasut | 51bd694 | 2010-06-13 17:25:51 +0200 | [diff] [blame] | 162 | return err; |
| 163 | } |
| 164 | |
| 165 | static int __devexit isl6271a_remove(struct i2c_client *i2c) |
| 166 | { |
| 167 | struct isl_pmic *pmic = i2c_get_clientdata(i2c); |
| 168 | int i; |
| 169 | |
Marek Vasut | 51bd694 | 2010-06-13 17:25:51 +0200 | [diff] [blame] | 170 | for (i = 0; i < 3; i++) |
| 171 | regulator_unregister(pmic->rdev[i]); |
Marek Vasut | 51bd694 | 2010-06-13 17:25:51 +0200 | [diff] [blame] | 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | static const struct i2c_device_id isl6271a_id[] = { |
| 176 | {.name = "isl6271a", 0 }, |
| 177 | { }, |
| 178 | }; |
| 179 | |
| 180 | MODULE_DEVICE_TABLE(i2c, isl6271a_id); |
| 181 | |
| 182 | static struct i2c_driver isl6271a_i2c_driver = { |
| 183 | .driver = { |
| 184 | .name = "isl6271a", |
| 185 | .owner = THIS_MODULE, |
| 186 | }, |
| 187 | .probe = isl6271a_probe, |
| 188 | .remove = __devexit_p(isl6271a_remove), |
| 189 | .id_table = isl6271a_id, |
| 190 | }; |
| 191 | |
| 192 | static int __init isl6271a_init(void) |
| 193 | { |
| 194 | return i2c_add_driver(&isl6271a_i2c_driver); |
| 195 | } |
| 196 | |
| 197 | static void __exit isl6271a_cleanup(void) |
| 198 | { |
| 199 | i2c_del_driver(&isl6271a_i2c_driver); |
| 200 | } |
| 201 | |
| 202 | subsys_initcall(isl6271a_init); |
| 203 | module_exit(isl6271a_cleanup); |
| 204 | |
| 205 | MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>"); |
| 206 | MODULE_DESCRIPTION("Intersil ISL6271A voltage regulator driver"); |
| 207 | MODULE_LICENSE("GPL v2"); |