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 | |
Axel Lin | 6ea67d0 | 2012-03-26 09:20:58 +0800 | [diff] [blame] | 133 | static int max8952_set_voltage_sel(struct regulator_dev *rdev, |
| 134 | unsigned selector) |
MyungJoo Ham | 202f4f5 | 2010-08-20 14:43:56 +0900 | [diff] [blame] | 135 | { |
| 136 | struct max8952_data *max8952 = rdev_get_drvdata(rdev); |
MyungJoo Ham | 202f4f5 | 2010-08-20 14:43:56 +0900 | [diff] [blame] | 137 | |
| 138 | if (!gpio_is_valid(max8952->pdata->gpio_vid0) || |
Jonghwan Choi | e2cf313 | 2011-06-15 17:54:12 +0900 | [diff] [blame] | 139 | !gpio_is_valid(max8952->pdata->gpio_vid1)) { |
MyungJoo Ham | 202f4f5 | 2010-08-20 14:43:56 +0900 | [diff] [blame] | 140 | /* DVS not supported */ |
| 141 | return -EPERM; |
| 142 | } |
| 143 | |
Axel Lin | 6ea67d0 | 2012-03-26 09:20:58 +0800 | [diff] [blame] | 144 | max8952->vid0 = selector & 0x1; |
| 145 | max8952->vid1 = (selector >> 1) & 0x1; |
| 146 | gpio_set_value(max8952->pdata->gpio_vid0, max8952->vid0); |
| 147 | gpio_set_value(max8952->pdata->gpio_vid1, max8952->vid1); |
MyungJoo Ham | 202f4f5 | 2010-08-20 14:43:56 +0900 | [diff] [blame] | 148 | |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | static struct regulator_ops max8952_ops = { |
| 153 | .list_voltage = max8952_list_voltage, |
| 154 | .is_enabled = max8952_is_enabled, |
| 155 | .enable = max8952_enable, |
| 156 | .disable = max8952_disable, |
| 157 | .get_voltage = max8952_get_voltage, |
Axel Lin | 6ea67d0 | 2012-03-26 09:20:58 +0800 | [diff] [blame] | 158 | .set_voltage_sel = max8952_set_voltage_sel, |
MyungJoo Ham | 202f4f5 | 2010-08-20 14:43:56 +0900 | [diff] [blame] | 159 | .set_suspend_disable = max8952_disable, |
| 160 | }; |
| 161 | |
Axel Lin | bd6ff0d | 2012-04-06 08:29:49 +0800 | [diff] [blame] | 162 | static const struct regulator_desc regulator = { |
MyungJoo Ham | 202f4f5 | 2010-08-20 14:43:56 +0900 | [diff] [blame] | 163 | .name = "MAX8952_VOUT", |
| 164 | .id = 0, |
| 165 | .n_voltages = MAX8952_NUM_DVS_MODE, |
| 166 | .ops = &max8952_ops, |
| 167 | .type = REGULATOR_VOLTAGE, |
| 168 | .owner = THIS_MODULE, |
| 169 | }; |
| 170 | |
| 171 | static int __devinit max8952_pmic_probe(struct i2c_client *client, |
| 172 | const struct i2c_device_id *i2c_id) |
| 173 | { |
| 174 | struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent); |
| 175 | struct max8952_platform_data *pdata = client->dev.platform_data; |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 176 | struct regulator_config config = { }; |
MyungJoo Ham | 202f4f5 | 2010-08-20 14:43:56 +0900 | [diff] [blame] | 177 | struct max8952_data *max8952; |
| 178 | |
| 179 | int ret = 0, err = 0; |
| 180 | |
| 181 | if (!pdata) { |
| 182 | dev_err(&client->dev, "Require the platform data\n"); |
| 183 | return -EINVAL; |
| 184 | } |
| 185 | |
| 186 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE)) |
| 187 | return -EIO; |
| 188 | |
| 189 | max8952 = kzalloc(sizeof(struct max8952_data), GFP_KERNEL); |
| 190 | if (!max8952) |
| 191 | return -ENOMEM; |
| 192 | |
| 193 | max8952->client = client; |
| 194 | max8952->dev = &client->dev; |
| 195 | max8952->pdata = pdata; |
MyungJoo Ham | 202f4f5 | 2010-08-20 14:43:56 +0900 | [diff] [blame] | 196 | |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 197 | config.dev = max8952->dev; |
| 198 | config.init_data = &pdata->reg_data; |
| 199 | config.driver_data = max8952; |
| 200 | |
| 201 | max8952->rdev = regulator_register(®ulator, &config); |
MyungJoo Ham | 202f4f5 | 2010-08-20 14:43:56 +0900 | [diff] [blame] | 202 | |
Axel Lin | da05738 | 2010-10-25 10:11:07 +0800 | [diff] [blame] | 203 | if (IS_ERR(max8952->rdev)) { |
| 204 | ret = PTR_ERR(max8952->rdev); |
MyungJoo Ham | 202f4f5 | 2010-08-20 14:43:56 +0900 | [diff] [blame] | 205 | dev_err(max8952->dev, "regulator init failed (%d)\n", ret); |
Axel Lin | da05738 | 2010-10-25 10:11:07 +0800 | [diff] [blame] | 206 | goto err_reg; |
| 207 | } |
MyungJoo Ham | 202f4f5 | 2010-08-20 14:43:56 +0900 | [diff] [blame] | 208 | |
| 209 | max8952->en = !!(pdata->reg_data.constraints.boot_on); |
Axel Lin | c8237f0 | 2012-03-15 08:47:48 +0800 | [diff] [blame] | 210 | max8952->vid0 = pdata->default_mode & 0x1; |
| 211 | max8952->vid1 = (pdata->default_mode >> 1) & 0x1; |
MyungJoo Ham | 202f4f5 | 2010-08-20 14:43:56 +0900 | [diff] [blame] | 212 | |
| 213 | if (gpio_is_valid(pdata->gpio_en)) { |
| 214 | if (!gpio_request(pdata->gpio_en, "MAX8952 EN")) |
| 215 | gpio_direction_output(pdata->gpio_en, max8952->en); |
| 216 | else |
| 217 | err = 1; |
| 218 | } else |
| 219 | err = 2; |
| 220 | |
| 221 | if (err) { |
| 222 | dev_info(max8952->dev, "EN gpio invalid: assume that EN" |
| 223 | "is always High\n"); |
| 224 | max8952->en = 1; |
| 225 | pdata->gpio_en = -1; /* Mark invalid */ |
| 226 | } |
| 227 | |
| 228 | err = 0; |
| 229 | |
| 230 | if (gpio_is_valid(pdata->gpio_vid0) && |
| 231 | gpio_is_valid(pdata->gpio_vid1)) { |
| 232 | if (!gpio_request(pdata->gpio_vid0, "MAX8952 VID0")) |
| 233 | gpio_direction_output(pdata->gpio_vid0, |
Axel Lin | c8237f0 | 2012-03-15 08:47:48 +0800 | [diff] [blame] | 234 | (pdata->default_mode) & 0x1); |
MyungJoo Ham | 202f4f5 | 2010-08-20 14:43:56 +0900 | [diff] [blame] | 235 | else |
| 236 | err = 1; |
| 237 | |
| 238 | if (!gpio_request(pdata->gpio_vid1, "MAX8952 VID1")) |
| 239 | gpio_direction_output(pdata->gpio_vid1, |
Axel Lin | c8237f0 | 2012-03-15 08:47:48 +0800 | [diff] [blame] | 240 | (pdata->default_mode >> 1) & 0x1); |
MyungJoo Ham | 202f4f5 | 2010-08-20 14:43:56 +0900 | [diff] [blame] | 241 | else { |
| 242 | if (!err) |
| 243 | gpio_free(pdata->gpio_vid0); |
| 244 | err = 2; |
| 245 | } |
| 246 | |
| 247 | } else |
| 248 | err = 3; |
| 249 | |
| 250 | if (err) { |
| 251 | dev_warn(max8952->dev, "VID0/1 gpio invalid: " |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 252 | "DVS not available.\n"); |
MyungJoo Ham | 202f4f5 | 2010-08-20 14:43:56 +0900 | [diff] [blame] | 253 | max8952->vid0 = 0; |
| 254 | max8952->vid1 = 0; |
| 255 | /* Mark invalid */ |
| 256 | pdata->gpio_vid0 = -1; |
| 257 | pdata->gpio_vid1 = -1; |
| 258 | |
| 259 | /* Disable Pulldown of EN only */ |
| 260 | max8952_write_reg(max8952, MAX8952_REG_CONTROL, 0x60); |
| 261 | |
| 262 | dev_err(max8952->dev, "DVS modes disabled because VID0 and VID1" |
| 263 | " do not have proper controls.\n"); |
| 264 | } else { |
| 265 | /* |
| 266 | * Disable Pulldown on EN, VID0, VID1 to reduce |
| 267 | * leakage current of MAX8952 assuming that MAX8952 |
| 268 | * is turned on (EN==1). Note that without having VID0/1 |
| 269 | * properly connected, turning pulldown off can be |
| 270 | * problematic. Thus, turn this off only when they are |
| 271 | * controllable by GPIO. |
| 272 | */ |
| 273 | max8952_write_reg(max8952, MAX8952_REG_CONTROL, 0x0); |
| 274 | } |
| 275 | |
| 276 | max8952_write_reg(max8952, MAX8952_REG_MODE0, |
| 277 | (max8952_read_reg(max8952, |
| 278 | MAX8952_REG_MODE0) & 0xC0) | |
| 279 | (pdata->dvs_mode[0] & 0x3F)); |
| 280 | max8952_write_reg(max8952, MAX8952_REG_MODE1, |
| 281 | (max8952_read_reg(max8952, |
| 282 | MAX8952_REG_MODE1) & 0xC0) | |
| 283 | (pdata->dvs_mode[1] & 0x3F)); |
| 284 | max8952_write_reg(max8952, MAX8952_REG_MODE2, |
| 285 | (max8952_read_reg(max8952, |
| 286 | MAX8952_REG_MODE2) & 0xC0) | |
| 287 | (pdata->dvs_mode[2] & 0x3F)); |
| 288 | max8952_write_reg(max8952, MAX8952_REG_MODE3, |
| 289 | (max8952_read_reg(max8952, |
| 290 | MAX8952_REG_MODE3) & 0xC0) | |
| 291 | (pdata->dvs_mode[3] & 0x3F)); |
| 292 | |
| 293 | max8952_write_reg(max8952, MAX8952_REG_SYNC, |
| 294 | (max8952_read_reg(max8952, MAX8952_REG_SYNC) & 0x3F) | |
| 295 | ((pdata->sync_freq & 0x3) << 6)); |
| 296 | max8952_write_reg(max8952, MAX8952_REG_RAMP, |
| 297 | (max8952_read_reg(max8952, MAX8952_REG_RAMP) & 0x1F) | |
| 298 | ((pdata->ramp_speed & 0x7) << 5)); |
| 299 | |
| 300 | i2c_set_clientdata(client, max8952); |
| 301 | |
Axel Lin | da05738 | 2010-10-25 10:11:07 +0800 | [diff] [blame] | 302 | return 0; |
| 303 | |
| 304 | err_reg: |
| 305 | kfree(max8952); |
MyungJoo Ham | 202f4f5 | 2010-08-20 14:43:56 +0900 | [diff] [blame] | 306 | return ret; |
| 307 | } |
| 308 | |
| 309 | static int __devexit max8952_pmic_remove(struct i2c_client *client) |
| 310 | { |
| 311 | struct max8952_data *max8952 = i2c_get_clientdata(client); |
| 312 | struct max8952_platform_data *pdata = max8952->pdata; |
| 313 | struct regulator_dev *rdev = max8952->rdev; |
| 314 | |
| 315 | regulator_unregister(rdev); |
| 316 | |
| 317 | gpio_free(pdata->gpio_vid0); |
| 318 | gpio_free(pdata->gpio_vid1); |
| 319 | gpio_free(pdata->gpio_en); |
| 320 | |
| 321 | kfree(max8952); |
| 322 | return 0; |
| 323 | } |
| 324 | |
| 325 | static const struct i2c_device_id max8952_ids[] = { |
| 326 | { "max8952", 0 }, |
| 327 | { }, |
| 328 | }; |
| 329 | MODULE_DEVICE_TABLE(i2c, max8952_ids); |
| 330 | |
| 331 | static struct i2c_driver max8952_pmic_driver = { |
| 332 | .probe = max8952_pmic_probe, |
| 333 | .remove = __devexit_p(max8952_pmic_remove), |
| 334 | .driver = { |
| 335 | .name = "max8952", |
| 336 | }, |
| 337 | .id_table = max8952_ids, |
| 338 | }; |
| 339 | |
| 340 | static int __init max8952_pmic_init(void) |
| 341 | { |
| 342 | return i2c_add_driver(&max8952_pmic_driver); |
| 343 | } |
| 344 | subsys_initcall(max8952_pmic_init); |
| 345 | |
| 346 | static void __exit max8952_pmic_exit(void) |
| 347 | { |
| 348 | i2c_del_driver(&max8952_pmic_driver); |
| 349 | } |
| 350 | module_exit(max8952_pmic_exit); |
| 351 | |
| 352 | MODULE_DESCRIPTION("MAXIM 8952 voltage regulator driver"); |
| 353 | MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>"); |
| 354 | MODULE_LICENSE("GPL"); |