From mboxrd@z Thu Jan 1 00:00:00 1970 Return-path: Received: from mail-pa0-x230.google.com ([2607:f8b0:400e:c03::230]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1aygSO-00017E-79 for barebox@lists.infradead.org; Fri, 06 May 2016 14:10:18 +0000 Received: by mail-pa0-x230.google.com with SMTP id xk12so48350334pac.0 for ; Fri, 06 May 2016 07:09:55 -0700 (PDT) From: Andrey Smirnov Date: Fri, 6 May 2016 07:09:29 -0700 Message-Id: <1462543770-4619-6-git-send-email-andrew.smirnov@gmail.com> In-Reply-To: <1462543770-4619-1-git-send-email-andrew.smirnov@gmail.com> References: <1462543770-4619-1-git-send-email-andrew.smirnov@gmail.com> List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "barebox" Errors-To: barebox-bounces+u.kleine-koenig=pengutronix.de@lists.infradead.org Subject: [PATCH v2 5/6] aiodev: Add TEMPMON driver To: barebox@lists.infradead.org Cc: Andrey Smirnov Port TEMPMON driver from U-Boot Signed-off-by: Andrey Smirnov --- arch/arm/dts/imx6qdl.dtsi | 14 +++ arch/arm/dts/imx6sx.dtsi | 14 +++ drivers/aiodev/Kconfig | 8 ++ drivers/aiodev/Makefile | 1 + drivers/aiodev/imx_thermal.c | 239 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 276 insertions(+) create mode 100644 drivers/aiodev/imx_thermal.c diff --git a/arch/arm/dts/imx6qdl.dtsi b/arch/arm/dts/imx6qdl.dtsi index 828be9c..0deafbc 100644 --- a/arch/arm/dts/imx6qdl.dtsi +++ b/arch/arm/dts/imx6qdl.dtsi @@ -8,3 +8,17 @@ ipu0 = &ipu1; }; }; + +&ocotp { + #address-cells = <1>; + #size-cells = <1>; + + analog1: ocotp_ana1@34 { + reg = <0x38 0x4>; + }; +}; + +&tempmon { + nvmem-cells = <&analog1>; + nvmem-cell-names = "ocotp-ana1"; +}; diff --git a/arch/arm/dts/imx6sx.dtsi b/arch/arm/dts/imx6sx.dtsi index 5a8ee46..89678b2 100644 --- a/arch/arm/dts/imx6sx.dtsi +++ b/arch/arm/dts/imx6sx.dtsi @@ -10,3 +10,17 @@ pwm7 = &pwm8; }; }; + +&ocotp { + #address-cells = <1>; + #size-cells = <1>; + + analog1: ocotp_ana1@34 { + reg = <0x34 0x4>; + }; +}; + +&tempmon { + nvmem-cells = <&analog1>; + nvmem-cell-names = "ocotp-ana1"; +}; diff --git a/drivers/aiodev/Kconfig b/drivers/aiodev/Kconfig index d6d4ac0..c877bb7 100644 --- a/drivers/aiodev/Kconfig +++ b/drivers/aiodev/Kconfig @@ -5,4 +5,12 @@ menuconfig AIODEV bool "Analog I/O drivers" if AIODEV + +config IMX_THERMAL + tristate "Temperature sensor driver for Freescale i.MX SoCs" + select NVMEM + help + Support for Temperature Monitor (TEMPMON) found on Freescale + i.MX SoCs. + endif diff --git a/drivers/aiodev/Makefile b/drivers/aiodev/Makefile index 806464e..5480a8a 100644 --- a/drivers/aiodev/Makefile +++ b/drivers/aiodev/Makefile @@ -1,2 +1,3 @@ obj-$(CONFIG_AIODEV) += core.o +obj-$(CONFIG_IMX_THERMAL) += imx_thermal.o diff --git a/drivers/aiodev/imx_thermal.c b/drivers/aiodev/imx_thermal.c new file mode 100644 index 0000000..ca4f17b --- /dev/null +++ b/drivers/aiodev/imx_thermal.c @@ -0,0 +1,239 @@ +/* + * imx_thermal + * + * Copyright (c) 2015 Zodiac Inflight Innovation + * Author: Andrey Smirnov + * + * based on the code of analogous driver from U-Boot: + * (C) Copyright 2014-2015 Freescale Semiconductor, Inc. + * Author: Nitin Garg + * Ye Li + * + * See file CREDITS for list of people who contributed to this + * project. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define FACTOR0 10000000 +#define MEASURE_FREQ 327 + +struct imx_thermal_data { + int c1, c2; + void __iomem *base; + struct clk *clk; + + struct aiodevice aiodev; + struct aiochannel aiochan; +}; + +static inline struct imx_thermal_data * +to_imx_thermal_data(struct aiochannel *chan) +{ + return container_of(chan, struct imx_thermal_data, aiochan); +} + + +static int imx_thermal_read(struct aiochannel *chan, int *val) +{ + uint64_t start; + uint32_t tempsense0, tempsense1; + uint16_t n_meas; + struct imx_thermal_data *imx_thermal = to_imx_thermal_data(chan); + + /* + * now we only use single measure, every time we read + * the temperature, we will power on/down anadig thermal + * module + */ + writel(BM_ANADIG_TEMPSENSE0_POWER_DOWN, + imx_thermal->base + HW_ANADIG_TEMPSENSE0_CLR); + writel(BM_ANADIG_ANA_MISC0_REFTOP_SELBIASOFF, + imx_thermal->base + HW_ANADIG_ANA_MISC0_SET); + + /* setup measure freq */ + tempsense1 = readl(imx_thermal->base + HW_ANADIG_TEMPSENSE1); + tempsense1 &= ~BM_ANADIG_TEMPSENSE1_MEASURE_FREQ; + tempsense1 |= MEASURE_FREQ; + writel(tempsense1, imx_thermal->base + HW_ANADIG_TEMPSENSE1); + + /* start the measurement process */ + writel(BM_ANADIG_TEMPSENSE0_MEASURE_TEMP, + imx_thermal->base + HW_ANADIG_TEMPSENSE0_CLR); + writel(BM_ANADIG_TEMPSENSE0_FINISHED, + imx_thermal->base + HW_ANADIG_TEMPSENSE0_CLR); + writel(BM_ANADIG_TEMPSENSE0_MEASURE_TEMP, + imx_thermal->base + HW_ANADIG_TEMPSENSE0_SET); + + /* make sure that the latest temp is valid */ + start = get_time_ns(); + do { + tempsense0 = readl(imx_thermal->base + HW_ANADIG_TEMPSENSE0); + + if (is_timeout(start, 1 * SECOND)) { + dev_err(imx_thermal->aiodev.hwdev, + "Timeout waiting for measurement\n"); + return -EIO; + } + } while (!(tempsense0 & BM_ANADIG_TEMPSENSE0_FINISHED)); + + n_meas = (tempsense0 & BM_ANADIG_TEMPSENSE0_TEMP_VALUE) + >> BP_ANADIG_TEMPSENSE0_TEMP_VALUE; + writel(BM_ANADIG_TEMPSENSE0_FINISHED, + imx_thermal->base + HW_ANADIG_TEMPSENSE0_CLR); + + *val = (int)n_meas * imx_thermal->c1 + imx_thermal->c2; + + /* power down anatop thermal sensor */ + writel(BM_ANADIG_TEMPSENSE0_POWER_DOWN, + imx_thermal->base + HW_ANADIG_TEMPSENSE0_SET); + writel(BM_ANADIG_ANA_MISC0_REFTOP_SELBIASOFF, + imx_thermal->base + HW_ANADIG_ANA_MISC0_CLR); + + return 0; +} + +static int imx_thermal_probe(struct device_d *dev) +{ + uint32_t *ocotp_ana1; + struct device_node *node; + struct imx_thermal_data *imx_thermal; + struct device_d *anatop; + struct nvmem_cell *cell; + struct resource *res; + int t1, n1, t2, n2; + int ret; + size_t len; + + cell = nvmem_cell_get(dev, "ocotp-ana1"); + if (IS_ERR(cell)) { + dev_err(dev, "Failed to get calibration data cell: %s\n", + strerrorp(cell)); + return PTR_ERR(cell); + } + + ocotp_ana1 = nvmem_cell_read(cell, &len); + if (IS_ERR(ocotp_ana1)) { + ret = PTR_ERR(ocotp_ana1); + dev_err(dev, "Failed to read calibration data cell: %s\n", + strerrorp(ocotp_ana1)); + goto put_nvmem_cell; + } + + if (len != sizeof(*ocotp_ana1)) { + ret = -EINVAL; + dev_err(dev, "Unexpected size of calibration data cell\n"); + goto free_calibration_data; + } + + node = of_parse_phandle(dev->device_node, "fsl,tempmon", 0); + if (!node) { + ret = -EINVAL; + dev_err(dev, "Failed to parse fsl,tempmon\n"); + goto free_calibration_data; + } + + anatop = of_find_device_by_node(node); + if (!anatop) { + ret = -EINVAL; + dev_err(dev, "No device corresponds to fsl,tempmon\n"); + goto free_calibration_data; + } + + imx_thermal = xzalloc(sizeof(*imx_thermal)); + + res = dev_request_mem_resource(anatop, 0); + if (IS_ERR(res)) { + ret = PTR_ERR(res); + dev_err(dev, "Failed to request anatop resource: %s\n", + strerrorp(res)); + goto free_imx_thermal; + } + imx_thermal->base = IOMEM(res->start); + + n1 = *ocotp_ana1 >> 20; + t1 = 25; + n2 = (*ocotp_ana1 & 0x000FFF00) >> 8; + t2 = *ocotp_ana1 & 0xFF; + + imx_thermal->c1 = (-1000 * (t2 - t1)) / (n1 - n2); + imx_thermal->c2 = 1000 * t2 + (1000 * n2 * (t2 - t1)) / (n1 - n2); + + imx_thermal->clk = clk_get(dev, NULL); + if (IS_ERR(imx_thermal->clk)) { + ret = PTR_ERR(imx_thermal->clk); + goto free_res; + } + + ret = clk_enable(imx_thermal->clk); + if (ret) { + dev_err(dev, "Failed to enable clock: %s\n", + strerror(ret)); + goto put_clock; + } + + imx_thermal->aiodev.num_channels = 1; + imx_thermal->aiodev.hwdev = dev; + imx_thermal->aiodev.channels = + xmalloc(imx_thermal->aiodev.num_channels * + sizeof(imx_thermal->aiodev.channels[0])); + imx_thermal->aiodev.channels[0] = &imx_thermal->aiochan; + imx_thermal->aiochan.unit = "mC"; + imx_thermal->aiodev.read = imx_thermal_read; + + ret = aiodevice_register(&imx_thermal->aiodev); + if (!ret) + goto free_calibration_data; + + clk_disable(imx_thermal->clk); +put_clock: + clk_put(imx_thermal->clk); +free_res: + release_region(res); +free_imx_thermal: + kfree(imx_thermal); +free_calibration_data: + free(ocotp_ana1); +put_nvmem_cell: + nvmem_cell_put(cell); + return ret; +} + +static const struct of_device_id of_imx_thermal_match[] = { + { .compatible = "fsl,imx6q-tempmon", }, + { .compatible = "fsl,imx6sx-tempmon", }, + { /* end */ } +}; + + +static struct driver_d imx_thermal_driver = { + .name = "imx_thermal", + .probe = imx_thermal_probe, + .of_compatible = DRV_OF_COMPAT(of_imx_thermal_match), +}; + +device_platform_driver(imx_thermal_driver); -- 2.5.5 _______________________________________________ barebox mailing list barebox@lists.infradead.org http://lists.infradead.org/mailman/listinfo/barebox