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> |
| 25 | #include <linux/delay.h> |
| 26 | #include <linux/slab.h> |
| 27 | |
| 28 | #define ISL6271A_VOLTAGE_MIN 850000 |
| 29 | #define ISL6271A_VOLTAGE_MAX 1600000 |
| 30 | #define ISL6271A_VOLTAGE_STEP 50000 |
| 31 | |
| 32 | /* PMIC details */ |
| 33 | struct isl_pmic { |
| 34 | struct i2c_client *client; |
| 35 | struct regulator_dev *rdev[3]; |
| 36 | struct mutex mtx; |
| 37 | }; |
| 38 | |
| 39 | static int isl6271a_get_voltage(struct regulator_dev *dev) |
| 40 | { |
| 41 | struct isl_pmic *pmic = rdev_get_drvdata(dev); |
| 42 | int idx, data; |
| 43 | |
| 44 | mutex_lock(&pmic->mtx); |
| 45 | |
| 46 | idx = i2c_smbus_read_byte(pmic->client); |
| 47 | if (idx < 0) { |
| 48 | dev_err(&pmic->client->dev, "Error getting voltage\n"); |
| 49 | data = idx; |
| 50 | goto out; |
| 51 | } |
| 52 | |
| 53 | /* Convert the data from chip to microvolts */ |
| 54 | data = ISL6271A_VOLTAGE_MIN + (ISL6271A_VOLTAGE_STEP * (idx & 0xf)); |
| 55 | |
| 56 | out: |
| 57 | mutex_unlock(&pmic->mtx); |
| 58 | return data; |
| 59 | } |
| 60 | |
Mark Brown | 3a93f2a | 2010-11-10 14:38:29 +0000 | [diff] [blame] | 61 | static int isl6271a_set_voltage(struct regulator_dev *dev, |
| 62 | int minuV, int maxuV, |
| 63 | unsigned *selector) |
Marek Vasut | 51bd694 | 2010-06-13 17:25:51 +0200 | [diff] [blame] | 64 | { |
| 65 | struct isl_pmic *pmic = rdev_get_drvdata(dev); |
Axel Lin | 48ee116 | 2012-03-02 09:19:02 +0800 | [diff] [blame] | 66 | int err, data; |
Marek Vasut | 51bd694 | 2010-06-13 17:25:51 +0200 | [diff] [blame] | 67 | |
| 68 | if (minuV < ISL6271A_VOLTAGE_MIN || minuV > ISL6271A_VOLTAGE_MAX) |
| 69 | return -EINVAL; |
| 70 | if (maxuV < ISL6271A_VOLTAGE_MIN || maxuV > ISL6271A_VOLTAGE_MAX) |
| 71 | return -EINVAL; |
| 72 | |
Axel Lin | 48ee116 | 2012-03-02 09:19:02 +0800 | [diff] [blame] | 73 | data = DIV_ROUND_UP(minuV - ISL6271A_VOLTAGE_MIN, |
| 74 | ISL6271A_VOLTAGE_STEP); |
Mark Brown | 3a93f2a | 2010-11-10 14:38:29 +0000 | [diff] [blame] | 75 | *selector = data; |
| 76 | |
Marek Vasut | 51bd694 | 2010-06-13 17:25:51 +0200 | [diff] [blame] | 77 | mutex_lock(&pmic->mtx); |
| 78 | |
| 79 | err = i2c_smbus_write_byte(pmic->client, data); |
| 80 | if (err < 0) |
| 81 | dev_err(&pmic->client->dev, "Error setting voltage\n"); |
| 82 | |
| 83 | mutex_unlock(&pmic->mtx); |
| 84 | return err; |
| 85 | } |
| 86 | |
| 87 | static int isl6271a_list_voltage(struct regulator_dev *dev, unsigned selector) |
| 88 | { |
| 89 | return ISL6271A_VOLTAGE_MIN + (ISL6271A_VOLTAGE_STEP * selector); |
| 90 | } |
| 91 | |
| 92 | static struct regulator_ops isl_core_ops = { |
| 93 | .get_voltage = isl6271a_get_voltage, |
| 94 | .set_voltage = isl6271a_set_voltage, |
| 95 | .list_voltage = isl6271a_list_voltage, |
| 96 | }; |
| 97 | |
| 98 | static int isl6271a_get_fixed_voltage(struct regulator_dev *dev) |
| 99 | { |
| 100 | int id = rdev_get_id(dev); |
| 101 | return (id == 1) ? 1100000 : 1300000; |
| 102 | } |
| 103 | |
| 104 | static int isl6271a_list_fixed_voltage(struct regulator_dev *dev, unsigned selector) |
| 105 | { |
| 106 | int id = rdev_get_id(dev); |
| 107 | return (id == 1) ? 1100000 : 1300000; |
| 108 | } |
| 109 | |
| 110 | static struct regulator_ops isl_fixed_ops = { |
| 111 | .get_voltage = isl6271a_get_fixed_voltage, |
| 112 | .list_voltage = isl6271a_list_fixed_voltage, |
| 113 | }; |
| 114 | |
Axel Lin | 4b65e15 | 2012-04-05 11:58:06 +0800 | [diff] [blame] | 115 | static const struct regulator_desc isl_rd[] = { |
Marek Vasut | 51bd694 | 2010-06-13 17:25:51 +0200 | [diff] [blame] | 116 | { |
| 117 | .name = "Core Buck", |
| 118 | .id = 0, |
| 119 | .n_voltages = 16, |
| 120 | .ops = &isl_core_ops, |
| 121 | .type = REGULATOR_VOLTAGE, |
| 122 | .owner = THIS_MODULE, |
| 123 | }, { |
| 124 | .name = "LDO1", |
| 125 | .id = 1, |
| 126 | .n_voltages = 1, |
| 127 | .ops = &isl_fixed_ops, |
| 128 | .type = REGULATOR_VOLTAGE, |
| 129 | .owner = THIS_MODULE, |
| 130 | }, { |
| 131 | .name = "LDO2", |
| 132 | .id = 2, |
| 133 | .n_voltages = 1, |
| 134 | .ops = &isl_fixed_ops, |
| 135 | .type = REGULATOR_VOLTAGE, |
| 136 | .owner = THIS_MODULE, |
| 137 | }, |
| 138 | }; |
| 139 | |
| 140 | static int __devinit isl6271a_probe(struct i2c_client *i2c, |
| 141 | const struct i2c_device_id *id) |
| 142 | { |
| 143 | struct regulator_init_data *init_data = i2c->dev.platform_data; |
| 144 | struct isl_pmic *pmic; |
| 145 | int err, i; |
| 146 | |
| 147 | if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
| 148 | return -EIO; |
| 149 | |
Marek Vasut | 51bd694 | 2010-06-13 17:25:51 +0200 | [diff] [blame] | 150 | pmic = kzalloc(sizeof(struct isl_pmic), GFP_KERNEL); |
| 151 | if (!pmic) |
| 152 | return -ENOMEM; |
| 153 | |
| 154 | pmic->client = i2c; |
| 155 | |
| 156 | mutex_init(&pmic->mtx); |
| 157 | |
| 158 | for (i = 0; i < 3; i++) { |
Axel Lin | b9e5d11 | 2010-09-01 13:09:41 +0800 | [diff] [blame] | 159 | pmic->rdev[i] = regulator_register(&isl_rd[i], &i2c->dev, |
Rajendra Nayak | 2c043bc | 2011-11-18 16:47:19 +0530 | [diff] [blame] | 160 | init_data, pmic, NULL); |
Marek Vasut | 51bd694 | 2010-06-13 17:25:51 +0200 | [diff] [blame] | 161 | if (IS_ERR(pmic->rdev[i])) { |
| 162 | dev_err(&i2c->dev, "failed to register %s\n", id->name); |
roel kluin | fa63bd4 | 2010-12-31 16:26:47 +0100 | [diff] [blame] | 163 | err = PTR_ERR(pmic->rdev[i]); |
Marek Vasut | 51bd694 | 2010-06-13 17:25:51 +0200 | [diff] [blame] | 164 | goto error; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | i2c_set_clientdata(i2c, pmic); |
| 169 | |
| 170 | return 0; |
| 171 | |
| 172 | error: |
| 173 | while (--i >= 0) |
| 174 | regulator_unregister(pmic->rdev[i]); |
| 175 | |
| 176 | kfree(pmic); |
| 177 | return err; |
| 178 | } |
| 179 | |
| 180 | static int __devexit isl6271a_remove(struct i2c_client *i2c) |
| 181 | { |
| 182 | struct isl_pmic *pmic = i2c_get_clientdata(i2c); |
| 183 | int i; |
| 184 | |
Marek Vasut | 51bd694 | 2010-06-13 17:25:51 +0200 | [diff] [blame] | 185 | for (i = 0; i < 3; i++) |
| 186 | regulator_unregister(pmic->rdev[i]); |
| 187 | |
| 188 | kfree(pmic); |
| 189 | |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | static const struct i2c_device_id isl6271a_id[] = { |
| 194 | {.name = "isl6271a", 0 }, |
| 195 | { }, |
| 196 | }; |
| 197 | |
| 198 | MODULE_DEVICE_TABLE(i2c, isl6271a_id); |
| 199 | |
| 200 | static struct i2c_driver isl6271a_i2c_driver = { |
| 201 | .driver = { |
| 202 | .name = "isl6271a", |
| 203 | .owner = THIS_MODULE, |
| 204 | }, |
| 205 | .probe = isl6271a_probe, |
| 206 | .remove = __devexit_p(isl6271a_remove), |
| 207 | .id_table = isl6271a_id, |
| 208 | }; |
| 209 | |
| 210 | static int __init isl6271a_init(void) |
| 211 | { |
| 212 | return i2c_add_driver(&isl6271a_i2c_driver); |
| 213 | } |
| 214 | |
| 215 | static void __exit isl6271a_cleanup(void) |
| 216 | { |
| 217 | i2c_del_driver(&isl6271a_i2c_driver); |
| 218 | } |
| 219 | |
| 220 | subsys_initcall(isl6271a_init); |
| 221 | module_exit(isl6271a_cleanup); |
| 222 | |
| 223 | MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>"); |
| 224 | MODULE_DESCRIPTION("Intersil ISL6271A voltage regulator driver"); |
| 225 | MODULE_LICENSE("GPL v2"); |