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