1 // SPDX-License-Identifier: GPL-2.0
2 /* Author: Dan Scally <djrscally@gmail.com> */
3 
4 #include <linux/acpi.h>
5 #include <linux/clkdev.h>
6 #include <linux/clk-provider.h>
7 #include <linux/device.h>
8 #include <linux/gpio/consumer.h>
9 #include <linux/regulator/driver.h>
10 #include <linux/slab.h>
11 
12 #include "common.h"
13 
14 /*
15  * The regulators have to have .ops to be valid, but the only ops we actually
16  * support are .enable and .disable which are handled via .ena_gpiod. Pass an
17  * empty struct to clear the check without lying about capabilities.
18  */
19 static const struct regulator_ops int3472_gpio_regulator_ops;
20 
21 static int skl_int3472_clk_prepare(struct clk_hw *hw)
22 {
23 	struct int3472_gpio_clock *clk = to_int3472_clk(hw);
24 
25 	gpiod_set_value_cansleep(clk->ena_gpio, 1);
26 	return 0;
27 }
28 
29 static void skl_int3472_clk_unprepare(struct clk_hw *hw)
30 {
31 	struct int3472_gpio_clock *clk = to_int3472_clk(hw);
32 
33 	gpiod_set_value_cansleep(clk->ena_gpio, 0);
34 }
35 
36 static int skl_int3472_clk_enable(struct clk_hw *hw)
37 {
38 	/*
39 	 * We're just turning a GPIO on to enable the clock, which operation
40 	 * has the potential to sleep. Given .enable() cannot sleep, but
41 	 * .prepare() can, we toggle the GPIO in .prepare() instead. Thus,
42 	 * nothing to do here.
43 	 */
44 	return 0;
45 }
46 
47 static void skl_int3472_clk_disable(struct clk_hw *hw)
48 {
49 	/* Likewise, nothing to do here... */
50 }
51 
52 static unsigned int skl_int3472_get_clk_frequency(struct int3472_discrete_device *int3472)
53 {
54 	union acpi_object *obj;
55 	unsigned int freq;
56 
57 	obj = skl_int3472_get_acpi_buffer(int3472->sensor, "SSDB");
58 	if (IS_ERR(obj))
59 		return 0; /* report rate as 0 on error */
60 
61 	if (obj->buffer.length < CIO2_SENSOR_SSDB_MCLKSPEED_OFFSET + sizeof(u32)) {
62 		dev_err(int3472->dev, "The buffer is too small\n");
63 		kfree(obj);
64 		return 0;
65 	}
66 
67 	freq = *(u32 *)(obj->buffer.pointer + CIO2_SENSOR_SSDB_MCLKSPEED_OFFSET);
68 
69 	kfree(obj);
70 	return freq;
71 }
72 
73 static unsigned long skl_int3472_clk_recalc_rate(struct clk_hw *hw,
74 						 unsigned long parent_rate)
75 {
76 	struct int3472_gpio_clock *clk = to_int3472_clk(hw);
77 
78 	return clk->frequency;
79 }
80 
81 static const struct clk_ops skl_int3472_clock_ops = {
82 	.prepare = skl_int3472_clk_prepare,
83 	.unprepare = skl_int3472_clk_unprepare,
84 	.enable = skl_int3472_clk_enable,
85 	.disable = skl_int3472_clk_disable,
86 	.recalc_rate = skl_int3472_clk_recalc_rate,
87 };
88 
89 int skl_int3472_register_clock(struct int3472_discrete_device *int3472)
90 {
91 	struct clk_init_data init = {
92 		.ops = &skl_int3472_clock_ops,
93 		.flags = CLK_GET_RATE_NOCACHE,
94 	};
95 	int ret;
96 
97 	init.name = kasprintf(GFP_KERNEL, "%s-clk",
98 			      acpi_dev_name(int3472->adev));
99 	if (!init.name)
100 		return -ENOMEM;
101 
102 	int3472->clock.frequency = skl_int3472_get_clk_frequency(int3472);
103 
104 	int3472->clock.clk_hw.init = &init;
105 	int3472->clock.clk = clk_register(&int3472->adev->dev,
106 					  &int3472->clock.clk_hw);
107 	if (IS_ERR(int3472->clock.clk)) {
108 		ret = PTR_ERR(int3472->clock.clk);
109 		goto out_free_init_name;
110 	}
111 
112 	int3472->clock.cl = clkdev_create(int3472->clock.clk, NULL,
113 					  int3472->sensor_name);
114 	if (!int3472->clock.cl) {
115 		ret = -ENOMEM;
116 		goto err_unregister_clk;
117 	}
118 
119 	kfree(init.name);
120 	return 0;
121 
122 err_unregister_clk:
123 	clk_unregister(int3472->clock.clk);
124 out_free_init_name:
125 	kfree(init.name);
126 
127 	return ret;
128 }
129 
130 void skl_int3472_unregister_clock(struct int3472_discrete_device *int3472)
131 {
132 	clkdev_drop(int3472->clock.cl);
133 	clk_unregister(int3472->clock.clk);
134 }
135 
136 int skl_int3472_register_regulator(struct int3472_discrete_device *int3472,
137 				   struct acpi_resource_gpio *agpio)
138 {
139 	const struct int3472_sensor_config *sensor_config;
140 	char *path = agpio->resource_source.string_ptr;
141 	struct regulator_consumer_supply supply_map;
142 	struct regulator_init_data init_data = { };
143 	struct regulator_config cfg = { };
144 	int ret;
145 
146 	sensor_config = int3472->sensor_config;
147 	if (IS_ERR(sensor_config)) {
148 		dev_err(int3472->dev, "No sensor module config\n");
149 		return PTR_ERR(sensor_config);
150 	}
151 
152 	if (!sensor_config->supply_map.supply) {
153 		dev_err(int3472->dev, "No supply name defined\n");
154 		return -ENODEV;
155 	}
156 
157 	init_data.constraints.valid_ops_mask = REGULATOR_CHANGE_STATUS;
158 	init_data.num_consumer_supplies = 1;
159 	supply_map = sensor_config->supply_map;
160 	supply_map.dev_name = int3472->sensor_name;
161 	init_data.consumer_supplies = &supply_map;
162 
163 	snprintf(int3472->regulator.regulator_name,
164 		 sizeof(int3472->regulator.regulator_name), "%s-regulator",
165 		 acpi_dev_name(int3472->adev));
166 	snprintf(int3472->regulator.supply_name,
167 		 GPIO_REGULATOR_SUPPLY_NAME_LENGTH, "supply-0");
168 
169 	int3472->regulator.rdesc = INT3472_REGULATOR(
170 						int3472->regulator.regulator_name,
171 						int3472->regulator.supply_name,
172 						&int3472_gpio_regulator_ops);
173 
174 	int3472->regulator.gpio = acpi_get_and_request_gpiod(path, agpio->pin_table[0],
175 							     "int3472,regulator");
176 	if (IS_ERR(int3472->regulator.gpio)) {
177 		dev_err(int3472->dev, "Failed to get regulator GPIO line\n");
178 		return PTR_ERR(int3472->regulator.gpio);
179 	}
180 
181 	/* Ensure the pin is in output mode and non-active state */
182 	gpiod_direction_output(int3472->regulator.gpio, 0);
183 
184 	cfg.dev = &int3472->adev->dev;
185 	cfg.init_data = &init_data;
186 	cfg.ena_gpiod = int3472->regulator.gpio;
187 
188 	int3472->regulator.rdev = regulator_register(int3472->dev,
189 						     &int3472->regulator.rdesc,
190 						     &cfg);
191 	if (IS_ERR(int3472->regulator.rdev)) {
192 		ret = PTR_ERR(int3472->regulator.rdev);
193 		goto err_free_gpio;
194 	}
195 
196 	return 0;
197 
198 err_free_gpio:
199 	gpiod_put(int3472->regulator.gpio);
200 
201 	return ret;
202 }
203 
204 void skl_int3472_unregister_regulator(struct int3472_discrete_device *int3472)
205 {
206 	regulator_unregister(int3472->regulator.rdev);
207 	gpiod_put(int3472->regulator.gpio);
208 }
209