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