Commit | Line | Data |
---|---|---|
8c485bed AF |
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 | ||
947d0cce AF |
10 | #include <linux/gpio/consumer.h> |
11 | #include <linux/i2c.h> | |
12 | #include <linux/mfd/sy7636a.h> | |
8c485bed AF |
13 | #include <linux/module.h> |
14 | #include <linux/platform_device.h> | |
947d0cce AF |
15 | #include <linux/regulator/driver.h> |
16 | #include <linux/regulator/machine.h> | |
8c485bed | 17 | #include <linux/regmap.h> |
8c485bed | 18 | |
d38d49b1 | 19 | struct sy7636a_data { |
27fdd3bb | 20 | struct regmap *regmap; |
d38d49b1 AF |
21 | struct gpio_desc *pgood_gpio; |
22 | }; | |
23 | ||
8c485bed AF |
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 | { | |
d38d49b1 | 44 | struct sy7636a_data *data = dev_get_drvdata(rdev->dev.parent); |
8c485bed AF |
45 | int ret = 0; |
46 | ||
d38d49b1 | 47 | ret = gpiod_get_value_cansleep(data->pgood_gpio); |
8c485bed AF |
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 | ||
31a89d29 | 62 | static const struct regulator_desc desc = { |
8c485bed AF |
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, | |
8c485bed AF |
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 | { | |
cb17820e | 76 | struct regmap *regmap = dev_get_regmap(pdev->dev.parent, NULL); |
8c485bed AF |
77 | struct regulator_config config = { }; |
78 | struct regulator_dev *rdev; | |
79 | struct gpio_desc *gdp; | |
d38d49b1 | 80 | struct sy7636a_data *data; |
8c485bed AF |
81 | int ret; |
82 | ||
27fdd3bb | 83 | if (!regmap) |
8c485bed AF |
84 | return -EPROBE_DEFER; |
85 | ||
4cafe1ae | 86 | gdp = devm_gpiod_get(pdev->dev.parent, "epd-pwr-good", GPIOD_IN); |
8c485bed | 87 | if (IS_ERR(gdp)) { |
4cafe1ae | 88 | dev_err(pdev->dev.parent, "Power good GPIO fault %ld\n", PTR_ERR(gdp)); |
8c485bed AF |
89 | return PTR_ERR(gdp); |
90 | } | |
91 | ||
d38d49b1 AF |
92 | data = devm_kzalloc(&pdev->dev, sizeof(struct sy7636a_data), GFP_KERNEL); |
93 | if (!data) | |
94 | return -ENOMEM; | |
95 | ||
27fdd3bb | 96 | data->regmap = regmap; |
d38d49b1 AF |
97 | data->pgood_gpio = gdp; |
98 | ||
99 | platform_set_drvdata(pdev, data); | |
8c485bed | 100 | |
27fdd3bb | 101 | ret = regmap_write(regmap, SY7636A_REG_POWER_ON_DELAY_TIME, 0x0); |
8c485bed | 102 | if (ret) { |
4cafe1ae | 103 | dev_err(pdev->dev.parent, "Failed to initialize regulator: %d\n", ret); |
8c485bed AF |
104 | return ret; |
105 | } | |
106 | ||
107 | config.dev = &pdev->dev; | |
4cafe1ae | 108 | config.dev->of_node = pdev->dev.parent->of_node; |
27fdd3bb | 109 | config.regmap = regmap; |
8c485bed AF |
110 | |
111 | rdev = devm_regulator_register(&pdev->dev, &desc, &config); | |
112 | if (IS_ERR(rdev)) { | |
4cafe1ae | 113 | dev_err(pdev->dev.parent, "Failed to register %s regulator\n", |
8c485bed AF |
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", }, | |
686f6b31 | 123 | { } |
8c485bed AF |
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"); |