Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* Author: Dan Scally <djrscally@gmail.com> */
0003 
0004 #include <linux/acpi.h>
0005 #include <linux/clkdev.h>
0006 #include <linux/clk-provider.h>
0007 #include <linux/device.h>
0008 #include <linux/gpio/consumer.h>
0009 #include <linux/regulator/driver.h>
0010 #include <linux/slab.h>
0011 
0012 #include "common.h"
0013 
0014 /*
0015  * The regulators have to have .ops to be valid, but the only ops we actually
0016  * support are .enable and .disable which are handled via .ena_gpiod. Pass an
0017  * empty struct to clear the check without lying about capabilities.
0018  */
0019 static const struct regulator_ops int3472_gpio_regulator_ops;
0020 
0021 static int skl_int3472_clk_prepare(struct clk_hw *hw)
0022 {
0023     struct int3472_gpio_clock *clk = to_int3472_clk(hw);
0024 
0025     gpiod_set_value_cansleep(clk->ena_gpio, 1);
0026     gpiod_set_value_cansleep(clk->led_gpio, 1);
0027 
0028     return 0;
0029 }
0030 
0031 static void skl_int3472_clk_unprepare(struct clk_hw *hw)
0032 {
0033     struct int3472_gpio_clock *clk = to_int3472_clk(hw);
0034 
0035     gpiod_set_value_cansleep(clk->ena_gpio, 0);
0036     gpiod_set_value_cansleep(clk->led_gpio, 0);
0037 }
0038 
0039 static int skl_int3472_clk_enable(struct clk_hw *hw)
0040 {
0041     /*
0042      * We're just turning a GPIO on to enable the clock, which operation
0043      * has the potential to sleep. Given .enable() cannot sleep, but
0044      * .prepare() can, we toggle the GPIO in .prepare() instead. Thus,
0045      * nothing to do here.
0046      */
0047     return 0;
0048 }
0049 
0050 static void skl_int3472_clk_disable(struct clk_hw *hw)
0051 {
0052     /* Likewise, nothing to do here... */
0053 }
0054 
0055 static unsigned int skl_int3472_get_clk_frequency(struct int3472_discrete_device *int3472)
0056 {
0057     union acpi_object *obj;
0058     unsigned int freq;
0059 
0060     obj = skl_int3472_get_acpi_buffer(int3472->sensor, "SSDB");
0061     if (IS_ERR(obj))
0062         return 0; /* report rate as 0 on error */
0063 
0064     if (obj->buffer.length < CIO2_SENSOR_SSDB_MCLKSPEED_OFFSET + sizeof(u32)) {
0065         dev_err(int3472->dev, "The buffer is too small\n");
0066         kfree(obj);
0067         return 0;
0068     }
0069 
0070     freq = *(u32 *)(obj->buffer.pointer + CIO2_SENSOR_SSDB_MCLKSPEED_OFFSET);
0071 
0072     kfree(obj);
0073     return freq;
0074 }
0075 
0076 static unsigned long skl_int3472_clk_recalc_rate(struct clk_hw *hw,
0077                          unsigned long parent_rate)
0078 {
0079     struct int3472_gpio_clock *clk = to_int3472_clk(hw);
0080 
0081     return clk->frequency;
0082 }
0083 
0084 static const struct clk_ops skl_int3472_clock_ops = {
0085     .prepare = skl_int3472_clk_prepare,
0086     .unprepare = skl_int3472_clk_unprepare,
0087     .enable = skl_int3472_clk_enable,
0088     .disable = skl_int3472_clk_disable,
0089     .recalc_rate = skl_int3472_clk_recalc_rate,
0090 };
0091 
0092 int skl_int3472_register_clock(struct int3472_discrete_device *int3472)
0093 {
0094     struct clk_init_data init = {
0095         .ops = &skl_int3472_clock_ops,
0096         .flags = CLK_GET_RATE_NOCACHE,
0097     };
0098     int ret;
0099 
0100     init.name = kasprintf(GFP_KERNEL, "%s-clk",
0101                   acpi_dev_name(int3472->adev));
0102     if (!init.name)
0103         return -ENOMEM;
0104 
0105     int3472->clock.frequency = skl_int3472_get_clk_frequency(int3472);
0106 
0107     int3472->clock.clk_hw.init = &init;
0108     int3472->clock.clk = clk_register(&int3472->adev->dev,
0109                       &int3472->clock.clk_hw);
0110     if (IS_ERR(int3472->clock.clk)) {
0111         ret = PTR_ERR(int3472->clock.clk);
0112         goto out_free_init_name;
0113     }
0114 
0115     int3472->clock.cl = clkdev_create(int3472->clock.clk, NULL,
0116                       int3472->sensor_name);
0117     if (!int3472->clock.cl) {
0118         ret = -ENOMEM;
0119         goto err_unregister_clk;
0120     }
0121 
0122     kfree(init.name);
0123     return 0;
0124 
0125 err_unregister_clk:
0126     clk_unregister(int3472->clock.clk);
0127 out_free_init_name:
0128     kfree(init.name);
0129 
0130     return ret;
0131 }
0132 
0133 void skl_int3472_unregister_clock(struct int3472_discrete_device *int3472)
0134 {
0135     clkdev_drop(int3472->clock.cl);
0136     clk_unregister(int3472->clock.clk);
0137 }
0138 
0139 int skl_int3472_register_regulator(struct int3472_discrete_device *int3472,
0140                    struct acpi_resource_gpio *agpio)
0141 {
0142     const struct int3472_sensor_config *sensor_config;
0143     char *path = agpio->resource_source.string_ptr;
0144     struct regulator_consumer_supply supply_map;
0145     struct regulator_init_data init_data = { };
0146     struct regulator_config cfg = { };
0147     int ret;
0148 
0149     sensor_config = int3472->sensor_config;
0150     if (IS_ERR(sensor_config)) {
0151         dev_err(int3472->dev, "No sensor module config\n");
0152         return PTR_ERR(sensor_config);
0153     }
0154 
0155     if (!sensor_config->supply_map.supply) {
0156         dev_err(int3472->dev, "No supply name defined\n");
0157         return -ENODEV;
0158     }
0159 
0160     init_data.constraints.valid_ops_mask = REGULATOR_CHANGE_STATUS;
0161     init_data.num_consumer_supplies = 1;
0162     supply_map = sensor_config->supply_map;
0163     supply_map.dev_name = int3472->sensor_name;
0164     init_data.consumer_supplies = &supply_map;
0165 
0166     snprintf(int3472->regulator.regulator_name,
0167          sizeof(int3472->regulator.regulator_name), "%s-regulator",
0168          acpi_dev_name(int3472->adev));
0169     snprintf(int3472->regulator.supply_name,
0170          GPIO_REGULATOR_SUPPLY_NAME_LENGTH, "supply-0");
0171 
0172     int3472->regulator.rdesc = INT3472_REGULATOR(
0173                         int3472->regulator.regulator_name,
0174                         int3472->regulator.supply_name,
0175                         &int3472_gpio_regulator_ops);
0176 
0177     int3472->regulator.gpio = acpi_get_and_request_gpiod(path, agpio->pin_table[0],
0178                                  "int3472,regulator");
0179     if (IS_ERR(int3472->regulator.gpio)) {
0180         dev_err(int3472->dev, "Failed to get regulator GPIO line\n");
0181         return PTR_ERR(int3472->regulator.gpio);
0182     }
0183 
0184     cfg.dev = &int3472->adev->dev;
0185     cfg.init_data = &init_data;
0186     cfg.ena_gpiod = int3472->regulator.gpio;
0187 
0188     int3472->regulator.rdev = regulator_register(&int3472->regulator.rdesc,
0189                              &cfg);
0190     if (IS_ERR(int3472->regulator.rdev)) {
0191         ret = PTR_ERR(int3472->regulator.rdev);
0192         goto err_free_gpio;
0193     }
0194 
0195     return 0;
0196 
0197 err_free_gpio:
0198     gpiod_put(int3472->regulator.gpio);
0199 
0200     return ret;
0201 }
0202 
0203 void skl_int3472_unregister_regulator(struct int3472_discrete_device *int3472)
0204 {
0205     regulator_unregister(int3472->regulator.rdev);
0206     gpiod_put(int3472->regulator.gpio);
0207 }