1 // SPDX-License-Identifier: GPL-2.0+ 2 // 3 // Functions to access SY3686A power management chip voltages 4 // 5 // Copyright (C) 2019 reMarkable AS - http://www.remarkable.com/ 6 // 7 // Authors: Lars Ivar Miljeteig <lars.ivar.miljeteig@remarkable.com> 8 // Alistair Francis <alistair@alistair23.me> 9 10 #include <linux/gpio/consumer.h> 11 #include <linux/i2c.h> 12 #include <linux/mfd/sy7636a.h> 13 #include <linux/module.h> 14 #include <linux/platform_device.h> 15 #include <linux/regulator/driver.h> 16 #include <linux/regulator/machine.h> 17 #include <linux/regmap.h> 18 19 struct sy7636a_data { 20 struct regmap *regmap; 21 struct gpio_desc *pgood_gpio; 22 }; 23 24 static int sy7636a_get_vcom_voltage_op(struct regulator_dev *rdev) 25 { 26 int ret; 27 unsigned int val, val_h; 28 29 ret = regmap_read(rdev->regmap, SY7636A_REG_VCOM_ADJUST_CTRL_L, &val); 30 if (ret) 31 return ret; 32 33 ret = regmap_read(rdev->regmap, SY7636A_REG_VCOM_ADJUST_CTRL_H, &val_h); 34 if (ret) 35 return ret; 36 37 val |= (val_h << VCOM_ADJUST_CTRL_SHIFT); 38 39 return (val & VCOM_ADJUST_CTRL_MASK) * VCOM_ADJUST_CTRL_SCAL; 40 } 41 42 static int sy7636a_get_status(struct regulator_dev *rdev) 43 { 44 struct sy7636a_data *data = dev_get_drvdata(rdev->dev.parent); 45 int ret = 0; 46 47 ret = gpiod_get_value_cansleep(data->pgood_gpio); 48 if (ret < 0) 49 dev_err(&rdev->dev, "Failed to read pgood gpio: %d\n", ret); 50 51 return ret; 52 } 53 54 static const struct regulator_ops sy7636a_vcom_volt_ops = { 55 .get_voltage = sy7636a_get_vcom_voltage_op, 56 .enable = regulator_enable_regmap, 57 .disable = regulator_disable_regmap, 58 .is_enabled = regulator_is_enabled_regmap, 59 .get_status = sy7636a_get_status, 60 }; 61 62 static const struct regulator_desc desc = { 63 .name = "vcom", 64 .id = 0, 65 .ops = &sy7636a_vcom_volt_ops, 66 .type = REGULATOR_VOLTAGE, 67 .owner = THIS_MODULE, 68 .enable_reg = SY7636A_REG_OPERATION_MODE_CRL, 69 .enable_mask = SY7636A_OPERATION_MODE_CRL_ONOFF, 70 .regulators_node = of_match_ptr("regulators"), 71 .of_match = of_match_ptr("vcom"), 72 }; 73 74 static int sy7636a_regulator_probe(struct platform_device *pdev) 75 { 76 struct regmap *regmap = dev_get_regmap(pdev->dev.parent, NULL); 77 struct regulator_config config = { }; 78 struct regulator_dev *rdev; 79 struct gpio_desc *gdp; 80 struct sy7636a_data *data; 81 int ret; 82 83 if (!regmap) 84 return -EPROBE_DEFER; 85 86 gdp = devm_gpiod_get(pdev->dev.parent, "epd-pwr-good", GPIOD_IN); 87 if (IS_ERR(gdp)) { 88 dev_err(pdev->dev.parent, "Power good GPIO fault %ld\n", PTR_ERR(gdp)); 89 return PTR_ERR(gdp); 90 } 91 92 data = devm_kzalloc(&pdev->dev, sizeof(struct sy7636a_data), GFP_KERNEL); 93 if (!data) 94 return -ENOMEM; 95 96 data->regmap = regmap; 97 data->pgood_gpio = gdp; 98 99 platform_set_drvdata(pdev, data); 100 101 ret = regmap_write(regmap, SY7636A_REG_POWER_ON_DELAY_TIME, 0x0); 102 if (ret) { 103 dev_err(pdev->dev.parent, "Failed to initialize regulator: %d\n", ret); 104 return ret; 105 } 106 107 config.dev = &pdev->dev; 108 config.dev->of_node = pdev->dev.parent->of_node; 109 config.regmap = regmap; 110 111 rdev = devm_regulator_register(&pdev->dev, &desc, &config); 112 if (IS_ERR(rdev)) { 113 dev_err(pdev->dev.parent, "Failed to register %s regulator\n", 114 pdev->name); 115 return PTR_ERR(rdev); 116 } 117 118 return 0; 119 } 120 121 static const struct platform_device_id sy7636a_regulator_id_table[] = { 122 { "sy7636a-regulator", }, 123 { } 124 }; 125 MODULE_DEVICE_TABLE(platform, sy7636a_regulator_id_table); 126 127 static struct platform_driver sy7636a_regulator_driver = { 128 .driver = { 129 .name = "sy7636a-regulator", 130 }, 131 .probe = sy7636a_regulator_probe, 132 .id_table = sy7636a_regulator_id_table, 133 }; 134 module_platform_driver(sy7636a_regulator_driver); 135 136 MODULE_AUTHOR("Lars Ivar Miljeteig <lars.ivar.miljeteig@remarkable.com>"); 137 MODULE_DESCRIPTION("SY7636A voltage regulator driver"); 138 MODULE_LICENSE("GPL v2"); 139