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