From mboxrd@z Thu Jan 1 00:00:00 1970 Return-path: Received: from mail-pa0-x231.google.com ([2607:f8b0:400e:c03::231]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1a5qca-0003wL-5O for barebox@lists.infradead.org; Mon, 07 Dec 2015 07:54:14 +0000 Received: by pacej9 with SMTP id ej9so121230018pac.2 for ; Sun, 06 Dec 2015 23:53:48 -0800 (PST) From: Andrey Smirnov Date: Sun, 6 Dec 2015 23:52:43 -0800 Message-Id: <1449474763-14099-7-git-send-email-andrew.smirnov@gmail.com> In-Reply-To: <1449474763-14099-1-git-send-email-andrew.smirnov@gmail.com> References: <1449474763-14099-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 7/7] hwmon: Port TEMPMON sensor driver To: barebox@lists.infradead.org Cc: Andrey Smirnov Port TEMPMON sensor driver from U-Boot. Signed-off-by: Andrey Smirnov --- drivers/hwmon/Kconfig | 6 ++ drivers/hwmon/Makefile | 1 + drivers/hwmon/imx_thermal.c | 234 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 241 insertions(+) create mode 100644 drivers/hwmon/imx_thermal.c diff --git a/drivers/hwmon/Kconfig b/drivers/hwmon/Kconfig index db51d26..92e27ea 100644 --- a/drivers/hwmon/Kconfig +++ b/drivers/hwmon/Kconfig @@ -22,4 +22,10 @@ config SENSORS_LM75 - National Semiconductor LM75, LM75A +config SENSORS_IMX_THERMAL + tristate "Temperature sensor driver for Freescale i.MX SoCs" + help + Support for Temperature Monitor (TEMPMON) found on Freescale + i.MX SoCs. + endif # HWMON diff --git a/drivers/hwmon/Makefile b/drivers/hwmon/Makefile index e73e681..4a0d316 100644 --- a/drivers/hwmon/Makefile +++ b/drivers/hwmon/Makefile @@ -5,3 +5,4 @@ obj-$(CONFIG_HWMON) += class.o obj-$(CONFIG_SENSORS_LM75) += lm75.o +obj-$(CONFIG_SENSORS_IMX_THERMAL) += imx_thermal.o diff --git a/drivers/hwmon/imx_thermal.c b/drivers/hwmon/imx_thermal.c new file mode 100644 index 0000000..f9c506b --- /dev/null +++ b/drivers/hwmon/imx_thermal.c @@ -0,0 +1,234 @@ +/* + * tempmon.c - handle most I2C EEPROMs + * + * Copyright (C) 2005-2007 David Brownell + * Copyright (C) 2008 Wolfram Sang, Pengutronix + * + * 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; either version 2 of the License, or + * (at your option) any later version. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define FACTOR0 10000000 +#define MEASURE_FREQ 327 +#define OCOTP_ANA1_OFFSET (0xE * sizeof(uint32_t)) + +struct imx_thermal_data { + int c1, c2; + void __iomem *base; + struct clk *clk; + + struct hwmon_sensor sensor; +}; + +static inline struct imx_thermal_data * +to_imx_thermal_data(struct hwmon_sensor *sensor) +{ + return container_of(sensor, struct imx_thermal_data, sensor); +} + + +static int imx_thermal_read_temperature(struct hwmon_sensor *sensor, + s32 *reading) +{ + uint64_t start; + uint32_t tempsense0, tempsense1; + uint16_t n_meas; + struct imx_thermal_data *imx_thermal = to_imx_thermal_data(sensor); + + /* + * 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->sensor.dev, + "timeout waiting for TEMPMON 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); + + *reading = (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 cdev *ocotp; + struct device_d *anatop; + int t1, n1, t2, n2; + int ret; + + node = of_parse_phandle(dev->device_node, "fsl,tempmon-data", 0); + if (!node) { + dev_err(dev, "no calibration data source\n"); + return -ENODEV; + } + + ocotp = cdev_by_device_node(node); + if (!ocotp) { + dev_err(dev, "no OCOTP character device\n"); + return -ENODEV; + } + + ret = cdev_do_open(ocotp, O_RDONLY); + if (!ret) { + ret = cdev_read(ocotp, + &ocotp_ana1, sizeof(ocotp_ana1), + OCOTP_ANA1_OFFSET, 0); + + cdev_close(ocotp); + + if (ret != sizeof(ocotp_ana1)) { + dev_err(dev, "failed to read calibration data\n"); + return -EINVAL; + } + } else { + dev_err(dev, "failed to open %s\n", ocotp->name); + return -EINVAL; + } + + if (ocotp_ana1 == 0 || ocotp_ana1 == ~0) { + dev_err(dev, "invalid sensor calibration data\n"); + return -EINVAL; + } + + node = of_parse_phandle(dev->device_node, "fsl,tempmon", 0); + if (!node) { + dev_err(dev, "failed to get the value of 'fsl,tempmon'\n"); + return -ENODEV; + } + + anatop = of_find_device_by_node(node); + if (!anatop) { + dev_err(dev, "failed to find 'fsl,tempmon' device\n"); + return -ENODEV; + + } + + imx_thermal = xzalloc(sizeof(*imx_thermal)); + + imx_thermal->base = dev_request_mem_region(anatop, 0); + if (IS_ERR(imx_thermal->base)) { + ret = PTR_ERR(imx_thermal->base); + goto fail; + } + + 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 fail; + } + + clk_enable(imx_thermal->clk); + + if (!dev->device_node) { + dev_err(dev, "No device_node associated with the device\n"); + ret = -EINVAL; + goto disable_clock; + } + + imx_thermal->sensor.name = of_get_property(dev->device_node, + "barebox,sensor-name", + NULL); + if (!imx_thermal->sensor.name) { + dev_err(dev, "No sensor-name specified\n"); + ret = -EINVAL; + goto disable_clock; + } + + imx_thermal->sensor.read = imx_thermal_read_temperature; + imx_thermal->sensor.dev = dev; + + return hwmon_sensor_register(&imx_thermal->sensor); + +disable_clock: + clk_disable(imx_thermal->clk); +fail: + kfree(imx_thermal); + 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), +}; + +static int imx_thermal_init(void) +{ + platform_driver_register(&imx_thermal_driver); + return 0; +} +device_initcall(imx_thermal_init); -- 2.5.0 _______________________________________________ barebox mailing list barebox@lists.infradead.org http://lists.infradead.org/mailman/listinfo/barebox