1 // SPDX-License-Identifier: GPL-2.0
2 /* Author: Dan Scally <djrscally@gmail.com> */
4 #include <linux/acpi.h>
5 #include <linux/clkdev.h>
6 #include <linux/clk-provider.h>
7 #include <linux/device.h>
9 #include <linux/gpio/consumer.h>
10 #include <linux/regulator/driver.h>
11 #include <linux/slab.h>
16 * 82c0d13a-78c5-4244-9bb1-eb8b539a8d11
17 * This _DSM GUID allows controlling the sensor clk when it is not controlled
20 static const guid_t img_clk_guid =
21 GUID_INIT(0x82c0d13a, 0x78c5, 0x4244,
22 0x9b, 0xb1, 0xeb, 0x8b, 0x53, 0x9a, 0x8d, 0x11);
24 static void skl_int3472_enable_clk(struct int3472_clock *clk, int enable)
26 struct int3472_discrete_device *int3472 = to_int3472_device(clk);
27 union acpi_object args[3];
28 union acpi_object argv4;
31 gpiod_set_value_cansleep(clk->ena_gpio, enable);
35 args[0].integer.type = ACPI_TYPE_INTEGER;
36 args[0].integer.value = clk->imgclk_index;
37 args[1].integer.type = ACPI_TYPE_INTEGER;
38 args[1].integer.value = enable;
39 args[2].integer.type = ACPI_TYPE_INTEGER;
40 args[2].integer.value = 1;
42 argv4.type = ACPI_TYPE_PACKAGE;
43 argv4.package.count = 3;
44 argv4.package.elements = args;
46 acpi_evaluate_dsm(acpi_device_handle(int3472->adev), &img_clk_guid,
51 * The regulators have to have .ops to be valid, but the only ops we actually
52 * support are .enable and .disable which are handled via .ena_gpiod. Pass an
53 * empty struct to clear the check without lying about capabilities.
55 static const struct regulator_ops int3472_gpio_regulator_ops;
57 static int skl_int3472_clk_prepare(struct clk_hw *hw)
59 skl_int3472_enable_clk(to_int3472_clk(hw), 1);
63 static void skl_int3472_clk_unprepare(struct clk_hw *hw)
65 skl_int3472_enable_clk(to_int3472_clk(hw), 0);
68 static int skl_int3472_clk_enable(struct clk_hw *hw)
71 * We're just turning a GPIO on to enable the clock, which operation
72 * has the potential to sleep. Given .enable() cannot sleep, but
73 * .prepare() can, we toggle the GPIO in .prepare() instead. Thus,
79 static void skl_int3472_clk_disable(struct clk_hw *hw)
81 /* Likewise, nothing to do here... */
84 static unsigned int skl_int3472_get_clk_frequency(struct int3472_discrete_device *int3472)
86 union acpi_object *obj;
89 obj = skl_int3472_get_acpi_buffer(int3472->sensor, "SSDB");
91 return 0; /* report rate as 0 on error */
93 if (obj->buffer.length < CIO2_SENSOR_SSDB_MCLKSPEED_OFFSET + sizeof(u32)) {
94 dev_err(int3472->dev, "The buffer is too small\n");
99 freq = *(u32 *)(obj->buffer.pointer + CIO2_SENSOR_SSDB_MCLKSPEED_OFFSET);
105 static unsigned long skl_int3472_clk_recalc_rate(struct clk_hw *hw,
106 unsigned long parent_rate)
108 struct int3472_clock *clk = to_int3472_clk(hw);
110 return clk->frequency;
113 static const struct clk_ops skl_int3472_clock_ops = {
114 .prepare = skl_int3472_clk_prepare,
115 .unprepare = skl_int3472_clk_unprepare,
116 .enable = skl_int3472_clk_enable,
117 .disable = skl_int3472_clk_disable,
118 .recalc_rate = skl_int3472_clk_recalc_rate,
121 int skl_int3472_register_dsm_clock(struct int3472_discrete_device *int3472)
123 struct acpi_device *adev = int3472->adev;
124 struct clk_init_data init = {
125 .ops = &skl_int3472_clock_ops,
126 .flags = CLK_GET_RATE_NOCACHE,
130 if (int3472->clock.cl)
131 return 0; /* A GPIO controlled clk has already been registered */
133 if (!acpi_check_dsm(adev->handle, &img_clk_guid, 0, BIT(1)))
134 return 0; /* DSM clock control is not available */
136 init.name = kasprintf(GFP_KERNEL, "%s-clk", acpi_dev_name(adev));
140 int3472->clock.frequency = skl_int3472_get_clk_frequency(int3472);
141 int3472->clock.clk_hw.init = &init;
142 int3472->clock.clk = clk_register(&adev->dev, &int3472->clock.clk_hw);
143 if (IS_ERR(int3472->clock.clk)) {
144 ret = PTR_ERR(int3472->clock.clk);
145 goto out_free_init_name;
148 int3472->clock.cl = clkdev_create(int3472->clock.clk, NULL, int3472->sensor_name);
149 if (!int3472->clock.cl) {
151 goto err_unregister_clk;
158 clk_unregister(int3472->clock.clk);
164 int skl_int3472_register_gpio_clock(struct int3472_discrete_device *int3472,
165 struct acpi_resource_gpio *agpio, u32 polarity)
167 char *path = agpio->resource_source.string_ptr;
168 struct clk_init_data init = {
169 .ops = &skl_int3472_clock_ops,
170 .flags = CLK_GET_RATE_NOCACHE,
174 if (int3472->clock.cl)
177 int3472->clock.ena_gpio = acpi_get_and_request_gpiod(path, agpio->pin_table[0],
178 "int3472,clk-enable");
179 if (IS_ERR(int3472->clock.ena_gpio)) {
180 ret = PTR_ERR(int3472->clock.ena_gpio);
181 int3472->clock.ena_gpio = NULL;
182 return dev_err_probe(int3472->dev, ret, "getting clk-enable GPIO\n");
185 if (polarity == GPIO_ACTIVE_LOW)
186 gpiod_toggle_active_low(int3472->clock.ena_gpio);
188 /* Ensure the pin is in output mode and non-active state */
189 gpiod_direction_output(int3472->clock.ena_gpio, 0);
191 init.name = kasprintf(GFP_KERNEL, "%s-clk",
192 acpi_dev_name(int3472->adev));
198 int3472->clock.frequency = skl_int3472_get_clk_frequency(int3472);
200 int3472->clock.clk_hw.init = &init;
201 int3472->clock.clk = clk_register(&int3472->adev->dev,
202 &int3472->clock.clk_hw);
203 if (IS_ERR(int3472->clock.clk)) {
204 ret = PTR_ERR(int3472->clock.clk);
205 goto out_free_init_name;
208 int3472->clock.cl = clkdev_create(int3472->clock.clk, NULL,
209 int3472->sensor_name);
210 if (!int3472->clock.cl) {
212 goto err_unregister_clk;
219 clk_unregister(int3472->clock.clk);
223 gpiod_put(int3472->clock.ena_gpio);
228 void skl_int3472_unregister_clock(struct int3472_discrete_device *int3472)
230 if (!int3472->clock.cl)
233 clkdev_drop(int3472->clock.cl);
234 clk_unregister(int3472->clock.clk);
235 gpiod_put(int3472->clock.ena_gpio);
239 * The INT3472 device is going to be the only supplier of a regulator for
240 * the sensor device. But unlike the clk framework the regulator framework
241 * does not allow matching by consumer-device-name only.
243 * Ideally all sensor drivers would use "avdd" as supply-id. But for drivers
244 * where this cannot be changed because another supply-id is already used in
245 * e.g. DeviceTree files an alias for the other supply-id can be added here.
247 * Do not forget to update GPIO_REGULATOR_SUPPLY_MAP_COUNT when changing this.
249 static const char * const skl_int3472_regulator_map_supplies[] = {
254 static_assert(ARRAY_SIZE(skl_int3472_regulator_map_supplies) ==
255 GPIO_REGULATOR_SUPPLY_MAP_COUNT);
258 * On some models there is a single GPIO regulator which is shared between
259 * sensors and only listed in the ACPI resources of one sensor.
260 * This DMI table contains the name of the second sensor. This is used to add
261 * entries for the second sensor to the supply_map.
263 const struct dmi_system_id skl_int3472_regulator_second_sensor[] = {
265 /* Lenovo Miix 510-12IKB */
267 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
268 DMI_MATCH(DMI_PRODUCT_VERSION, "MIIX 510-12IKB"),
270 .driver_data = "i2c-OVTI2680:00",
275 int skl_int3472_register_regulator(struct int3472_discrete_device *int3472,
276 struct acpi_resource_gpio *agpio)
278 char *path = agpio->resource_source.string_ptr;
279 struct regulator_init_data init_data = { };
280 struct regulator_config cfg = { };
281 const char *second_sensor = NULL;
282 const struct dmi_system_id *id;
285 id = dmi_first_match(skl_int3472_regulator_second_sensor);
287 second_sensor = id->driver_data;
289 for (i = 0, j = 0; i < ARRAY_SIZE(skl_int3472_regulator_map_supplies); i++) {
290 int3472->regulator.supply_map[j].supply = skl_int3472_regulator_map_supplies[i];
291 int3472->regulator.supply_map[j].dev_name = int3472->sensor_name;
295 int3472->regulator.supply_map[j].supply =
296 skl_int3472_regulator_map_supplies[i];
297 int3472->regulator.supply_map[j].dev_name = second_sensor;
302 init_data.constraints.valid_ops_mask = REGULATOR_CHANGE_STATUS;
303 init_data.consumer_supplies = int3472->regulator.supply_map;
304 init_data.num_consumer_supplies = j;
306 snprintf(int3472->regulator.regulator_name,
307 sizeof(int3472->regulator.regulator_name), "%s-regulator",
308 acpi_dev_name(int3472->adev));
309 snprintf(int3472->regulator.supply_name,
310 GPIO_REGULATOR_SUPPLY_NAME_LENGTH, "supply-0");
312 int3472->regulator.rdesc = INT3472_REGULATOR(
313 int3472->regulator.regulator_name,
314 int3472->regulator.supply_name,
315 &int3472_gpio_regulator_ops);
317 int3472->regulator.gpio = acpi_get_and_request_gpiod(path, agpio->pin_table[0],
318 "int3472,regulator");
319 if (IS_ERR(int3472->regulator.gpio)) {
320 ret = PTR_ERR(int3472->regulator.gpio);
321 int3472->regulator.gpio = NULL;
322 return dev_err_probe(int3472->dev, ret, "getting regulator GPIO\n");
325 /* Ensure the pin is in output mode and non-active state */
326 gpiod_direction_output(int3472->regulator.gpio, 0);
328 cfg.dev = &int3472->adev->dev;
329 cfg.init_data = &init_data;
330 cfg.ena_gpiod = int3472->regulator.gpio;
332 int3472->regulator.rdev = regulator_register(int3472->dev,
333 &int3472->regulator.rdesc,
335 if (IS_ERR(int3472->regulator.rdev)) {
336 ret = PTR_ERR(int3472->regulator.rdev);
343 gpiod_put(int3472->regulator.gpio);
348 void skl_int3472_unregister_regulator(struct int3472_discrete_device *int3472)
350 regulator_unregister(int3472->regulator.rdev);
351 gpiod_put(int3472->regulator.gpio);