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> |
Tomasz Figa | 3ff4aa9 | 2013-04-04 18:17:20 +0200 | [diff] [blame] | 31 | #include <linux/of.h> |
| 32 | #include <linux/of_gpio.h> |
| 33 | #include <linux/regulator/of_regulator.h> |
MyungJoo Ham | 202f4f5 | 2010-08-20 14:43:56 +0900 | [diff] [blame] | 34 | #include <linux/slab.h> |
| 35 | |
| 36 | /* Registers */ |
| 37 | enum { |
| 38 | MAX8952_REG_MODE0, |
| 39 | MAX8952_REG_MODE1, |
| 40 | MAX8952_REG_MODE2, |
| 41 | MAX8952_REG_MODE3, |
| 42 | MAX8952_REG_CONTROL, |
| 43 | MAX8952_REG_SYNC, |
| 44 | MAX8952_REG_RAMP, |
| 45 | MAX8952_REG_CHIP_ID1, |
| 46 | MAX8952_REG_CHIP_ID2, |
| 47 | }; |
| 48 | |
| 49 | struct max8952_data { |
| 50 | struct i2c_client *client; |
| 51 | struct device *dev; |
MyungJoo Ham | 202f4f5 | 2010-08-20 14:43:56 +0900 | [diff] [blame] | 52 | struct max8952_platform_data *pdata; |
| 53 | struct regulator_dev *rdev; |
| 54 | |
| 55 | bool vid0; |
| 56 | bool vid1; |
MyungJoo Ham | 202f4f5 | 2010-08-20 14:43:56 +0900 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | static int max8952_read_reg(struct max8952_data *max8952, u8 reg) |
| 60 | { |
| 61 | int ret = i2c_smbus_read_byte_data(max8952->client, reg); |
| 62 | if (ret > 0) |
| 63 | ret &= 0xff; |
| 64 | |
| 65 | return ret; |
| 66 | } |
| 67 | |
| 68 | static int max8952_write_reg(struct max8952_data *max8952, |
| 69 | u8 reg, u8 value) |
| 70 | { |
| 71 | return i2c_smbus_write_byte_data(max8952->client, reg, value); |
| 72 | } |
| 73 | |
MyungJoo Ham | 202f4f5 | 2010-08-20 14:43:56 +0900 | [diff] [blame] | 74 | static int max8952_list_voltage(struct regulator_dev *rdev, |
| 75 | unsigned int selector) |
| 76 | { |
| 77 | struct max8952_data *max8952 = rdev_get_drvdata(rdev); |
| 78 | |
| 79 | if (rdev_get_id(rdev) != 0) |
| 80 | return -EINVAL; |
| 81 | |
Axel Lin | b9b49af | 2012-04-20 18:36:14 +0800 | [diff] [blame] | 82 | return (max8952->pdata->dvs_mode[selector] * 10 + 770) * 1000; |
MyungJoo Ham | 202f4f5 | 2010-08-20 14:43:56 +0900 | [diff] [blame] | 83 | } |
| 84 | |
Axel Lin | b9b49af | 2012-04-20 18:36:14 +0800 | [diff] [blame] | 85 | static int max8952_get_voltage_sel(struct regulator_dev *rdev) |
MyungJoo Ham | 202f4f5 | 2010-08-20 14:43:56 +0900 | [diff] [blame] | 86 | { |
| 87 | struct max8952_data *max8952 = rdev_get_drvdata(rdev); |
| 88 | u8 vid = 0; |
| 89 | |
| 90 | if (max8952->vid0) |
| 91 | vid += 1; |
| 92 | if (max8952->vid1) |
| 93 | vid += 2; |
| 94 | |
Axel Lin | b9b49af | 2012-04-20 18:36:14 +0800 | [diff] [blame] | 95 | return vid; |
MyungJoo Ham | 202f4f5 | 2010-08-20 14:43:56 +0900 | [diff] [blame] | 96 | } |
| 97 | |
Axel Lin | 6ea67d0 | 2012-03-26 09:20:58 +0800 | [diff] [blame] | 98 | static int max8952_set_voltage_sel(struct regulator_dev *rdev, |
| 99 | unsigned selector) |
MyungJoo Ham | 202f4f5 | 2010-08-20 14:43:56 +0900 | [diff] [blame] | 100 | { |
| 101 | struct max8952_data *max8952 = rdev_get_drvdata(rdev); |
MyungJoo Ham | 202f4f5 | 2010-08-20 14:43:56 +0900 | [diff] [blame] | 102 | |
| 103 | if (!gpio_is_valid(max8952->pdata->gpio_vid0) || |
Jonghwan Choi | e2cf313 | 2011-06-15 17:54:12 +0900 | [diff] [blame] | 104 | !gpio_is_valid(max8952->pdata->gpio_vid1)) { |
MyungJoo Ham | 202f4f5 | 2010-08-20 14:43:56 +0900 | [diff] [blame] | 105 | /* DVS not supported */ |
| 106 | return -EPERM; |
| 107 | } |
| 108 | |
Axel Lin | 6ea67d0 | 2012-03-26 09:20:58 +0800 | [diff] [blame] | 109 | max8952->vid0 = selector & 0x1; |
| 110 | max8952->vid1 = (selector >> 1) & 0x1; |
| 111 | gpio_set_value(max8952->pdata->gpio_vid0, max8952->vid0); |
| 112 | gpio_set_value(max8952->pdata->gpio_vid1, max8952->vid1); |
MyungJoo Ham | 202f4f5 | 2010-08-20 14:43:56 +0900 | [diff] [blame] | 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | static struct regulator_ops max8952_ops = { |
| 118 | .list_voltage = max8952_list_voltage, |
Axel Lin | b9b49af | 2012-04-20 18:36:14 +0800 | [diff] [blame] | 119 | .get_voltage_sel = max8952_get_voltage_sel, |
Axel Lin | 6ea67d0 | 2012-03-26 09:20:58 +0800 | [diff] [blame] | 120 | .set_voltage_sel = max8952_set_voltage_sel, |
MyungJoo Ham | 202f4f5 | 2010-08-20 14:43:56 +0900 | [diff] [blame] | 121 | }; |
| 122 | |
Axel Lin | bd6ff0d | 2012-04-06 08:29:49 +0800 | [diff] [blame] | 123 | static const struct regulator_desc regulator = { |
MyungJoo Ham | 202f4f5 | 2010-08-20 14:43:56 +0900 | [diff] [blame] | 124 | .name = "MAX8952_VOUT", |
| 125 | .id = 0, |
| 126 | .n_voltages = MAX8952_NUM_DVS_MODE, |
| 127 | .ops = &max8952_ops, |
| 128 | .type = REGULATOR_VOLTAGE, |
| 129 | .owner = THIS_MODULE, |
| 130 | }; |
| 131 | |
Tomasz Figa | 3ff4aa9 | 2013-04-04 18:17:20 +0200 | [diff] [blame] | 132 | #ifdef CONFIG_OF |
| 133 | static struct of_device_id max8952_dt_match[] = { |
| 134 | { .compatible = "maxim,max8952" }, |
| 135 | {}, |
| 136 | }; |
| 137 | MODULE_DEVICE_TABLE(of, max8952_dt_match); |
| 138 | |
| 139 | static struct max8952_platform_data *max8952_parse_dt(struct device *dev) |
| 140 | { |
| 141 | struct max8952_platform_data *pd; |
| 142 | struct device_node *np = dev->of_node; |
| 143 | int ret; |
| 144 | int i; |
| 145 | |
| 146 | pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL); |
| 147 | if (!pd) { |
| 148 | dev_err(dev, "Failed to allocate platform data\n"); |
| 149 | return NULL; |
| 150 | } |
| 151 | |
| 152 | pd->gpio_vid0 = of_get_named_gpio(np, "max8952,vid-gpios", 0); |
| 153 | pd->gpio_vid1 = of_get_named_gpio(np, "max8952,vid-gpios", 1); |
| 154 | pd->gpio_en = of_get_named_gpio(np, "max8952,en-gpio", 0); |
| 155 | |
| 156 | if (of_property_read_u32(np, "max8952,default-mode", &pd->default_mode)) |
| 157 | dev_warn(dev, "Default mode not specified, assuming 0\n"); |
| 158 | |
| 159 | ret = of_property_read_u32_array(np, "max8952,dvs-mode-microvolt", |
| 160 | pd->dvs_mode, ARRAY_SIZE(pd->dvs_mode)); |
| 161 | if (ret) { |
| 162 | dev_err(dev, "max8952,dvs-mode-microvolt property not specified"); |
| 163 | return NULL; |
| 164 | } |
| 165 | |
| 166 | for (i = 0; i < ARRAY_SIZE(pd->dvs_mode); ++i) { |
| 167 | if (pd->dvs_mode[i] < 770000 || pd->dvs_mode[i] > 1400000) { |
| 168 | dev_err(dev, "DVS voltage %d out of range\n", i); |
| 169 | return NULL; |
| 170 | } |
| 171 | pd->dvs_mode[i] = (pd->dvs_mode[i] - 770000) / 10000; |
| 172 | } |
| 173 | |
| 174 | if (of_property_read_u32(np, "max8952,sync-freq", &pd->sync_freq)) |
| 175 | dev_warn(dev, "max8952,sync-freq property not specified, defaulting to 26MHz\n"); |
| 176 | |
| 177 | if (of_property_read_u32(np, "max8952,ramp-speed", &pd->ramp_speed)) |
| 178 | dev_warn(dev, "max8952,ramp-speed property not specified, defaulting to 32mV/us\n"); |
| 179 | |
| 180 | pd->reg_data = of_get_regulator_init_data(dev, np); |
| 181 | if (!pd->reg_data) { |
| 182 | dev_err(dev, "Failed to parse regulator init data\n"); |
| 183 | return NULL; |
| 184 | } |
| 185 | |
| 186 | return pd; |
| 187 | } |
| 188 | #else |
| 189 | static struct max8952_platform_data *max8952_parse_dt(struct device *dev) |
| 190 | { |
| 191 | return NULL; |
| 192 | } |
| 193 | #endif |
| 194 | |
Bill Pemberton | a502357 | 2012-11-19 13:22:22 -0500 | [diff] [blame] | 195 | static int max8952_pmic_probe(struct i2c_client *client, |
MyungJoo Ham | 202f4f5 | 2010-08-20 14:43:56 +0900 | [diff] [blame] | 196 | const struct i2c_device_id *i2c_id) |
| 197 | { |
| 198 | struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent); |
Jingoo Han | dff91d0 | 2013-07-30 17:20:47 +0900 | [diff] [blame] | 199 | struct max8952_platform_data *pdata = dev_get_platdata(&client->dev); |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 200 | struct regulator_config config = { }; |
MyungJoo Ham | 202f4f5 | 2010-08-20 14:43:56 +0900 | [diff] [blame] | 201 | struct max8952_data *max8952; |
| 202 | |
| 203 | int ret = 0, err = 0; |
| 204 | |
Tomasz Figa | 3ff4aa9 | 2013-04-04 18:17:20 +0200 | [diff] [blame] | 205 | if (client->dev.of_node) |
| 206 | pdata = max8952_parse_dt(&client->dev); |
| 207 | |
MyungJoo Ham | 202f4f5 | 2010-08-20 14:43:56 +0900 | [diff] [blame] | 208 | if (!pdata) { |
| 209 | dev_err(&client->dev, "Require the platform data\n"); |
| 210 | return -EINVAL; |
| 211 | } |
| 212 | |
| 213 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE)) |
| 214 | return -EIO; |
| 215 | |
Axel Lin | 372de4a | 2012-05-16 12:50:47 +0800 | [diff] [blame] | 216 | max8952 = devm_kzalloc(&client->dev, sizeof(struct max8952_data), |
| 217 | GFP_KERNEL); |
MyungJoo Ham | 202f4f5 | 2010-08-20 14:43:56 +0900 | [diff] [blame] | 218 | if (!max8952) |
| 219 | return -ENOMEM; |
| 220 | |
| 221 | max8952->client = client; |
| 222 | max8952->dev = &client->dev; |
| 223 | max8952->pdata = pdata; |
MyungJoo Ham | 202f4f5 | 2010-08-20 14:43:56 +0900 | [diff] [blame] | 224 | |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 225 | config.dev = max8952->dev; |
Tomasz Figa | 3ec6eb9 | 2013-04-04 18:17:19 +0200 | [diff] [blame] | 226 | config.init_data = pdata->reg_data; |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 227 | config.driver_data = max8952; |
Axel Lin | 71622e1 | 2013-04-25 22:15:21 +0800 | [diff] [blame] | 228 | config.of_node = client->dev.of_node; |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 229 | |
Axel Lin | b669e0a | 2012-07-10 06:10:12 +0800 | [diff] [blame] | 230 | config.ena_gpio = pdata->gpio_en; |
Tomasz Figa | 3ec6eb9 | 2013-04-04 18:17:19 +0200 | [diff] [blame] | 231 | if (pdata->reg_data->constraints.boot_on) |
Axel Lin | b669e0a | 2012-07-10 06:10:12 +0800 | [diff] [blame] | 232 | config.ena_gpio_flags |= GPIOF_OUT_INIT_HIGH; |
| 233 | |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 234 | max8952->rdev = regulator_register(®ulator, &config); |
MyungJoo Ham | 202f4f5 | 2010-08-20 14:43:56 +0900 | [diff] [blame] | 235 | |
Axel Lin | da05738 | 2010-10-25 10:11:07 +0800 | [diff] [blame] | 236 | if (IS_ERR(max8952->rdev)) { |
| 237 | ret = PTR_ERR(max8952->rdev); |
MyungJoo Ham | 202f4f5 | 2010-08-20 14:43:56 +0900 | [diff] [blame] | 238 | dev_err(max8952->dev, "regulator init failed (%d)\n", ret); |
Axel Lin | 372de4a | 2012-05-16 12:50:47 +0800 | [diff] [blame] | 239 | return ret; |
Axel Lin | da05738 | 2010-10-25 10:11:07 +0800 | [diff] [blame] | 240 | } |
MyungJoo Ham | 202f4f5 | 2010-08-20 14:43:56 +0900 | [diff] [blame] | 241 | |
Axel Lin | c8237f0 | 2012-03-15 08:47:48 +0800 | [diff] [blame] | 242 | max8952->vid0 = pdata->default_mode & 0x1; |
| 243 | max8952->vid1 = (pdata->default_mode >> 1) & 0x1; |
MyungJoo Ham | 202f4f5 | 2010-08-20 14:43:56 +0900 | [diff] [blame] | 244 | |
MyungJoo Ham | 202f4f5 | 2010-08-20 14:43:56 +0900 | [diff] [blame] | 245 | if (gpio_is_valid(pdata->gpio_vid0) && |
| 246 | gpio_is_valid(pdata->gpio_vid1)) { |
| 247 | if (!gpio_request(pdata->gpio_vid0, "MAX8952 VID0")) |
| 248 | gpio_direction_output(pdata->gpio_vid0, |
Axel Lin | c8237f0 | 2012-03-15 08:47:48 +0800 | [diff] [blame] | 249 | (pdata->default_mode) & 0x1); |
MyungJoo Ham | 202f4f5 | 2010-08-20 14:43:56 +0900 | [diff] [blame] | 250 | else |
| 251 | err = 1; |
| 252 | |
| 253 | if (!gpio_request(pdata->gpio_vid1, "MAX8952 VID1")) |
| 254 | gpio_direction_output(pdata->gpio_vid1, |
Axel Lin | c8237f0 | 2012-03-15 08:47:48 +0800 | [diff] [blame] | 255 | (pdata->default_mode >> 1) & 0x1); |
MyungJoo Ham | 202f4f5 | 2010-08-20 14:43:56 +0900 | [diff] [blame] | 256 | else { |
| 257 | if (!err) |
| 258 | gpio_free(pdata->gpio_vid0); |
| 259 | err = 2; |
| 260 | } |
| 261 | |
| 262 | } else |
| 263 | err = 3; |
| 264 | |
| 265 | if (err) { |
| 266 | dev_warn(max8952->dev, "VID0/1 gpio invalid: " |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 267 | "DVS not available.\n"); |
MyungJoo Ham | 202f4f5 | 2010-08-20 14:43:56 +0900 | [diff] [blame] | 268 | max8952->vid0 = 0; |
| 269 | max8952->vid1 = 0; |
| 270 | /* Mark invalid */ |
| 271 | pdata->gpio_vid0 = -1; |
| 272 | pdata->gpio_vid1 = -1; |
| 273 | |
| 274 | /* Disable Pulldown of EN only */ |
| 275 | max8952_write_reg(max8952, MAX8952_REG_CONTROL, 0x60); |
| 276 | |
| 277 | dev_err(max8952->dev, "DVS modes disabled because VID0 and VID1" |
| 278 | " do not have proper controls.\n"); |
| 279 | } else { |
| 280 | /* |
| 281 | * Disable Pulldown on EN, VID0, VID1 to reduce |
| 282 | * leakage current of MAX8952 assuming that MAX8952 |
| 283 | * is turned on (EN==1). Note that without having VID0/1 |
| 284 | * properly connected, turning pulldown off can be |
| 285 | * problematic. Thus, turn this off only when they are |
| 286 | * controllable by GPIO. |
| 287 | */ |
| 288 | max8952_write_reg(max8952, MAX8952_REG_CONTROL, 0x0); |
| 289 | } |
| 290 | |
| 291 | max8952_write_reg(max8952, MAX8952_REG_MODE0, |
| 292 | (max8952_read_reg(max8952, |
| 293 | MAX8952_REG_MODE0) & 0xC0) | |
| 294 | (pdata->dvs_mode[0] & 0x3F)); |
| 295 | max8952_write_reg(max8952, MAX8952_REG_MODE1, |
| 296 | (max8952_read_reg(max8952, |
| 297 | MAX8952_REG_MODE1) & 0xC0) | |
| 298 | (pdata->dvs_mode[1] & 0x3F)); |
| 299 | max8952_write_reg(max8952, MAX8952_REG_MODE2, |
| 300 | (max8952_read_reg(max8952, |
| 301 | MAX8952_REG_MODE2) & 0xC0) | |
| 302 | (pdata->dvs_mode[2] & 0x3F)); |
| 303 | max8952_write_reg(max8952, MAX8952_REG_MODE3, |
| 304 | (max8952_read_reg(max8952, |
| 305 | MAX8952_REG_MODE3) & 0xC0) | |
| 306 | (pdata->dvs_mode[3] & 0x3F)); |
| 307 | |
| 308 | max8952_write_reg(max8952, MAX8952_REG_SYNC, |
| 309 | (max8952_read_reg(max8952, MAX8952_REG_SYNC) & 0x3F) | |
| 310 | ((pdata->sync_freq & 0x3) << 6)); |
| 311 | max8952_write_reg(max8952, MAX8952_REG_RAMP, |
| 312 | (max8952_read_reg(max8952, MAX8952_REG_RAMP) & 0x1F) | |
| 313 | ((pdata->ramp_speed & 0x7) << 5)); |
| 314 | |
| 315 | i2c_set_clientdata(client, max8952); |
| 316 | |
Axel Lin | da05738 | 2010-10-25 10:11:07 +0800 | [diff] [blame] | 317 | return 0; |
MyungJoo Ham | 202f4f5 | 2010-08-20 14:43:56 +0900 | [diff] [blame] | 318 | } |
| 319 | |
Bill Pemberton | 8dc995f | 2012-11-19 13:26:10 -0500 | [diff] [blame] | 320 | static int max8952_pmic_remove(struct i2c_client *client) |
MyungJoo Ham | 202f4f5 | 2010-08-20 14:43:56 +0900 | [diff] [blame] | 321 | { |
| 322 | struct max8952_data *max8952 = i2c_get_clientdata(client); |
| 323 | struct max8952_platform_data *pdata = max8952->pdata; |
| 324 | struct regulator_dev *rdev = max8952->rdev; |
| 325 | |
| 326 | regulator_unregister(rdev); |
| 327 | |
| 328 | gpio_free(pdata->gpio_vid0); |
| 329 | gpio_free(pdata->gpio_vid1); |
MyungJoo Ham | 202f4f5 | 2010-08-20 14:43:56 +0900 | [diff] [blame] | 330 | return 0; |
| 331 | } |
| 332 | |
| 333 | static const struct i2c_device_id max8952_ids[] = { |
| 334 | { "max8952", 0 }, |
| 335 | { }, |
| 336 | }; |
| 337 | MODULE_DEVICE_TABLE(i2c, max8952_ids); |
| 338 | |
| 339 | static struct i2c_driver max8952_pmic_driver = { |
| 340 | .probe = max8952_pmic_probe, |
Bill Pemberton | 5eb9f2b | 2012-11-19 13:20:42 -0500 | [diff] [blame] | 341 | .remove = max8952_pmic_remove, |
MyungJoo Ham | 202f4f5 | 2010-08-20 14:43:56 +0900 | [diff] [blame] | 342 | .driver = { |
| 343 | .name = "max8952", |
Tomasz Figa | 3ff4aa9 | 2013-04-04 18:17:20 +0200 | [diff] [blame] | 344 | .of_match_table = of_match_ptr(max8952_dt_match), |
MyungJoo Ham | 202f4f5 | 2010-08-20 14:43:56 +0900 | [diff] [blame] | 345 | }, |
| 346 | .id_table = max8952_ids, |
| 347 | }; |
| 348 | |
| 349 | static int __init max8952_pmic_init(void) |
| 350 | { |
| 351 | return i2c_add_driver(&max8952_pmic_driver); |
| 352 | } |
| 353 | subsys_initcall(max8952_pmic_init); |
| 354 | |
| 355 | static void __exit max8952_pmic_exit(void) |
| 356 | { |
| 357 | i2c_del_driver(&max8952_pmic_driver); |
| 358 | } |
| 359 | module_exit(max8952_pmic_exit); |
| 360 | |
| 361 | MODULE_DESCRIPTION("MAXIM 8952 voltage regulator driver"); |
| 362 | MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>"); |
| 363 | MODULE_LICENSE("GPL"); |