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