From d0fd59fb0aae728e2e830221992acf253d4e147d Mon Sep 17 00:00:00 2001 From: Alistair Francis Date: Sun, 7 Feb 2021 10:43:03 -0800 Subject: [PATCH] rM2: thermal: Copy the rM2 sy7636a_thermal driver Copy the rM2 zero-sugar branch (https://github.com/reMarkable/linux/tree/zero-sugar) to the new kernel. Signed-off-by: Alistair Francis --- drivers/thermal/Kconfig | 7 ++ drivers/thermal/Makefile | 1 + drivers/thermal/sy7636a_thermal.c | 120 ++++++++++++++++++++++++++++++ 3 files changed, 128 insertions(+) create mode 100644 drivers/thermal/sy7636a_thermal.c diff --git a/drivers/thermal/Kconfig b/drivers/thermal/Kconfig index 62f82dcf7729..8a9853fe5f2b 100644 --- a/drivers/thermal/Kconfig +++ b/drivers/thermal/Kconfig @@ -403,6 +403,13 @@ depends on (ARCH_STI || ARCH_STM32) && OF source "drivers/thermal/st/Kconfig" endmenu +config SY7636A_THERMAL + tristate "SY7636A thermal management" + depends on MFD_SY7636A + help + Enable the sy7636a thermal driver, which supports the + temperature sensor embedded in Silabs SY7636A chip. + config TANGO_THERMAL tristate "Tango thermal management" depends on ARCH_TANGO || COMPILE_TEST diff --git a/drivers/thermal/Makefile b/drivers/thermal/Makefile index d532a5834de1..e20c50cc09c7 100644 --- a/drivers/thermal/Makefile +++ b/drivers/thermal/Makefile @@ -50,6 +50,7 @@ obj-$(CONFIG_DA9062_THERMAL) += da9062-thermal.o obj-y += intel/ obj-$(CONFIG_TI_SOC_THERMAL) += ti-soc-thermal/ obj-y += st/ +obj-$(CONFIG_SY7636A_THERMAL) += sy7636a_thermal.o obj-$(CONFIG_QCOM_TSENS) += qcom/ obj-y += tegra/ obj-$(CONFIG_HISI_THERMAL) += hisi_thermal.o diff --git a/drivers/thermal/sy7636a_thermal.c b/drivers/thermal/sy7636a_thermal.c new file mode 100644 index 000000000000..8bdbee26a3a9 --- /dev/null +++ b/drivers/thermal/sy7636a_thermal.c @@ -0,0 +1,120 @@ +/* + * Functions to access SY3686A power management chip temperature + * + * Copyright (C) 2019 reMarkable AS - http://www.remarkable.com/ + * + * Author: Lars Ivar Miljeteig + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation version 2. + * + * This program is distributed "as is" WITHOUT ANY WARRANTY of any + * kind, whether express or implied; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#include +#include +#include + +#include + +struct sy7636a_data { + struct sy7636a *sy7636a; + struct thermal_zone_device *thermal_zone_dev; +}; + +static int sy7636a_get_temp(void *arg, int *res) +{ + unsigned int reg_val, mode_ctr; + int ret; + struct sy7636a_data *data = arg; + bool isVoltageActive; + + mutex_lock(&data->sy7636a->reglock); + + ret = regmap_read(data->sy7636a->regmap, + SY7636A_REG_OPERATION_MODE_CRL, &mode_ctr); + if (ret) + goto done; + + isVoltageActive = mode_ctr & SY7636A_OPERATION_MODE_CRL_ONOFF; + + if (!isVoltageActive) { + ret = regmap_write(data->sy7636a->regmap, + SY7636A_REG_OPERATION_MODE_CRL, + mode_ctr | SY7636A_OPERATION_MODE_CRL_ONOFF); + if (ret) + goto done; + usleep_range(1000, 1500); + } + + ret = regmap_read(data->sy7636a->regmap, + SY7636A_REG_TERMISTOR_READOUT, ®_val); + if (ret) + goto done; + + if (!isVoltageActive) { + ret = regmap_write(data->sy7636a->regmap, + SY7636A_REG_OPERATION_MODE_CRL, + mode_ctr); + if (ret) + goto done; + } + + *res = *((signed char*)®_val); + *res *= 1000; + + done: + mutex_unlock(&data->sy7636a->reglock); + + return ret; +} + +static const struct thermal_zone_of_device_ops ops = { + .get_temp = sy7636a_get_temp, +}; + +static int sy7636a_thermal_probe(struct platform_device *pdev) +{ + struct sy7636a *sy7636a = dev_get_drvdata(pdev->dev.parent); + struct sy7636a_data *data; + + if (!sy7636a) + return -EPROBE_DEFER; + + data = devm_kzalloc(&pdev->dev, sizeof(struct sy7636a_data), GFP_KERNEL); + if (!data) + return -ENOMEM; + + platform_set_drvdata(pdev, data); + + data->sy7636a = sy7636a; + data->thermal_zone_dev = devm_thermal_zone_of_sensor_register( + pdev->dev.parent, + 0, + data, + &ops); + + return PTR_ERR_OR_ZERO(data->thermal_zone_dev); +} + +static const struct platform_device_id sy7636a_thermal_id_table[] = { + { "sy7636a-thermal", }, +}; +MODULE_DEVICE_TABLE(platform, sy7636a_thermal_id_table); + +static struct platform_driver sy7636a_thermal_driver = { + .driver = { + .name = "sy7636a-thermal", + }, + .probe = sy7636a_thermal_probe, + .id_table = sy7636a_thermal_id_table, +}; +module_platform_driver(sy7636a_thermal_driver); + +MODULE_AUTHOR("Lars Ivar Miljeteig "); +MODULE_DESCRIPTION("SY7636A thermal driver"); +MODULE_LICENSE("GPL v2");