MyungJoo Ham | 202f4f5 | 2010-08-20 14:43:56 +0900 | [diff] [blame] | 1 | /* |
| 2 | * max8952.c - Voltage and current regulation for the Maxim 8952 |
| 3 | * |
| 4 | * Copyright (C) 2010 Samsung Electronics |
| 5 | * MyungJoo Ham <myungjoo.ham@samsung.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | */ |
| 21 | |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/init.h> |
| 24 | #include <linux/i2c.h> |
| 25 | #include <linux/err.h> |
| 26 | #include <linux/platform_device.h> |
| 27 | #include <linux/regulator/driver.h> |
| 28 | #include <linux/regulator/max8952.h> |
MyungJoo Ham | 202f4f5 | 2010-08-20 14:43:56 +0900 | [diff] [blame] | 29 | #include <linux/gpio.h> |
| 30 | #include <linux/io.h> |
| 31 | #include <linux/slab.h> |
| 32 | |
| 33 | /* Registers */ |
| 34 | enum { |
| 35 | MAX8952_REG_MODE0, |
| 36 | MAX8952_REG_MODE1, |
| 37 | MAX8952_REG_MODE2, |
| 38 | MAX8952_REG_MODE3, |
| 39 | MAX8952_REG_CONTROL, |
| 40 | MAX8952_REG_SYNC, |
| 41 | MAX8952_REG_RAMP, |
| 42 | MAX8952_REG_CHIP_ID1, |
| 43 | MAX8952_REG_CHIP_ID2, |
| 44 | }; |
| 45 | |
| 46 | struct max8952_data { |
| 47 | struct i2c_client *client; |
| 48 | struct device *dev; |
MyungJoo Ham | 202f4f5 | 2010-08-20 14:43:56 +0900 | [diff] [blame] | 49 | struct max8952_platform_data *pdata; |
| 50 | struct regulator_dev *rdev; |
| 51 | |
| 52 | bool vid0; |
| 53 | bool vid1; |
| 54 | bool en; |
| 55 | }; |
| 56 | |
| 57 | static int max8952_read_reg(struct max8952_data *max8952, u8 reg) |
| 58 | { |
| 59 | int ret = i2c_smbus_read_byte_data(max8952->client, reg); |
| 60 | if (ret > 0) |
| 61 | ret &= 0xff; |
| 62 | |
| 63 | return ret; |
| 64 | } |
| 65 | |
| 66 | static int max8952_write_reg(struct max8952_data *max8952, |
| 67 | u8 reg, u8 value) |
| 68 | { |
| 69 | return i2c_smbus_write_byte_data(max8952->client, reg, value); |
| 70 | } |
| 71 | |
| 72 | static int max8952_voltage(struct max8952_data *max8952, u8 mode) |
| 73 | { |
| 74 | return (max8952->pdata->dvs_mode[mode] * 10 + 770) * 1000; |
| 75 | } |
| 76 | |
| 77 | static int max8952_list_voltage(struct regulator_dev *rdev, |
| 78 | unsigned int selector) |
| 79 | { |
| 80 | struct max8952_data *max8952 = rdev_get_drvdata(rdev); |
| 81 | |
| 82 | if (rdev_get_id(rdev) != 0) |
| 83 | return -EINVAL; |
| 84 | |
| 85 | return max8952_voltage(max8952, selector); |
| 86 | } |
| 87 | |
| 88 | static int max8952_is_enabled(struct regulator_dev *rdev) |
| 89 | { |
| 90 | struct max8952_data *max8952 = rdev_get_drvdata(rdev); |
| 91 | return max8952->en; |
| 92 | } |
| 93 | |
| 94 | static int max8952_enable(struct regulator_dev *rdev) |
| 95 | { |
| 96 | struct max8952_data *max8952 = rdev_get_drvdata(rdev); |
| 97 | |
| 98 | /* If not valid, assume "ALWAYS_HIGH" */ |
| 99 | if (gpio_is_valid(max8952->pdata->gpio_en)) |
| 100 | gpio_set_value(max8952->pdata->gpio_en, 1); |
| 101 | |
| 102 | max8952->en = true; |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | static int max8952_disable(struct regulator_dev *rdev) |
| 107 | { |
| 108 | struct max8952_data *max8952 = rdev_get_drvdata(rdev); |
| 109 | |
| 110 | /* If not valid, assume "ALWAYS_HIGH" -> not permitted */ |
| 111 | if (gpio_is_valid(max8952->pdata->gpio_en)) |
| 112 | gpio_set_value(max8952->pdata->gpio_en, 0); |
| 113 | else |
| 114 | return -EPERM; |
| 115 | |
| 116 | max8952->en = false; |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | static int max8952_get_voltage(struct regulator_dev *rdev) |
| 121 | { |
| 122 | struct max8952_data *max8952 = rdev_get_drvdata(rdev); |
| 123 | u8 vid = 0; |
| 124 | |
| 125 | if (max8952->vid0) |
| 126 | vid += 1; |
| 127 | if (max8952->vid1) |
| 128 | vid += 2; |
| 129 | |
| 130 | return max8952_voltage(max8952, vid); |
| 131 | } |
| 132 | |
| 133 | static int max8952_set_voltage(struct regulator_dev *rdev, |
Mark Brown | 3a93f2a | 2010-11-10 14:38:29 +0000 | [diff] [blame] | 134 | int min_uV, int max_uV, unsigned *selector) |
MyungJoo Ham | 202f4f5 | 2010-08-20 14:43:56 +0900 | [diff] [blame] | 135 | { |
| 136 | struct max8952_data *max8952 = rdev_get_drvdata(rdev); |
Axel Lin | ec10b0e | 2010-10-26 07:55:54 +0800 | [diff] [blame] | 137 | s8 vid = -1, i; |
MyungJoo Ham | 202f4f5 | 2010-08-20 14:43:56 +0900 | [diff] [blame] | 138 | |
| 139 | if (!gpio_is_valid(max8952->pdata->gpio_vid0) || |
Jonghwan Choi | e2cf313 | 2011-06-15 17:54:12 +0900 | [diff] [blame] | 140 | !gpio_is_valid(max8952->pdata->gpio_vid1)) { |
MyungJoo Ham | 202f4f5 | 2010-08-20 14:43:56 +0900 | [diff] [blame] | 141 | /* DVS not supported */ |
| 142 | return -EPERM; |
| 143 | } |
| 144 | |
| 145 | for (i = 0; i < MAX8952_NUM_DVS_MODE; i++) { |
| 146 | int volt = max8952_voltage(max8952, i); |
| 147 | |
| 148 | /* Set the voltage as low as possible within the range */ |
| 149 | if (volt <= max_uV && volt >= min_uV) |
| 150 | if (vid == -1 || max8952_voltage(max8952, vid) > volt) |
| 151 | vid = i; |
| 152 | } |
| 153 | |
| 154 | if (vid >= 0 && vid < MAX8952_NUM_DVS_MODE) { |
| 155 | max8952->vid0 = (vid % 2 == 1); |
| 156 | max8952->vid1 = (((vid >> 1) % 2) == 1); |
Mark Brown | 3a93f2a | 2010-11-10 14:38:29 +0000 | [diff] [blame] | 157 | *selector = vid; |
MyungJoo Ham | 202f4f5 | 2010-08-20 14:43:56 +0900 | [diff] [blame] | 158 | gpio_set_value(max8952->pdata->gpio_vid0, max8952->vid0); |
| 159 | gpio_set_value(max8952->pdata->gpio_vid1, max8952->vid1); |
| 160 | } else |
| 161 | return -EINVAL; |
| 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | static struct regulator_ops max8952_ops = { |
| 167 | .list_voltage = max8952_list_voltage, |
| 168 | .is_enabled = max8952_is_enabled, |
| 169 | .enable = max8952_enable, |
| 170 | .disable = max8952_disable, |
| 171 | .get_voltage = max8952_get_voltage, |
| 172 | .set_voltage = max8952_set_voltage, |
| 173 | .set_suspend_disable = max8952_disable, |
| 174 | }; |
| 175 | |
| 176 | static struct regulator_desc regulator = { |
| 177 | .name = "MAX8952_VOUT", |
| 178 | .id = 0, |
| 179 | .n_voltages = MAX8952_NUM_DVS_MODE, |
| 180 | .ops = &max8952_ops, |
| 181 | .type = REGULATOR_VOLTAGE, |
| 182 | .owner = THIS_MODULE, |
| 183 | }; |
| 184 | |
| 185 | static int __devinit max8952_pmic_probe(struct i2c_client *client, |
| 186 | const struct i2c_device_id *i2c_id) |
| 187 | { |
| 188 | struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent); |
| 189 | struct max8952_platform_data *pdata = client->dev.platform_data; |
| 190 | struct max8952_data *max8952; |
| 191 | |
| 192 | int ret = 0, err = 0; |
| 193 | |
| 194 | if (!pdata) { |
| 195 | dev_err(&client->dev, "Require the platform data\n"); |
| 196 | return -EINVAL; |
| 197 | } |
| 198 | |
| 199 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE)) |
| 200 | return -EIO; |
| 201 | |
| 202 | max8952 = kzalloc(sizeof(struct max8952_data), GFP_KERNEL); |
| 203 | if (!max8952) |
| 204 | return -ENOMEM; |
| 205 | |
| 206 | max8952->client = client; |
| 207 | max8952->dev = &client->dev; |
| 208 | max8952->pdata = pdata; |
MyungJoo Ham | 202f4f5 | 2010-08-20 14:43:56 +0900 | [diff] [blame] | 209 | |
| 210 | max8952->rdev = regulator_register(®ulator, max8952->dev, |
Rajendra Nayak | 2c043bc | 2011-11-18 16:47:19 +0530 | [diff] [blame] | 211 | &pdata->reg_data, max8952, NULL); |
MyungJoo Ham | 202f4f5 | 2010-08-20 14:43:56 +0900 | [diff] [blame] | 212 | |
Axel Lin | da05738 | 2010-10-25 10:11:07 +0800 | [diff] [blame] | 213 | if (IS_ERR(max8952->rdev)) { |
| 214 | ret = PTR_ERR(max8952->rdev); |
MyungJoo Ham | 202f4f5 | 2010-08-20 14:43:56 +0900 | [diff] [blame] | 215 | dev_err(max8952->dev, "regulator init failed (%d)\n", ret); |
Axel Lin | da05738 | 2010-10-25 10:11:07 +0800 | [diff] [blame] | 216 | goto err_reg; |
| 217 | } |
MyungJoo Ham | 202f4f5 | 2010-08-20 14:43:56 +0900 | [diff] [blame] | 218 | |
| 219 | max8952->en = !!(pdata->reg_data.constraints.boot_on); |
| 220 | max8952->vid0 = (pdata->default_mode % 2) == 1; |
| 221 | max8952->vid1 = ((pdata->default_mode >> 1) % 2) == 1; |
| 222 | |
| 223 | if (gpio_is_valid(pdata->gpio_en)) { |
| 224 | if (!gpio_request(pdata->gpio_en, "MAX8952 EN")) |
| 225 | gpio_direction_output(pdata->gpio_en, max8952->en); |
| 226 | else |
| 227 | err = 1; |
| 228 | } else |
| 229 | err = 2; |
| 230 | |
| 231 | if (err) { |
| 232 | dev_info(max8952->dev, "EN gpio invalid: assume that EN" |
| 233 | "is always High\n"); |
| 234 | max8952->en = 1; |
| 235 | pdata->gpio_en = -1; /* Mark invalid */ |
| 236 | } |
| 237 | |
| 238 | err = 0; |
| 239 | |
| 240 | if (gpio_is_valid(pdata->gpio_vid0) && |
| 241 | gpio_is_valid(pdata->gpio_vid1)) { |
| 242 | if (!gpio_request(pdata->gpio_vid0, "MAX8952 VID0")) |
| 243 | gpio_direction_output(pdata->gpio_vid0, |
| 244 | (pdata->default_mode) % 2); |
| 245 | else |
| 246 | err = 1; |
| 247 | |
| 248 | if (!gpio_request(pdata->gpio_vid1, "MAX8952 VID1")) |
| 249 | gpio_direction_output(pdata->gpio_vid1, |
| 250 | (pdata->default_mode >> 1) % 2); |
| 251 | else { |
| 252 | if (!err) |
| 253 | gpio_free(pdata->gpio_vid0); |
| 254 | err = 2; |
| 255 | } |
| 256 | |
| 257 | } else |
| 258 | err = 3; |
| 259 | |
| 260 | if (err) { |
| 261 | dev_warn(max8952->dev, "VID0/1 gpio invalid: " |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 262 | "DVS not available.\n"); |
MyungJoo Ham | 202f4f5 | 2010-08-20 14:43:56 +0900 | [diff] [blame] | 263 | max8952->vid0 = 0; |
| 264 | max8952->vid1 = 0; |
| 265 | /* Mark invalid */ |
| 266 | pdata->gpio_vid0 = -1; |
| 267 | pdata->gpio_vid1 = -1; |
| 268 | |
| 269 | /* Disable Pulldown of EN only */ |
| 270 | max8952_write_reg(max8952, MAX8952_REG_CONTROL, 0x60); |
| 271 | |
| 272 | dev_err(max8952->dev, "DVS modes disabled because VID0 and VID1" |
| 273 | " do not have proper controls.\n"); |
| 274 | } else { |
| 275 | /* |
| 276 | * Disable Pulldown on EN, VID0, VID1 to reduce |
| 277 | * leakage current of MAX8952 assuming that MAX8952 |
| 278 | * is turned on (EN==1). Note that without having VID0/1 |
| 279 | * properly connected, turning pulldown off can be |
| 280 | * problematic. Thus, turn this off only when they are |
| 281 | * controllable by GPIO. |
| 282 | */ |
| 283 | max8952_write_reg(max8952, MAX8952_REG_CONTROL, 0x0); |
| 284 | } |
| 285 | |
| 286 | max8952_write_reg(max8952, MAX8952_REG_MODE0, |
| 287 | (max8952_read_reg(max8952, |
| 288 | MAX8952_REG_MODE0) & 0xC0) | |
| 289 | (pdata->dvs_mode[0] & 0x3F)); |
| 290 | max8952_write_reg(max8952, MAX8952_REG_MODE1, |
| 291 | (max8952_read_reg(max8952, |
| 292 | MAX8952_REG_MODE1) & 0xC0) | |
| 293 | (pdata->dvs_mode[1] & 0x3F)); |
| 294 | max8952_write_reg(max8952, MAX8952_REG_MODE2, |
| 295 | (max8952_read_reg(max8952, |
| 296 | MAX8952_REG_MODE2) & 0xC0) | |
| 297 | (pdata->dvs_mode[2] & 0x3F)); |
| 298 | max8952_write_reg(max8952, MAX8952_REG_MODE3, |
| 299 | (max8952_read_reg(max8952, |
| 300 | MAX8952_REG_MODE3) & 0xC0) | |
| 301 | (pdata->dvs_mode[3] & 0x3F)); |
| 302 | |
| 303 | max8952_write_reg(max8952, MAX8952_REG_SYNC, |
| 304 | (max8952_read_reg(max8952, MAX8952_REG_SYNC) & 0x3F) | |
| 305 | ((pdata->sync_freq & 0x3) << 6)); |
| 306 | max8952_write_reg(max8952, MAX8952_REG_RAMP, |
| 307 | (max8952_read_reg(max8952, MAX8952_REG_RAMP) & 0x1F) | |
| 308 | ((pdata->ramp_speed & 0x7) << 5)); |
| 309 | |
| 310 | i2c_set_clientdata(client, max8952); |
| 311 | |
Axel Lin | da05738 | 2010-10-25 10:11:07 +0800 | [diff] [blame] | 312 | return 0; |
| 313 | |
| 314 | err_reg: |
| 315 | kfree(max8952); |
MyungJoo Ham | 202f4f5 | 2010-08-20 14:43:56 +0900 | [diff] [blame] | 316 | return ret; |
| 317 | } |
| 318 | |
| 319 | static int __devexit max8952_pmic_remove(struct i2c_client *client) |
| 320 | { |
| 321 | struct max8952_data *max8952 = i2c_get_clientdata(client); |
| 322 | struct max8952_platform_data *pdata = max8952->pdata; |
| 323 | struct regulator_dev *rdev = max8952->rdev; |
| 324 | |
| 325 | regulator_unregister(rdev); |
| 326 | |
| 327 | gpio_free(pdata->gpio_vid0); |
| 328 | gpio_free(pdata->gpio_vid1); |
| 329 | gpio_free(pdata->gpio_en); |
| 330 | |
| 331 | kfree(max8952); |
| 332 | return 0; |
| 333 | } |
| 334 | |
| 335 | static const struct i2c_device_id max8952_ids[] = { |
| 336 | { "max8952", 0 }, |
| 337 | { }, |
| 338 | }; |
| 339 | MODULE_DEVICE_TABLE(i2c, max8952_ids); |
| 340 | |
| 341 | static struct i2c_driver max8952_pmic_driver = { |
| 342 | .probe = max8952_pmic_probe, |
| 343 | .remove = __devexit_p(max8952_pmic_remove), |
| 344 | .driver = { |
| 345 | .name = "max8952", |
| 346 | }, |
| 347 | .id_table = max8952_ids, |
| 348 | }; |
| 349 | |
| 350 | static int __init max8952_pmic_init(void) |
| 351 | { |
| 352 | return i2c_add_driver(&max8952_pmic_driver); |
| 353 | } |
| 354 | subsys_initcall(max8952_pmic_init); |
| 355 | |
| 356 | static void __exit max8952_pmic_exit(void) |
| 357 | { |
| 358 | i2c_del_driver(&max8952_pmic_driver); |
| 359 | } |
| 360 | module_exit(max8952_pmic_exit); |
| 361 | |
| 362 | MODULE_DESCRIPTION("MAXIM 8952 voltage regulator driver"); |
| 363 | MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>"); |
| 364 | MODULE_LICENSE("GPL"); |