1/* 2 * Copyright 2017, Data61 3 * Commonwealth Scientific and Industrial Research Organisation (CSIRO) 4 * ABN 41 687 119 230. 5 * 6 * This software may be distributed and modified according to the terms of 7 * the BSD 2-Clause license. Note that NO WARRANTY is provided. 8 * See "LICENSE_BSD2.txt" for details. 9 * 10 * @TAG(DATA61_BSD) 11 */ 12 13#pragma once 14 15#include <stdint.h> 16#include <platsupport/io.h> 17#include <platsupport/plat/tmu.h> 18#include <utils/temperature.h> 19#include <utils/zf_log.h> 20 21/* Interface for a temperature sensor driver */ 22 23typedef struct ps_tmu ps_tmu_t; 24 25struct ps_tmu { 26 void* priv; 27 28 /* Operations for the temperature sensor */ 29 temperature_t (*get_temperature)(ps_tmu_t* tmu); 30 uint32_t (*get_raw_temperature)(ps_tmu_t* tmu); 31}; 32 33/* 34 * Initialize a temperature sensor driver 35 * @param[in] id the id of the temperature sensor 36 * @param[in] ops a structure containing OS specific operations for memory access 37 * @param[out] dev temperature sensor driver to populate 38 * @return -1 on error, otherwise returns 0 39 */ 40int ps_tmu_init(enum tmu_id id, ps_io_ops_t* ops, ps_tmu_t* dev); 41 42/* 43 * Read the current temperature sensor value, converting to millikelvins 44 * @param[in] d The device to read data from 45 * @return The current temperature in millikelvin 46 */ 47static inline temperature_t ps_tmu_get_temperature(ps_tmu_t* d) 48{ 49 if (!d || !d->get_temperature) { 50 ZF_LOGF("TMU driver not initialized"); 51 } 52 return d->get_temperature(d); 53} 54 55/* 56 * Read the raw value from the temperature sensor 57 * @param[in] d The device to read data from 58 * @return The raw temperature sensor value 59 */ 60static inline uint32_t ps_tmu_get_raw_temperature(ps_tmu_t *d) 61{ 62 if (!d || !d->get_raw_temperature) { 63 ZF_LOGF("TMU driver not initialized"); 64 } 65 return d->get_raw_temperature(d); 66} 67 68/* 69 * Read the current temperature sensor value, converting to millicelsius 70 * @param[in] d The device to read data from 71 * @return The current temperature in millicelsius 72 */ 73static inline millicelcius_t ps_tmu_get_temperature_millicelsius(ps_tmu_t* d) 74{ 75 return millikelvin_to_millicelcius(ps_tmu_get_temperature(d)); 76} 77 78/* 79 * Read the current temperature sensor value, converting to degrees celsius 80 * @param[in] d The device to read data from 81 * @return The current temperature in degrees celsius 82 */ 83static inline celcius_t ps_tmu_get_temperature_celsius(ps_tmu_t* d) 84{ 85 return ps_tmu_get_temperature_millicelsius(d) / MILLICELCIUS_IN_CELCIUS; 86} 87 88