blob: bc14dc874594e4d9fd37a68874bb6adb6eb4b562 [file] [log] [blame]
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001/*
Eduardo Valentin03e859d2013-03-19 10:54:21 -04002 * TI Bandgap temperature sensor driver
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03003 *
4 * Copyright (C) 2011-2012 Texas Instruments Incorporated - http://www.ti.com/
5 * Author: J Keerthy <j-keerthy@ti.com>
6 * Author: Moiz Sonasath <m-sonasath@ti.com>
7 * Couple of fixes, DT and MFD adaptation:
8 * Eduardo Valentin <eduardo.valentin@ti.com>
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * version 2 as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22 * 02110-1301 USA
23 *
24 */
25
26#include <linux/module.h>
27#include <linux/export.h>
28#include <linux/init.h>
29#include <linux/kernel.h>
30#include <linux/interrupt.h>
31#include <linux/clk.h>
32#include <linux/gpio.h>
33#include <linux/platform_device.h>
34#include <linux/err.h>
35#include <linux/types.h>
Eduardo Valentinebf0bd52013-03-15 09:00:35 -040036#include <linux/spinlock.h>
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +030037#include <linux/reboot.h>
38#include <linux/of_device.h>
39#include <linux/of_platform.h>
40#include <linux/of_irq.h>
Eduardo Valentin57d16172013-06-07 11:11:53 -040041#include <linux/of_gpio.h>
Eduardo Valentin2aeeb8a2012-11-13 14:10:00 -040042#include <linux/io.h>
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +030043
Eduardo Valentin7372add2013-03-19 10:54:19 -040044#include "ti-bandgap.h"
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +030045
Eduardo Valentin8abbe712013-03-15 09:00:05 -040046/*** Helper functions to access registers and their bitfields ***/
47
Eduardo Valentin9c468aa2013-03-15 08:59:56 -040048/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -040049 * ti_bandgap_readl() - simple read helper function
50 * @bgp: pointer to ti_bandgap structure
Eduardo Valentin9c468aa2013-03-15 08:59:56 -040051 * @reg: desired register (offset) to be read
52 *
53 * Helper function to read bandgap registers. It uses the io remapped area.
Nishanth Menon169e8d02013-04-01 12:04:34 -040054 * Return: the register value.
Eduardo Valentin9c468aa2013-03-15 08:59:56 -040055 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -040056static u32 ti_bandgap_readl(struct ti_bandgap *bgp, u32 reg)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +030057{
Eduardo Valentind7f080e2013-03-19 10:54:18 -040058 return readl(bgp->base + reg);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +030059}
60
Eduardo Valentin9c468aa2013-03-15 08:59:56 -040061/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -040062 * ti_bandgap_writel() - simple write helper function
63 * @bgp: pointer to ti_bandgap structure
Eduardo Valentin9c468aa2013-03-15 08:59:56 -040064 * @val: desired register value to be written
65 * @reg: desired register (offset) to be written
66 *
67 * Helper function to write bandgap registers. It uses the io remapped area.
68 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -040069static void ti_bandgap_writel(struct ti_bandgap *bgp, u32 val, u32 reg)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +030070{
Eduardo Valentind7f080e2013-03-19 10:54:18 -040071 writel(val, bgp->base + reg);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +030072}
73
Eduardo Valentin9c468aa2013-03-15 08:59:56 -040074/**
75 * DOC: macro to update bits.
76 *
77 * RMW_BITS() - used to read, modify and update bandgap bitfields.
78 * The value passed will be shifted.
79 */
Eduardo Valentind7f080e2013-03-19 10:54:18 -040080#define RMW_BITS(bgp, id, reg, mask, val) \
Eduardo Valentind3c291a2013-03-15 08:59:55 -040081do { \
82 struct temp_sensor_registers *t; \
83 u32 r; \
84 \
Eduardo Valentind7f080e2013-03-19 10:54:18 -040085 t = bgp->conf->sensors[(id)].registers; \
Eduardo Valentin03e859d2013-03-19 10:54:21 -040086 r = ti_bandgap_readl(bgp, t->reg); \
Eduardo Valentind3c291a2013-03-15 08:59:55 -040087 r &= ~t->mask; \
88 r |= (val) << __ffs(t->mask); \
Eduardo Valentin03e859d2013-03-19 10:54:21 -040089 ti_bandgap_writel(bgp, r, t->reg); \
Eduardo Valentind3c291a2013-03-15 08:59:55 -040090} while (0)
91
Eduardo Valentin6ab52402013-03-15 09:00:06 -040092/*** Basic helper functions ***/
93
Eduardo Valentin7a556e62013-03-15 08:59:58 -040094/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -040095 * ti_bandgap_power() - controls the power state of a bandgap device
96 * @bgp: pointer to ti_bandgap structure
Eduardo Valentin7a556e62013-03-15 08:59:58 -040097 * @on: desired power state (1 - on, 0 - off)
98 *
99 * Used to power on/off a bandgap device instance. Only used on those
100 * that features tempsoff bit.
Nishanth Menon169e8d02013-04-01 12:04:34 -0400101 *
102 * Return: 0 on success, -ENOTSUPP if tempsoff is not supported.
Eduardo Valentin7a556e62013-03-15 08:59:58 -0400103 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400104static int ti_bandgap_power(struct ti_bandgap *bgp, bool on)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300105{
Eduardo Valentin422a3062013-04-01 12:04:33 -0400106 int i, ret = 0;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300107
Eduardo Valentin422a3062013-04-01 12:04:33 -0400108 if (!TI_BANDGAP_HAS(bgp, POWER_SWITCH)) {
109 ret = -ENOTSUPP;
Eduardo Valentin3d84e522013-03-15 08:59:57 -0400110 goto exit;
Eduardo Valentin422a3062013-04-01 12:04:33 -0400111 }
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300112
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400113 for (i = 0; i < bgp->conf->sensor_count; i++)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300114 /* active on 0 */
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400115 RMW_BITS(bgp, i, temp_sensor_ctrl, bgap_tempsoff_mask, !on);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300116
Eduardo Valentin3d84e522013-03-15 08:59:57 -0400117exit:
Eduardo Valentin422a3062013-04-01 12:04:33 -0400118 return ret;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300119}
120
Eduardo Valentin4a6554e2013-03-15 08:59:59 -0400121/**
Keerthy79010632015-04-22 18:21:41 +0530122 * ti_errata814_bandgap_read_temp() - helper function to read dra7 sensor temperature
123 * @bgp: pointer to ti_bandgap structure
124 * @reg: desired register (offset) to be read
125 *
126 * Function to read dra7 bandgap sensor temperature. This is done separately
127 * so as to workaround the errata "Bandgap Temperature read Dtemp can be
128 * corrupted" - Errata ID: i814".
129 * Read accesses to registers listed below can be corrupted due to incorrect
130 * resynchronization between clock domains.
131 * Read access to registers below can be corrupted :
132 * CTRL_CORE_DTEMP_MPU/GPU/CORE/DSPEVE/IVA_n (n = 0 to 4)
133 * CTRL_CORE_TEMP_SENSOR_MPU/GPU/CORE/DSPEVE/IVA_n
134 *
135 * Return: the register value.
136 */
137static u32 ti_errata814_bandgap_read_temp(struct ti_bandgap *bgp, u32 reg)
138{
139 u32 val1, val2;
140
141 val1 = ti_bandgap_readl(bgp, reg);
142 val2 = ti_bandgap_readl(bgp, reg);
143
144 /* If both times we read the same value then that is right */
145 if (val1 == val2)
146 return val1;
147
148 /* if val1 and val2 are different read it third time */
149 return ti_bandgap_readl(bgp, reg);
150}
151
152/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400153 * ti_bandgap_read_temp() - helper function to read sensor temperature
154 * @bgp: pointer to ti_bandgap structure
Eduardo Valentin4a6554e2013-03-15 08:59:59 -0400155 * @id: bandgap sensor id
156 *
157 * Function to concentrate the steps to read sensor temperature register.
158 * This function is desired because, depending on bandgap device version,
159 * it might be needed to freeze the bandgap state machine, before fetching
160 * the register value.
Nishanth Menon169e8d02013-04-01 12:04:34 -0400161 *
162 * Return: temperature in ADC values.
Eduardo Valentin4a6554e2013-03-15 08:59:59 -0400163 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400164static u32 ti_bandgap_read_temp(struct ti_bandgap *bgp, int id)
Eduardo Valentin194a54f2013-02-26 18:53:33 -0400165{
166 struct temp_sensor_registers *tsr;
Eduardo Valentind3c291a2013-03-15 08:59:55 -0400167 u32 temp, reg;
Eduardo Valentin194a54f2013-02-26 18:53:33 -0400168
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400169 tsr = bgp->conf->sensors[id].registers;
Eduardo Valentin194a54f2013-02-26 18:53:33 -0400170 reg = tsr->temp_sensor_ctrl;
171
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400172 if (TI_BANDGAP_HAS(bgp, FREEZE_BIT)) {
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400173 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 1);
Eduardo Valentin194a54f2013-02-26 18:53:33 -0400174 /*
175 * In case we cannot read from cur_dtemp / dtemp_0,
176 * then we read from the last valid temp read
177 */
178 reg = tsr->ctrl_dtemp_1;
179 }
180
181 /* read temperature */
Keerthy79010632015-04-22 18:21:41 +0530182 if (TI_BANDGAP_HAS(bgp, ERRATA_814))
183 temp = ti_errata814_bandgap_read_temp(bgp, reg);
184 else
185 temp = ti_bandgap_readl(bgp, reg);
186
Eduardo Valentin194a54f2013-02-26 18:53:33 -0400187 temp &= tsr->bgap_dtemp_mask;
188
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400189 if (TI_BANDGAP_HAS(bgp, FREEZE_BIT))
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400190 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 0);
Eduardo Valentin194a54f2013-02-26 18:53:33 -0400191
192 return temp;
193}
194
Eduardo Valentinfb65b882013-03-15 09:00:07 -0400195/*** IRQ handlers ***/
196
Eduardo Valentinee07d552013-03-15 09:00:01 -0400197/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400198 * ti_bandgap_talert_irq_handler() - handles Temperature alert IRQs
Eduardo Valentinee07d552013-03-15 09:00:01 -0400199 * @irq: IRQ number
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400200 * @data: private data (struct ti_bandgap *)
Eduardo Valentinee07d552013-03-15 09:00:01 -0400201 *
202 * This is the Talert handler. Use it only if bandgap device features
203 * HAS(TALERT). This handler goes over all sensors and checks their
204 * conditions and acts accordingly. In case there are events pending,
205 * it will reset the event mask to wait for the opposite event (next event).
206 * Every time there is a new event, it will be reported to thermal layer.
Nishanth Menon169e8d02013-04-01 12:04:34 -0400207 *
208 * Return: IRQ_HANDLED
Eduardo Valentinee07d552013-03-15 09:00:01 -0400209 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400210static irqreturn_t ti_bandgap_talert_irq_handler(int irq, void *data)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300211{
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400212 struct ti_bandgap *bgp = data;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300213 struct temp_sensor_registers *tsr;
Eduardo Valentin194a54f2013-02-26 18:53:33 -0400214 u32 t_hot = 0, t_cold = 0, ctrl;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300215 int i;
216
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400217 spin_lock(&bgp->lock);
218 for (i = 0; i < bgp->conf->sensor_count; i++) {
219 tsr = bgp->conf->sensors[i].registers;
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400220 ctrl = ti_bandgap_readl(bgp, tsr->bgap_status);
Eduardo Valentine555c952013-03-15 09:00:04 -0400221
222 /* Read the status of t_hot */
223 t_hot = ctrl & tsr->status_hot_mask;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300224
225 /* Read the status of t_cold */
Eduardo Valentine555c952013-03-15 09:00:04 -0400226 t_cold = ctrl & tsr->status_cold_mask;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300227
228 if (!t_cold && !t_hot)
229 continue;
230
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400231 ctrl = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300232 /*
233 * One TALERT interrupt: Two sources
234 * If the interrupt is due to t_hot then mask t_hot and
235 * and unmask t_cold else mask t_cold and unmask t_hot
236 */
237 if (t_hot) {
238 ctrl &= ~tsr->mask_hot_mask;
239 ctrl |= tsr->mask_cold_mask;
240 } else if (t_cold) {
241 ctrl &= ~tsr->mask_cold_mask;
242 ctrl |= tsr->mask_hot_mask;
243 }
244
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400245 ti_bandgap_writel(bgp, ctrl, tsr->bgap_mask_ctrl);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300246
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400247 dev_dbg(bgp->dev,
Eduardo Valentin71e303f2012-11-13 14:10:03 -0400248 "%s: IRQ from %s sensor: hotevent %d coldevent %d\n",
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400249 __func__, bgp->conf->sensors[i].domain,
Eduardo Valentin71e303f2012-11-13 14:10:03 -0400250 t_hot, t_cold);
251
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300252 /* report temperature to whom may concern */
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400253 if (bgp->conf->report_temperature)
254 bgp->conf->report_temperature(bgp, i);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300255 }
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400256 spin_unlock(&bgp->lock);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300257
258 return IRQ_HANDLED;
259}
260
Eduardo Valentin79857cd22013-03-15 09:00:02 -0400261/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400262 * ti_bandgap_tshut_irq_handler() - handles Temperature shutdown signal
Eduardo Valentin79857cd22013-03-15 09:00:02 -0400263 * @irq: IRQ number
264 * @data: private data (unused)
265 *
266 * This is the Tshut handler. Use it only if bandgap device features
267 * HAS(TSHUT). If any sensor fires the Tshut signal, we simply shutdown
268 * the system.
Nishanth Menon169e8d02013-04-01 12:04:34 -0400269 *
270 * Return: IRQ_HANDLED
Eduardo Valentin79857cd22013-03-15 09:00:02 -0400271 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400272static irqreturn_t ti_bandgap_tshut_irq_handler(int irq, void *data)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300273{
Ruslan Ruslichenkob3bf0e92013-02-26 18:53:24 -0400274 pr_emerg("%s: TSHUT temperature reached. Needs shut down...\n",
275 __func__);
276
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300277 orderly_poweroff(true);
278
279 return IRQ_HANDLED;
280}
281
Eduardo Valentin2f6af4b2013-03-15 09:00:08 -0400282/*** Helper functions which manipulate conversion ADC <-> mi Celsius ***/
283
Eduardo Valentin2577e932013-03-15 09:00:11 -0400284/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400285 * ti_bandgap_adc_to_mcelsius() - converts an ADC value to mCelsius scale
286 * @bgp: struct ti_bandgap pointer
Eduardo Valentin2577e932013-03-15 09:00:11 -0400287 * @adc_val: value in ADC representation
288 * @t: address where to write the resulting temperature in mCelsius
289 *
290 * Simple conversion from ADC representation to mCelsius. In case the ADC value
291 * is out of the ADC conv table range, it returns -ERANGE, 0 on success.
292 * The conversion table is indexed by the ADC values.
Nishanth Menon169e8d02013-04-01 12:04:34 -0400293 *
294 * Return: 0 if conversion was successful, else -ERANGE in case the @adc_val
295 * argument is out of the ADC conv table range.
Eduardo Valentin2577e932013-03-15 09:00:11 -0400296 */
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300297static
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400298int ti_bandgap_adc_to_mcelsius(struct ti_bandgap *bgp, int adc_val, int *t)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300299{
Eduardo Valentin9879b2c2013-03-19 10:54:23 -0400300 const struct ti_bandgap_data *conf = bgp->conf;
Eduardo Valentin20470592013-03-15 09:00:10 -0400301 int ret = 0;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300302
303 /* look up for temperature in the table and return the temperature */
Eduardo Valentin26a70ed2013-03-15 09:00:14 -0400304 if (adc_val < conf->adc_start_val || adc_val > conf->adc_end_val) {
Eduardo Valentin20470592013-03-15 09:00:10 -0400305 ret = -ERANGE;
306 goto exit;
307 }
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300308
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400309 *t = bgp->conf->conv_table[adc_val - conf->adc_start_val];
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300310
Eduardo Valentin20470592013-03-15 09:00:10 -0400311exit:
312 return ret;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300313}
314
Eduardo Valentine7f60b52013-03-15 09:00:15 -0400315/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400316 * ti_bandgap_mcelsius_to_adc() - converts a mCelsius value to ADC scale
317 * @bgp: struct ti_bandgap pointer
Eduardo Valentine7f60b52013-03-15 09:00:15 -0400318 * @temp: value in mCelsius
319 * @adc: address where to write the resulting temperature in ADC representation
320 *
321 * Simple conversion from mCelsius to ADC values. In case the temp value
322 * is out of the ADC conv table range, it returns -ERANGE, 0 on success.
323 * The conversion table is indexed by the ADC values.
Nishanth Menon169e8d02013-04-01 12:04:34 -0400324 *
325 * Return: 0 if conversion was successful, else -ERANGE in case the @temp
326 * argument is out of the ADC conv table range.
Eduardo Valentine7f60b52013-03-15 09:00:15 -0400327 */
Eduardo Valentine16f0722013-03-15 09:00:12 -0400328static
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400329int ti_bandgap_mcelsius_to_adc(struct ti_bandgap *bgp, long temp, int *adc)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300330{
Eduardo Valentin9879b2c2013-03-19 10:54:23 -0400331 const struct ti_bandgap_data *conf = bgp->conf;
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400332 const int *conv_table = bgp->conf->conv_table;
Eduardo Valentina6194772013-03-15 09:00:13 -0400333 int high, low, mid, ret = 0;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300334
335 low = 0;
Eduardo Valentin26a70ed2013-03-15 09:00:14 -0400336 high = conf->adc_end_val - conf->adc_start_val;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300337 mid = (high + low) / 2;
338
Eduardo Valentina6194772013-03-15 09:00:13 -0400339 if (temp < conv_table[low] || temp > conv_table[high]) {
340 ret = -ERANGE;
341 goto exit;
342 }
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300343
344 while (low < high) {
Eduardo Valentinc8a8f842013-02-26 18:53:36 -0400345 if (temp < conv_table[mid])
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300346 high = mid - 1;
347 else
348 low = mid + 1;
349 mid = (low + high) / 2;
350 }
351
Eduardo Valentin26a70ed2013-03-15 09:00:14 -0400352 *adc = conf->adc_start_val + low;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300353
Eduardo Valentina6194772013-03-15 09:00:13 -0400354exit:
355 return ret;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300356}
357
Eduardo Valentin8a1cefe2013-03-15 09:00:17 -0400358/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400359 * ti_bandgap_add_hyst() - add hysteresis (in mCelsius) to an ADC value
360 * @bgp: struct ti_bandgap pointer
Eduardo Valentin8a1cefe2013-03-15 09:00:17 -0400361 * @adc_val: temperature value in ADC representation
362 * @hyst_val: hysteresis value in mCelsius
363 * @sum: address where to write the resulting temperature (in ADC scale)
364 *
365 * Adds an hysteresis value (in mCelsius) to a ADC temperature value.
Nishanth Menon169e8d02013-04-01 12:04:34 -0400366 *
367 * Return: 0 on success, -ERANGE otherwise.
Eduardo Valentin8a1cefe2013-03-15 09:00:17 -0400368 */
Eduardo Valentin0f0ed7d2013-03-15 09:00:16 -0400369static
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400370int ti_bandgap_add_hyst(struct ti_bandgap *bgp, int adc_val, int hyst_val,
371 u32 *sum)
Eduardo Valentin0f0ed7d2013-03-15 09:00:16 -0400372{
373 int temp, ret;
374
375 /*
376 * Need to add in the mcelsius domain, so we have a temperature
377 * the conv_table range
378 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400379 ret = ti_bandgap_adc_to_mcelsius(bgp, adc_val, &temp);
Eduardo Valentin0f0ed7d2013-03-15 09:00:16 -0400380 if (ret < 0)
381 goto exit;
382
383 temp += hyst_val;
384
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400385 ret = ti_bandgap_mcelsius_to_adc(bgp, temp, sum);
Eduardo Valentin0f0ed7d2013-03-15 09:00:16 -0400386
387exit:
388 return ret;
389}
390
Eduardo Valentinf8ccce22013-03-15 09:00:18 -0400391/*** Helper functions handling device Alert/Shutdown signals ***/
392
Eduardo Valentinf47f6d32013-03-15 09:00:20 -0400393/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400394 * ti_bandgap_unmask_interrupts() - unmasks the events of thot & tcold
395 * @bgp: struct ti_bandgap pointer
Eduardo Valentin61603af2013-03-19 10:54:25 -0400396 * @id: bandgap sensor id
Eduardo Valentinf47f6d32013-03-15 09:00:20 -0400397 * @t_hot: hot temperature value to trigger alert signal
398 * @t_cold: cold temperature value to trigger alert signal
399 *
400 * Checks the requested t_hot and t_cold values and configures the IRQ event
401 * masks accordingly. Call this function only if bandgap features HAS(TALERT).
402 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400403static void ti_bandgap_unmask_interrupts(struct ti_bandgap *bgp, int id,
404 u32 t_hot, u32 t_cold)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300405{
406 struct temp_sensor_registers *tsr;
407 u32 temp, reg_val;
408
409 /* Read the current on die temperature */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400410 temp = ti_bandgap_read_temp(bgp, id);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300411
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400412 tsr = bgp->conf->sensors[id].registers;
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400413 reg_val = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl);
Eduardo Valentin194a54f2013-02-26 18:53:33 -0400414
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300415 if (temp < t_hot)
416 reg_val |= tsr->mask_hot_mask;
417 else
418 reg_val &= ~tsr->mask_hot_mask;
419
420 if (t_cold < temp)
421 reg_val |= tsr->mask_cold_mask;
422 else
423 reg_val &= ~tsr->mask_cold_mask;
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400424 ti_bandgap_writel(bgp, reg_val, tsr->bgap_mask_ctrl);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300425}
426
Eduardo Valentin38d99e82013-03-15 09:00:27 -0400427/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400428 * ti_bandgap_update_alert_threshold() - sequence to update thresholds
429 * @bgp: struct ti_bandgap pointer
Eduardo Valentin38d99e82013-03-15 09:00:27 -0400430 * @id: bandgap sensor id
431 * @val: value (ADC) of a new threshold
432 * @hot: desired threshold to be updated. true if threshold hot, false if
433 * threshold cold
434 *
435 * It will program the required thresholds (hot and cold) for TALERT signal.
436 * This function can be used to update t_hot or t_cold, depending on @hot value.
437 * It checks the resulting t_hot and t_cold values, based on the new passed @val
438 * and configures the thresholds so that t_hot is always greater than t_cold.
439 * Call this function only if bandgap features HAS(TALERT).
Nishanth Menon169e8d02013-04-01 12:04:34 -0400440 *
441 * Return: 0 if no error, else corresponding error
Eduardo Valentin38d99e82013-03-15 09:00:27 -0400442 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400443static int ti_bandgap_update_alert_threshold(struct ti_bandgap *bgp, int id,
444 int val, bool hot)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300445{
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400446 struct temp_sensor_data *ts_data = bgp->conf->sensors[id].ts_data;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300447 struct temp_sensor_registers *tsr;
Keerthye9a90d02015-04-22 18:21:42 +0530448 u32 thresh_val, reg_val, t_hot, t_cold, ctrl;
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400449 int err = 0;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300450
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400451 tsr = bgp->conf->sensors[id].registers;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300452
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400453 /* obtain the current value */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400454 thresh_val = ti_bandgap_readl(bgp, tsr->bgap_threshold);
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400455 t_cold = (thresh_val & tsr->threshold_tcold_mask) >>
456 __ffs(tsr->threshold_tcold_mask);
457 t_hot = (thresh_val & tsr->threshold_thot_mask) >>
458 __ffs(tsr->threshold_thot_mask);
459 if (hot)
460 t_hot = val;
461 else
462 t_cold = val;
463
Eduardo Valentinf5d43b72013-03-19 10:54:26 -0400464 if (t_cold > t_hot) {
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400465 if (hot)
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400466 err = ti_bandgap_add_hyst(bgp, t_hot,
467 -ts_data->hyst_val,
468 &t_cold);
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400469 else
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400470 err = ti_bandgap_add_hyst(bgp, t_cold,
471 ts_data->hyst_val,
472 &t_hot);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300473 }
474
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400475 /* write the new threshold values */
Eduardo Valentin0fb3c242013-03-19 10:54:27 -0400476 reg_val = thresh_val &
477 ~(tsr->threshold_thot_mask | tsr->threshold_tcold_mask);
478 reg_val |= (t_hot << __ffs(tsr->threshold_thot_mask)) |
479 (t_cold << __ffs(tsr->threshold_tcold_mask));
Keerthye9a90d02015-04-22 18:21:42 +0530480
481 /**
482 * Errata i813:
483 * Spurious Thermal Alert: Talert can happen randomly while the device
484 * remains under the temperature limit defined for this event to trig.
485 * This spurious event is caused by a incorrect re-synchronization
486 * between clock domains. The comparison between configured threshold
487 * and current temperature value can happen while the value is
488 * transitioning (metastable), thus causing inappropriate event
489 * generation. No spurious event occurs as long as the threshold value
490 * stays unchanged. Spurious event can be generated while a thermal
491 * alert threshold is modified in
492 * CONTROL_BANDGAP_THRESHOLD_MPU/GPU/CORE/DSPEVE/IVA_n.
493 */
494
495 if (TI_BANDGAP_HAS(bgp, ERRATA_813)) {
496 /* Mask t_hot and t_cold events at the IP Level */
497 ctrl = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl);
498
499 if (hot)
500 ctrl &= ~tsr->mask_hot_mask;
501 else
502 ctrl &= ~tsr->mask_cold_mask;
503
504 ti_bandgap_writel(bgp, ctrl, tsr->bgap_mask_ctrl);
505 }
506
507 /* Write the threshold value */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400508 ti_bandgap_writel(bgp, reg_val, tsr->bgap_threshold);
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400509
Keerthye9a90d02015-04-22 18:21:42 +0530510 if (TI_BANDGAP_HAS(bgp, ERRATA_813)) {
511 /* Unmask t_hot and t_cold events at the IP Level */
512 ctrl = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl);
513 if (hot)
514 ctrl |= tsr->mask_hot_mask;
515 else
516 ctrl |= tsr->mask_cold_mask;
517
518 ti_bandgap_writel(bgp, ctrl, tsr->bgap_mask_ctrl);
519 }
520
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300521 if (err) {
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400522 dev_err(bgp->dev, "failed to reprogram thot threshold\n");
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400523 err = -EIO;
524 goto exit;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300525 }
526
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400527 ti_bandgap_unmask_interrupts(bgp, id, t_hot, t_cold);
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400528exit:
529 return err;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300530}
531
Eduardo Valentine72b7bb2013-03-15 09:00:38 -0400532/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400533 * ti_bandgap_validate() - helper to check the sanity of a struct ti_bandgap
534 * @bgp: struct ti_bandgap pointer
Eduardo Valentine72b7bb2013-03-15 09:00:38 -0400535 * @id: bandgap sensor id
536 *
537 * Checks if the bandgap pointer is valid and if the sensor id is also
538 * applicable.
Nishanth Menon169e8d02013-04-01 12:04:34 -0400539 *
540 * Return: 0 if no errors, -EINVAL for invalid @bgp pointer or -ERANGE if
541 * @id cannot index @bgp sensors.
Eduardo Valentine72b7bb2013-03-15 09:00:38 -0400542 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400543static inline int ti_bandgap_validate(struct ti_bandgap *bgp, int id)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300544{
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400545 int ret = 0;
546
Eduardo Valentin0c12b5a2013-05-29 15:07:43 +0000547 if (!bgp || IS_ERR(bgp)) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300548 pr_err("%s: invalid bandgap pointer\n", __func__);
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400549 ret = -EINVAL;
550 goto exit;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300551 }
552
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400553 if ((id < 0) || (id >= bgp->conf->sensor_count)) {
554 dev_err(bgp->dev, "%s: sensor id out of range (%d)\n",
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300555 __func__, id);
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400556 ret = -ERANGE;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300557 }
558
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400559exit:
560 return ret;
561}
562
Eduardo Valentin9efa93b2013-03-15 09:00:28 -0400563/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400564 * _ti_bandgap_write_threshold() - helper to update TALERT t_cold or t_hot
565 * @bgp: struct ti_bandgap pointer
Eduardo Valentin9efa93b2013-03-15 09:00:28 -0400566 * @id: bandgap sensor id
567 * @val: value (mCelsius) of a new threshold
568 * @hot: desired threshold to be updated. true if threshold hot, false if
569 * threshold cold
570 *
571 * It will update the required thresholds (hot and cold) for TALERT signal.
572 * This function can be used to update t_hot or t_cold, depending on @hot value.
573 * Validates the mCelsius range and update the requested threshold.
574 * Call this function only if bandgap features HAS(TALERT).
Nishanth Menon169e8d02013-04-01 12:04:34 -0400575 *
576 * Return: 0 if no error, else corresponding error value.
Eduardo Valentin9efa93b2013-03-15 09:00:28 -0400577 */
Eduardo Valentin2f8ec2a2013-03-19 10:54:22 -0400578static int _ti_bandgap_write_threshold(struct ti_bandgap *bgp, int id, int val,
579 bool hot)
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400580{
581 struct temp_sensor_data *ts_data;
582 struct temp_sensor_registers *tsr;
583 u32 adc_val;
584 int ret;
585
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400586 ret = ti_bandgap_validate(bgp, id);
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400587 if (ret)
588 goto exit;
589
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400590 if (!TI_BANDGAP_HAS(bgp, TALERT)) {
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400591 ret = -ENOTSUPP;
592 goto exit;
593 }
594
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400595 ts_data = bgp->conf->sensors[id].ts_data;
596 tsr = bgp->conf->sensors[id].registers;
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400597 if (hot) {
598 if (val < ts_data->min_temp + ts_data->hyst_val)
599 ret = -EINVAL;
600 } else {
601 if (val > ts_data->max_temp + ts_data->hyst_val)
602 ret = -EINVAL;
603 }
604
605 if (ret)
606 goto exit;
607
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400608 ret = ti_bandgap_mcelsius_to_adc(bgp, val, &adc_val);
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400609 if (ret < 0)
610 goto exit;
611
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400612 spin_lock(&bgp->lock);
Eduardo Valentind52361c2013-03-19 10:54:28 -0400613 ret = ti_bandgap_update_alert_threshold(bgp, id, adc_val, hot);
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400614 spin_unlock(&bgp->lock);
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400615
616exit:
617 return ret;
618}
619
Eduardo Valentin7a681a52013-03-15 09:00:29 -0400620/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400621 * _ti_bandgap_read_threshold() - helper to read TALERT t_cold or t_hot
622 * @bgp: struct ti_bandgap pointer
Eduardo Valentin7a681a52013-03-15 09:00:29 -0400623 * @id: bandgap sensor id
624 * @val: value (mCelsius) of a threshold
625 * @hot: desired threshold to be read. true if threshold hot, false if
626 * threshold cold
627 *
628 * It will fetch the required thresholds (hot and cold) for TALERT signal.
629 * This function can be used to read t_hot or t_cold, depending on @hot value.
630 * Call this function only if bandgap features HAS(TALERT).
Nishanth Menon169e8d02013-04-01 12:04:34 -0400631 *
632 * Return: 0 if no error, -ENOTSUPP if it has no TALERT support, or the
633 * corresponding error value if some operation fails.
Eduardo Valentin7a681a52013-03-15 09:00:29 -0400634 */
Eduardo Valentin2f8ec2a2013-03-19 10:54:22 -0400635static int _ti_bandgap_read_threshold(struct ti_bandgap *bgp, int id,
636 int *val, bool hot)
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400637{
638 struct temp_sensor_registers *tsr;
639 u32 temp, mask;
640 int ret = 0;
641
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400642 ret = ti_bandgap_validate(bgp, id);
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400643 if (ret)
644 goto exit;
645
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400646 if (!TI_BANDGAP_HAS(bgp, TALERT)) {
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400647 ret = -ENOTSUPP;
648 goto exit;
649 }
650
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400651 tsr = bgp->conf->sensors[id].registers;
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400652 if (hot)
653 mask = tsr->threshold_thot_mask;
654 else
655 mask = tsr->threshold_tcold_mask;
656
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400657 temp = ti_bandgap_readl(bgp, tsr->bgap_threshold);
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400658 temp = (temp & mask) >> __ffs(mask);
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400659 ret |= ti_bandgap_adc_to_mcelsius(bgp, temp, &temp);
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400660 if (ret) {
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400661 dev_err(bgp->dev, "failed to read thot\n");
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400662 ret = -EIO;
663 goto exit;
664 }
665
666 *val = temp;
667
668exit:
Eduardo Valentin648b4c62013-03-19 10:54:17 -0400669 return ret;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300670}
671
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400672/*** Exposed APIs ***/
673
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300674/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400675 * ti_bandgap_read_thot() - reads sensor current thot
Eduardo Valentin61603af2013-03-19 10:54:25 -0400676 * @bgp: pointer to bandgap instance
677 * @id: sensor id
678 * @thot: resulting current thot value
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300679 *
Nishanth Menon169e8d02013-04-01 12:04:34 -0400680 * Return: 0 on success or the proper error code
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300681 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400682int ti_bandgap_read_thot(struct ti_bandgap *bgp, int id, int *thot)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300683{
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400684 return _ti_bandgap_read_threshold(bgp, id, thot, true);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300685}
686
687/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400688 * ti_bandgap_write_thot() - sets sensor current thot
Eduardo Valentin61603af2013-03-19 10:54:25 -0400689 * @bgp: pointer to bandgap instance
690 * @id: sensor id
691 * @val: desired thot value
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300692 *
Nishanth Menon169e8d02013-04-01 12:04:34 -0400693 * Return: 0 on success or the proper error code
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300694 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400695int ti_bandgap_write_thot(struct ti_bandgap *bgp, int id, int val)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300696{
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400697 return _ti_bandgap_write_threshold(bgp, id, val, true);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300698}
699
700/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400701 * ti_bandgap_read_tcold() - reads sensor current tcold
Eduardo Valentin61603af2013-03-19 10:54:25 -0400702 * @bgp: pointer to bandgap instance
703 * @id: sensor id
704 * @tcold: resulting current tcold value
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300705 *
Nishanth Menon169e8d02013-04-01 12:04:34 -0400706 * Return: 0 on success or the proper error code
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300707 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400708int ti_bandgap_read_tcold(struct ti_bandgap *bgp, int id, int *tcold)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300709{
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400710 return _ti_bandgap_read_threshold(bgp, id, tcold, false);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300711}
712
713/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400714 * ti_bandgap_write_tcold() - sets the sensor tcold
Eduardo Valentin61603af2013-03-19 10:54:25 -0400715 * @bgp: pointer to bandgap instance
716 * @id: sensor id
717 * @val: desired tcold value
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300718 *
Nishanth Menon169e8d02013-04-01 12:04:34 -0400719 * Return: 0 on success or the proper error code
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300720 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400721int ti_bandgap_write_tcold(struct ti_bandgap *bgp, int id, int val)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300722{
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400723 return _ti_bandgap_write_threshold(bgp, id, val, false);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300724}
725
726/**
J Keerthy58bccd02013-04-01 12:04:42 -0400727 * ti_bandgap_read_counter() - read the sensor counter
728 * @bgp: pointer to bandgap instance
729 * @id: sensor id
730 * @interval: resulting update interval in miliseconds
731 */
732static void ti_bandgap_read_counter(struct ti_bandgap *bgp, int id,
733 int *interval)
734{
735 struct temp_sensor_registers *tsr;
736 int time;
737
738 tsr = bgp->conf->sensors[id].registers;
739 time = ti_bandgap_readl(bgp, tsr->bgap_counter);
740 time = (time & tsr->counter_mask) >>
741 __ffs(tsr->counter_mask);
742 time = time * 1000 / bgp->clk_rate;
743 *interval = time;
744}
745
746/**
747 * ti_bandgap_read_counter_delay() - read the sensor counter delay
748 * @bgp: pointer to bandgap instance
749 * @id: sensor id
750 * @interval: resulting update interval in miliseconds
751 */
752static void ti_bandgap_read_counter_delay(struct ti_bandgap *bgp, int id,
753 int *interval)
754{
755 struct temp_sensor_registers *tsr;
756 int reg_val;
757
758 tsr = bgp->conf->sensors[id].registers;
759
760 reg_val = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl);
761 reg_val = (reg_val & tsr->mask_counter_delay_mask) >>
762 __ffs(tsr->mask_counter_delay_mask);
763 switch (reg_val) {
764 case 0:
765 *interval = 0;
766 break;
767 case 1:
768 *interval = 1;
769 break;
770 case 2:
771 *interval = 10;
772 break;
773 case 3:
774 *interval = 100;
775 break;
776 case 4:
777 *interval = 250;
778 break;
779 case 5:
780 *interval = 500;
781 break;
782 default:
783 dev_warn(bgp->dev, "Wrong counter delay value read from register %X",
784 reg_val);
785 }
786}
787
788/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400789 * ti_bandgap_read_update_interval() - read the sensor update interval
Eduardo Valentin61603af2013-03-19 10:54:25 -0400790 * @bgp: pointer to bandgap instance
791 * @id: sensor id
792 * @interval: resulting update interval in miliseconds
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300793 *
Nishanth Menon169e8d02013-04-01 12:04:34 -0400794 * Return: 0 on success or the proper error code
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300795 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400796int ti_bandgap_read_update_interval(struct ti_bandgap *bgp, int id,
797 int *interval)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300798{
J Keerthy58bccd02013-04-01 12:04:42 -0400799 int ret = 0;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300800
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400801 ret = ti_bandgap_validate(bgp, id);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300802 if (ret)
J Keerthy58bccd02013-04-01 12:04:42 -0400803 goto exit;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300804
J Keerthy58bccd02013-04-01 12:04:42 -0400805 if (!TI_BANDGAP_HAS(bgp, COUNTER) &&
806 !TI_BANDGAP_HAS(bgp, COUNTER_DELAY)) {
807 ret = -ENOTSUPP;
808 goto exit;
809 }
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300810
J Keerthy58bccd02013-04-01 12:04:42 -0400811 if (TI_BANDGAP_HAS(bgp, COUNTER)) {
812 ti_bandgap_read_counter(bgp, id, interval);
813 goto exit;
814 }
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300815
J Keerthy58bccd02013-04-01 12:04:42 -0400816 ti_bandgap_read_counter_delay(bgp, id, interval);
817exit:
818 return ret;
819}
820
821/**
822 * ti_bandgap_write_counter_delay() - set the counter_delay
823 * @bgp: pointer to bandgap instance
824 * @id: sensor id
825 * @interval: desired update interval in miliseconds
826 *
827 * Return: 0 on success or the proper error code
828 */
829static int ti_bandgap_write_counter_delay(struct ti_bandgap *bgp, int id,
830 u32 interval)
831{
832 int rval;
833
834 switch (interval) {
835 case 0: /* Immediate conversion */
836 rval = 0x0;
837 break;
838 case 1: /* Conversion after ever 1ms */
839 rval = 0x1;
840 break;
841 case 10: /* Conversion after ever 10ms */
842 rval = 0x2;
843 break;
844 case 100: /* Conversion after ever 100ms */
845 rval = 0x3;
846 break;
847 case 250: /* Conversion after ever 250ms */
848 rval = 0x4;
849 break;
850 case 500: /* Conversion after ever 500ms */
851 rval = 0x5;
852 break;
853 default:
854 dev_warn(bgp->dev, "Delay %d ms is not supported\n", interval);
855 return -EINVAL;
856 }
857
858 spin_lock(&bgp->lock);
859 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_counter_delay_mask, rval);
860 spin_unlock(&bgp->lock);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300861
862 return 0;
863}
864
865/**
J Keerthy58bccd02013-04-01 12:04:42 -0400866 * ti_bandgap_write_counter() - set the bandgap sensor counter
867 * @bgp: pointer to bandgap instance
868 * @id: sensor id
869 * @interval: desired update interval in miliseconds
870 */
871static void ti_bandgap_write_counter(struct ti_bandgap *bgp, int id,
872 u32 interval)
873{
874 interval = interval * bgp->clk_rate / 1000;
875 spin_lock(&bgp->lock);
876 RMW_BITS(bgp, id, bgap_counter, counter_mask, interval);
877 spin_unlock(&bgp->lock);
878}
879
880/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400881 * ti_bandgap_write_update_interval() - set the update interval
Eduardo Valentin61603af2013-03-19 10:54:25 -0400882 * @bgp: pointer to bandgap instance
883 * @id: sensor id
884 * @interval: desired update interval in miliseconds
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300885 *
Nishanth Menon169e8d02013-04-01 12:04:34 -0400886 * Return: 0 on success or the proper error code
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300887 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400888int ti_bandgap_write_update_interval(struct ti_bandgap *bgp,
889 int id, u32 interval)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300890{
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400891 int ret = ti_bandgap_validate(bgp, id);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300892 if (ret)
J Keerthy58bccd02013-04-01 12:04:42 -0400893 goto exit;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300894
J Keerthy58bccd02013-04-01 12:04:42 -0400895 if (!TI_BANDGAP_HAS(bgp, COUNTER) &&
896 !TI_BANDGAP_HAS(bgp, COUNTER_DELAY)) {
897 ret = -ENOTSUPP;
898 goto exit;
899 }
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300900
J Keerthy58bccd02013-04-01 12:04:42 -0400901 if (TI_BANDGAP_HAS(bgp, COUNTER)) {
902 ti_bandgap_write_counter(bgp, id, interval);
903 goto exit;
904 }
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300905
J Keerthy58bccd02013-04-01 12:04:42 -0400906 ret = ti_bandgap_write_counter_delay(bgp, id, interval);
907exit:
908 return ret;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300909}
910
911/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400912 * ti_bandgap_read_temperature() - report current temperature
Eduardo Valentin61603af2013-03-19 10:54:25 -0400913 * @bgp: pointer to bandgap instance
914 * @id: sensor id
915 * @temperature: resulting temperature
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300916 *
Nishanth Menon169e8d02013-04-01 12:04:34 -0400917 * Return: 0 on success or the proper error code
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300918 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400919int ti_bandgap_read_temperature(struct ti_bandgap *bgp, int id,
920 int *temperature)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300921{
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300922 u32 temp;
923 int ret;
924
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400925 ret = ti_bandgap_validate(bgp, id);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300926 if (ret)
927 return ret;
928
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400929 spin_lock(&bgp->lock);
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400930 temp = ti_bandgap_read_temp(bgp, id);
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400931 spin_unlock(&bgp->lock);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300932
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400933 ret |= ti_bandgap_adc_to_mcelsius(bgp, temp, &temp);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300934 if (ret)
935 return -EIO;
936
937 *temperature = temp;
938
939 return 0;
940}
941
942/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400943 * ti_bandgap_set_sensor_data() - helper function to store thermal
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300944 * framework related data.
Eduardo Valentin61603af2013-03-19 10:54:25 -0400945 * @bgp: pointer to bandgap instance
946 * @id: sensor id
947 * @data: thermal framework related data to be stored
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300948 *
Nishanth Menon169e8d02013-04-01 12:04:34 -0400949 * Return: 0 on success or the proper error code
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300950 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400951int ti_bandgap_set_sensor_data(struct ti_bandgap *bgp, int id, void *data)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300952{
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400953 int ret = ti_bandgap_validate(bgp, id);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300954 if (ret)
955 return ret;
956
Eduardo Valentin9879b2c2013-03-19 10:54:23 -0400957 bgp->regval[id].data = data;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300958
959 return 0;
960}
961
962/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400963 * ti_bandgap_get_sensor_data() - helper function to get thermal
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300964 * framework related data.
Eduardo Valentin61603af2013-03-19 10:54:25 -0400965 * @bgp: pointer to bandgap instance
966 * @id: sensor id
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300967 *
Nishanth Menon169e8d02013-04-01 12:04:34 -0400968 * Return: data stored by set function with sensor id on success or NULL
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300969 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400970void *ti_bandgap_get_sensor_data(struct ti_bandgap *bgp, int id)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300971{
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400972 int ret = ti_bandgap_validate(bgp, id);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300973 if (ret)
974 return ERR_PTR(ret);
975
Eduardo Valentin9879b2c2013-03-19 10:54:23 -0400976 return bgp->regval[id].data;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300977}
978
Eduardo Valentine195aba2013-03-15 09:00:22 -0400979/*** Helper functions used during device initialization ***/
980
Eduardo Valentin31102a72013-03-15 09:00:26 -0400981/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400982 * ti_bandgap_force_single_read() - executes 1 single ADC conversion
983 * @bgp: pointer to struct ti_bandgap
Eduardo Valentin31102a72013-03-15 09:00:26 -0400984 * @id: sensor id which it is desired to read 1 temperature
985 *
986 * Used to initialize the conversion state machine and set it to a valid
987 * state. Called during device initialization and context restore events.
Nishanth Menon169e8d02013-04-01 12:04:34 -0400988 *
989 * Return: 0
Eduardo Valentin31102a72013-03-15 09:00:26 -0400990 */
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300991static int
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400992ti_bandgap_force_single_read(struct ti_bandgap *bgp, int id)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300993{
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300994 u32 temp = 0, counter = 1000;
995
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300996 /* Select single conversion mode */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400997 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400998 RMW_BITS(bgp, id, bgap_mode_ctrl, mode_ctrl_mask, 0);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300999
1000 /* Start of Conversion = 1 */
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001001 RMW_BITS(bgp, id, temp_sensor_ctrl, bgap_soc_mask, 1);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001002 /* Wait until DTEMP is updated */
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001003 temp = ti_bandgap_read_temp(bgp, id);
Eduardo Valentin194a54f2013-02-26 18:53:33 -04001004
1005 while ((temp == 0) && --counter)
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001006 temp = ti_bandgap_read_temp(bgp, id);
Eduardo Valentind3c291a2013-03-15 08:59:55 -04001007 /* REVISIT: Check correct condition for end of conversion */
Eduardo Valentin194a54f2013-02-26 18:53:33 -04001008
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001009 /* Start of Conversion = 0 */
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001010 RMW_BITS(bgp, id, temp_sensor_ctrl, bgap_soc_mask, 0);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001011
1012 return 0;
1013}
1014
1015/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001016 * ti_bandgap_set_continous_mode() - One time enabling of continuous mode
1017 * @bgp: pointer to struct ti_bandgap
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001018 *
Eduardo Valentina84b6f42013-03-15 09:00:25 -04001019 * Call this function only if HAS(MODE_CONFIG) is set. As this driver may
1020 * be used for junction temperature monitoring, it is desirable that the
1021 * sensors are operational all the time, so that alerts are generated
1022 * properly.
Nishanth Menon169e8d02013-04-01 12:04:34 -04001023 *
1024 * Return: 0
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001025 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001026static int ti_bandgap_set_continuous_mode(struct ti_bandgap *bgp)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001027{
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001028 int i;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001029
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001030 for (i = 0; i < bgp->conf->sensor_count; i++) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001031 /* Perform a single read just before enabling continuous */
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001032 ti_bandgap_force_single_read(bgp, i);
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001033 RMW_BITS(bgp, i, bgap_mode_ctrl, mode_ctrl_mask, 1);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001034 }
1035
1036 return 0;
1037}
1038
Eduardo Valentind3790b32013-03-15 09:00:30 -04001039/**
J Keerthy2f440b02013-04-01 12:04:45 -04001040 * ti_bandgap_get_trend() - To fetch the temperature trend of a sensor
1041 * @bgp: pointer to struct ti_bandgap
1042 * @id: id of the individual sensor
1043 * @trend: Pointer to trend.
1044 *
1045 * This function needs to be called to fetch the temperature trend of a
1046 * Particular sensor. The function computes the difference in temperature
1047 * w.r.t time. For the bandgaps with built in history buffer the temperatures
1048 * are read from the buffer and for those without the Buffer -ENOTSUPP is
1049 * returned.
1050 *
1051 * Return: 0 if no error, else return corresponding error. If no
1052 * error then the trend value is passed on to trend parameter
1053 */
1054int ti_bandgap_get_trend(struct ti_bandgap *bgp, int id, int *trend)
1055{
1056 struct temp_sensor_registers *tsr;
1057 u32 temp1, temp2, reg1, reg2;
1058 int t1, t2, interval, ret = 0;
1059
1060 ret = ti_bandgap_validate(bgp, id);
1061 if (ret)
1062 goto exit;
1063
1064 if (!TI_BANDGAP_HAS(bgp, HISTORY_BUFFER) ||
1065 !TI_BANDGAP_HAS(bgp, FREEZE_BIT)) {
1066 ret = -ENOTSUPP;
1067 goto exit;
1068 }
1069
Eduardo Valentinba0049e2013-06-07 19:13:13 +00001070 spin_lock(&bgp->lock);
1071
J Keerthy2f440b02013-04-01 12:04:45 -04001072 tsr = bgp->conf->sensors[id].registers;
1073
1074 /* Freeze and read the last 2 valid readings */
Eduardo Valentinba0049e2013-06-07 19:13:13 +00001075 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 1);
J Keerthy2f440b02013-04-01 12:04:45 -04001076 reg1 = tsr->ctrl_dtemp_1;
1077 reg2 = tsr->ctrl_dtemp_2;
1078
1079 /* read temperature from history buffer */
1080 temp1 = ti_bandgap_readl(bgp, reg1);
1081 temp1 &= tsr->bgap_dtemp_mask;
1082
1083 temp2 = ti_bandgap_readl(bgp, reg2);
1084 temp2 &= tsr->bgap_dtemp_mask;
1085
1086 /* Convert from adc values to mCelsius temperature */
1087 ret = ti_bandgap_adc_to_mcelsius(bgp, temp1, &t1);
1088 if (ret)
Eduardo Valentinba0049e2013-06-07 19:13:13 +00001089 goto unfreeze;
J Keerthy2f440b02013-04-01 12:04:45 -04001090
1091 ret = ti_bandgap_adc_to_mcelsius(bgp, temp2, &t2);
1092 if (ret)
Eduardo Valentinba0049e2013-06-07 19:13:13 +00001093 goto unfreeze;
J Keerthy2f440b02013-04-01 12:04:45 -04001094
1095 /* Fetch the update interval */
1096 ret = ti_bandgap_read_update_interval(bgp, id, &interval);
Ranganath Krishnane838ff82013-08-23 11:08:23 -05001097 if (ret)
Eduardo Valentinba0049e2013-06-07 19:13:13 +00001098 goto unfreeze;
J Keerthy2f440b02013-04-01 12:04:45 -04001099
Ranganath Krishnane838ff82013-08-23 11:08:23 -05001100 /* Set the interval to 1 ms if bandgap counter delay is not set */
1101 if (interval == 0)
1102 interval = 1;
1103
J Keerthy2f440b02013-04-01 12:04:45 -04001104 *trend = (t1 - t2) / interval;
1105
1106 dev_dbg(bgp->dev, "The temperatures are t1 = %d and t2 = %d and trend =%d\n",
1107 t1, t2, *trend);
1108
Eduardo Valentinba0049e2013-06-07 19:13:13 +00001109unfreeze:
1110 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 0);
1111 spin_unlock(&bgp->lock);
J Keerthy2f440b02013-04-01 12:04:45 -04001112exit:
1113 return ret;
1114}
1115
1116/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001117 * ti_bandgap_tshut_init() - setup and initialize tshut handling
1118 * @bgp: pointer to struct ti_bandgap
Eduardo Valentind3790b32013-03-15 09:00:30 -04001119 * @pdev: pointer to device struct platform_device
1120 *
1121 * Call this function only in case the bandgap features HAS(TSHUT).
1122 * In this case, the driver needs to handle the TSHUT signal as an IRQ.
1123 * The IRQ is wired as a GPIO, and for this purpose, it is required
1124 * to specify which GPIO line is used. TSHUT IRQ is fired anytime
1125 * one of the bandgap sensors violates the TSHUT high/hot threshold.
1126 * And in that case, the system must go off.
Nishanth Menon169e8d02013-04-01 12:04:34 -04001127 *
1128 * Return: 0 if no error, else error status
Eduardo Valentind3790b32013-03-15 09:00:30 -04001129 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001130static int ti_bandgap_tshut_init(struct ti_bandgap *bgp,
1131 struct platform_device *pdev)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001132{
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001133 int gpio_nr = bgp->tshut_gpio;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001134 int status;
1135
1136 /* Request for gpio_86 line */
1137 status = gpio_request(gpio_nr, "tshut");
1138 if (status < 0) {
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001139 dev_err(bgp->dev, "Could not request for TSHUT GPIO:%i\n", 86);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001140 return status;
1141 }
1142 status = gpio_direction_input(gpio_nr);
1143 if (status) {
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001144 dev_err(bgp->dev, "Cannot set input TSHUT GPIO %d\n", gpio_nr);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001145 return status;
1146 }
1147
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001148 status = request_irq(gpio_to_irq(gpio_nr), ti_bandgap_tshut_irq_handler,
1149 IRQF_TRIGGER_RISING, "tshut", NULL);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001150 if (status) {
1151 gpio_free(gpio_nr);
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001152 dev_err(bgp->dev, "request irq failed for TSHUT");
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001153 }
1154
1155 return 0;
1156}
1157
Eduardo Valentin094b8ac2013-03-15 09:00:31 -04001158/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001159 * ti_bandgap_alert_init() - setup and initialize talert handling
1160 * @bgp: pointer to struct ti_bandgap
Eduardo Valentin094b8ac2013-03-15 09:00:31 -04001161 * @pdev: pointer to device struct platform_device
1162 *
1163 * Call this function only in case the bandgap features HAS(TALERT).
1164 * In this case, the driver needs to handle the TALERT signals as an IRQs.
1165 * TALERT is a normal IRQ and it is fired any time thresholds (hot or cold)
1166 * are violated. In these situation, the driver must reprogram the thresholds,
1167 * accordingly to specified policy.
Nishanth Menon169e8d02013-04-01 12:04:34 -04001168 *
1169 * Return: 0 if no error, else return corresponding error.
Eduardo Valentin094b8ac2013-03-15 09:00:31 -04001170 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001171static int ti_bandgap_talert_init(struct ti_bandgap *bgp,
1172 struct platform_device *pdev)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001173{
1174 int ret;
1175
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001176 bgp->irq = platform_get_irq(pdev, 0);
1177 if (bgp->irq < 0) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001178 dev_err(&pdev->dev, "get_irq failed\n");
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001179 return bgp->irq;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001180 }
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001181 ret = request_threaded_irq(bgp->irq, NULL,
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001182 ti_bandgap_talert_irq_handler,
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001183 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001184 "talert", bgp);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001185 if (ret) {
1186 dev_err(&pdev->dev, "Request threaded irq failed.\n");
1187 return ret;
1188 }
1189
1190 return 0;
1191}
1192
Eduardo Valentin61603af2013-03-19 10:54:25 -04001193static const struct of_device_id of_ti_bandgap_match[];
Eduardo Valentine9b6f8c2013-03-15 09:00:32 -04001194/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001195 * ti_bandgap_build() - parse DT and setup a struct ti_bandgap
Eduardo Valentine9b6f8c2013-03-15 09:00:32 -04001196 * @pdev: pointer to device struct platform_device
1197 *
1198 * Used to read the device tree properties accordingly to the bandgap
1199 * matching version. Based on bandgap version and its capabilities it
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001200 * will build a struct ti_bandgap out of the required DT entries.
Nishanth Menon169e8d02013-04-01 12:04:34 -04001201 *
1202 * Return: valid bandgap structure if successful, else returns ERR_PTR
1203 * return value must be verified with IS_ERR.
Eduardo Valentine9b6f8c2013-03-15 09:00:32 -04001204 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001205static struct ti_bandgap *ti_bandgap_build(struct platform_device *pdev)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001206{
1207 struct device_node *node = pdev->dev.of_node;
1208 const struct of_device_id *of_id;
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001209 struct ti_bandgap *bgp;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001210 struct resource *res;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001211 int i;
1212
1213 /* just for the sake */
1214 if (!node) {
1215 dev_err(&pdev->dev, "no platform information available\n");
1216 return ERR_PTR(-EINVAL);
1217 }
1218
Eduardo Valentinf6843562013-03-19 10:54:24 -04001219 bgp = devm_kzalloc(&pdev->dev, sizeof(*bgp), GFP_KERNEL);
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001220 if (!bgp) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001221 dev_err(&pdev->dev, "Unable to allocate mem for driver ref\n");
1222 return ERR_PTR(-ENOMEM);
1223 }
1224
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001225 of_id = of_match_device(of_ti_bandgap_match, &pdev->dev);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001226 if (of_id)
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001227 bgp->conf = of_id->data;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001228
Eduardo Valentin9879b2c2013-03-19 10:54:23 -04001229 /* register shadow for context save and restore */
1230 bgp->regval = devm_kzalloc(&pdev->dev, sizeof(*bgp->regval) *
1231 bgp->conf->sensor_count, GFP_KERNEL);
Rickard Strandqvistfbe2ddc2014-06-02 23:25:30 +02001232 if (!bgp->regval) {
Eduardo Valentin9879b2c2013-03-19 10:54:23 -04001233 dev_err(&pdev->dev, "Unable to allocate mem for driver ref\n");
1234 return ERR_PTR(-ENOMEM);
1235 }
1236
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001237 i = 0;
1238 do {
1239 void __iomem *chunk;
1240
1241 res = platform_get_resource(pdev, IORESOURCE_MEM, i);
1242 if (!res)
1243 break;
Thierry Reding97f4be602013-01-21 11:09:19 +01001244 chunk = devm_ioremap_resource(&pdev->dev, res);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001245 if (i == 0)
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001246 bgp->base = chunk;
Thierry Reding97f4be602013-01-21 11:09:19 +01001247 if (IS_ERR(chunk))
1248 return ERR_CAST(chunk);
Eduardo Valentin24796e12013-03-15 08:59:53 -04001249
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001250 i++;
1251 } while (res);
1252
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001253 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
Eduardo Valentin57d16172013-06-07 11:11:53 -04001254 bgp->tshut_gpio = of_get_gpio(node, 0);
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001255 if (!gpio_is_valid(bgp->tshut_gpio)) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001256 dev_err(&pdev->dev, "invalid gpio for tshut (%d)\n",
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001257 bgp->tshut_gpio);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001258 return ERR_PTR(-EINVAL);
1259 }
1260 }
1261
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001262 return bgp;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001263}
1264
Eduardo Valentinf91ddfe2013-03-15 09:00:23 -04001265/*** Device driver call backs ***/
1266
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001267static
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001268int ti_bandgap_probe(struct platform_device *pdev)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001269{
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001270 struct ti_bandgap *bgp;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001271 int clk_rate, ret = 0, i;
1272
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001273 bgp = ti_bandgap_build(pdev);
Eduardo Valentin0c12b5a2013-05-29 15:07:43 +00001274 if (IS_ERR(bgp)) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001275 dev_err(&pdev->dev, "failed to fetch platform data\n");
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001276 return PTR_ERR(bgp);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001277 }
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001278 bgp->dev = &pdev->dev;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001279
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001280 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
1281 ret = ti_bandgap_tshut_init(bgp, pdev);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001282 if (ret) {
1283 dev_err(&pdev->dev,
1284 "failed to initialize system tshut IRQ\n");
1285 return ret;
1286 }
1287 }
1288
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001289 bgp->fclock = clk_get(NULL, bgp->conf->fclock_name);
Eduardo Valentin0c12b5a2013-05-29 15:07:43 +00001290 ret = IS_ERR(bgp->fclock);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001291 if (ret) {
1292 dev_err(&pdev->dev, "failed to request fclock reference\n");
Eduardo Valentin0c12b5a2013-05-29 15:07:43 +00001293 ret = PTR_ERR(bgp->fclock);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001294 goto free_irqs;
1295 }
1296
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001297 bgp->div_clk = clk_get(NULL, bgp->conf->div_ck_name);
Eduardo Valentin0c12b5a2013-05-29 15:07:43 +00001298 ret = IS_ERR(bgp->div_clk);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001299 if (ret) {
1300 dev_err(&pdev->dev,
1301 "failed to request div_ts_ck clock ref\n");
Eduardo Valentin0c12b5a2013-05-29 15:07:43 +00001302 ret = PTR_ERR(bgp->div_clk);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001303 goto free_irqs;
1304 }
1305
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001306 for (i = 0; i < bgp->conf->sensor_count; i++) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001307 struct temp_sensor_registers *tsr;
1308 u32 val;
1309
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001310 tsr = bgp->conf->sensors[i].registers;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001311 /*
1312 * check if the efuse has a non-zero value if not
1313 * it is an untrimmed sample and the temperatures
1314 * may not be accurate
1315 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001316 val = ti_bandgap_readl(bgp, tsr->bgap_efuse);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001317 if (ret || !val)
1318 dev_info(&pdev->dev,
1319 "Non-trimmed BGAP, Temp not accurate\n");
1320 }
1321
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001322 clk_rate = clk_round_rate(bgp->div_clk,
1323 bgp->conf->sensors[0].ts_data->max_freq);
1324 if (clk_rate < bgp->conf->sensors[0].ts_data->min_freq ||
Paul Walmsleyc68789e2013-12-09 18:09:22 -08001325 clk_rate <= 0) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001326 ret = -ENODEV;
1327 dev_err(&pdev->dev, "wrong clock rate (%d)\n", clk_rate);
1328 goto put_clks;
1329 }
1330
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001331 ret = clk_set_rate(bgp->div_clk, clk_rate);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001332 if (ret)
1333 dev_err(&pdev->dev, "Cannot re-set clock rate. Continuing\n");
1334
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001335 bgp->clk_rate = clk_rate;
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001336 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001337 clk_prepare_enable(bgp->fclock);
Radhesh Fadnis6c9c1d62013-02-26 18:53:25 -04001338
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001339
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001340 spin_lock_init(&bgp->lock);
1341 bgp->dev = &pdev->dev;
1342 platform_set_drvdata(pdev, bgp);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001343
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001344 ti_bandgap_power(bgp, true);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001345
1346 /* Set default counter to 1 for now */
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001347 if (TI_BANDGAP_HAS(bgp, COUNTER))
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001348 for (i = 0; i < bgp->conf->sensor_count; i++)
1349 RMW_BITS(bgp, i, bgap_counter, counter_mask, 1);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001350
Eduardo Valentind3c291a2013-03-15 08:59:55 -04001351 /* Set default thresholds for alert and shutdown */
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001352 for (i = 0; i < bgp->conf->sensor_count; i++) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001353 struct temp_sensor_data *ts_data;
1354
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001355 ts_data = bgp->conf->sensors[i].ts_data;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001356
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001357 if (TI_BANDGAP_HAS(bgp, TALERT)) {
Eduardo Valentind3c291a2013-03-15 08:59:55 -04001358 /* Set initial Talert thresholds */
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001359 RMW_BITS(bgp, i, bgap_threshold,
Eduardo Valentind3c291a2013-03-15 08:59:55 -04001360 threshold_tcold_mask, ts_data->t_cold);
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001361 RMW_BITS(bgp, i, bgap_threshold,
Eduardo Valentind3c291a2013-03-15 08:59:55 -04001362 threshold_thot_mask, ts_data->t_hot);
1363 /* Enable the alert events */
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001364 RMW_BITS(bgp, i, bgap_mask_ctrl, mask_hot_mask, 1);
1365 RMW_BITS(bgp, i, bgap_mask_ctrl, mask_cold_mask, 1);
Eduardo Valentind3c291a2013-03-15 08:59:55 -04001366 }
1367
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001368 if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG)) {
Eduardo Valentind3c291a2013-03-15 08:59:55 -04001369 /* Set initial Tshut thresholds */
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001370 RMW_BITS(bgp, i, tshut_threshold,
Eduardo Valentind3c291a2013-03-15 08:59:55 -04001371 tshut_hot_mask, ts_data->tshut_hot);
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001372 RMW_BITS(bgp, i, tshut_threshold,
Eduardo Valentind3c291a2013-03-15 08:59:55 -04001373 tshut_cold_mask, ts_data->tshut_cold);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001374 }
1375 }
1376
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001377 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
1378 ti_bandgap_set_continuous_mode(bgp);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001379
1380 /* Set .250 seconds time as default counter */
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001381 if (TI_BANDGAP_HAS(bgp, COUNTER))
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001382 for (i = 0; i < bgp->conf->sensor_count; i++)
1383 RMW_BITS(bgp, i, bgap_counter, counter_mask,
1384 bgp->clk_rate / 4);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001385
1386 /* Every thing is good? Then expose the sensors */
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001387 for (i = 0; i < bgp->conf->sensor_count; i++) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001388 char *domain;
1389
Eduardo Valentinf1553332013-04-08 08:19:13 -04001390 if (bgp->conf->sensors[i].register_cooling) {
1391 ret = bgp->conf->sensors[i].register_cooling(bgp, i);
1392 if (ret)
1393 goto remove_sensors;
1394 }
Eduardo Valentin04a4d102012-09-11 19:06:55 +03001395
Eduardo Valentinf1553332013-04-08 08:19:13 -04001396 if (bgp->conf->expose_sensor) {
1397 domain = bgp->conf->sensors[i].domain;
1398 ret = bgp->conf->expose_sensor(bgp, i, domain);
1399 if (ret)
1400 goto remove_last_cooling;
1401 }
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001402 }
1403
1404 /*
1405 * Enable the Interrupts once everything is set. Otherwise irq handler
1406 * might be called as soon as it is enabled where as rest of framework
1407 * is still getting initialised.
1408 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001409 if (TI_BANDGAP_HAS(bgp, TALERT)) {
1410 ret = ti_bandgap_talert_init(bgp, pdev);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001411 if (ret) {
1412 dev_err(&pdev->dev, "failed to initialize Talert IRQ\n");
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001413 i = bgp->conf->sensor_count;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001414 goto disable_clk;
1415 }
1416 }
1417
1418 return 0;
1419
Eduardo Valentinf1553332013-04-08 08:19:13 -04001420remove_last_cooling:
1421 if (bgp->conf->sensors[i].unregister_cooling)
1422 bgp->conf->sensors[i].unregister_cooling(bgp, i);
1423remove_sensors:
1424 for (i--; i >= 0; i--) {
1425 if (bgp->conf->sensors[i].unregister_cooling)
1426 bgp->conf->sensors[i].unregister_cooling(bgp, i);
1427 if (bgp->conf->remove_sensor)
1428 bgp->conf->remove_sensor(bgp, i);
1429 }
1430 ti_bandgap_power(bgp, false);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001431disable_clk:
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001432 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001433 clk_disable_unprepare(bgp->fclock);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001434put_clks:
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001435 clk_put(bgp->fclock);
1436 clk_put(bgp->div_clk);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001437free_irqs:
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001438 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001439 free_irq(gpio_to_irq(bgp->tshut_gpio), NULL);
1440 gpio_free(bgp->tshut_gpio);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001441 }
1442
1443 return ret;
1444}
1445
1446static
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001447int ti_bandgap_remove(struct platform_device *pdev)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001448{
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001449 struct ti_bandgap *bgp = platform_get_drvdata(pdev);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001450 int i;
1451
1452 /* First thing is to remove sensor interfaces */
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001453 for (i = 0; i < bgp->conf->sensor_count; i++) {
Eduardo Valentin262235b2013-04-08 08:19:14 -04001454 if (bgp->conf->sensors[i].unregister_cooling)
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001455 bgp->conf->sensors[i].unregister_cooling(bgp, i);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001456
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001457 if (bgp->conf->remove_sensor)
1458 bgp->conf->remove_sensor(bgp, i);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001459 }
1460
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001461 ti_bandgap_power(bgp, false);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001462
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001463 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001464 clk_disable_unprepare(bgp->fclock);
1465 clk_put(bgp->fclock);
1466 clk_put(bgp->div_clk);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001467
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001468 if (TI_BANDGAP_HAS(bgp, TALERT))
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001469 free_irq(bgp->irq, bgp);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001470
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001471 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001472 free_irq(gpio_to_irq(bgp->tshut_gpio), NULL);
1473 gpio_free(bgp->tshut_gpio);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001474 }
1475
1476 return 0;
1477}
1478
Grygorii Strashko3992b622015-02-06 16:55:46 +02001479#ifdef CONFIG_PM_SLEEP
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001480static int ti_bandgap_save_ctxt(struct ti_bandgap *bgp)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001481{
1482 int i;
1483
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001484 for (i = 0; i < bgp->conf->sensor_count; i++) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001485 struct temp_sensor_registers *tsr;
1486 struct temp_sensor_regval *rval;
1487
Eduardo Valentin9879b2c2013-03-19 10:54:23 -04001488 rval = &bgp->regval[i];
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001489 tsr = bgp->conf->sensors[i].registers;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001490
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001491 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
1492 rval->bg_mode_ctrl = ti_bandgap_readl(bgp,
J Keerthy76d2cd32012-09-11 19:06:52 +03001493 tsr->bgap_mode_ctrl);
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001494 if (TI_BANDGAP_HAS(bgp, COUNTER))
1495 rval->bg_counter = ti_bandgap_readl(bgp,
J Keerthy76d2cd32012-09-11 19:06:52 +03001496 tsr->bgap_counter);
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001497 if (TI_BANDGAP_HAS(bgp, TALERT)) {
1498 rval->bg_threshold = ti_bandgap_readl(bgp,
J Keerthy76d2cd32012-09-11 19:06:52 +03001499 tsr->bgap_threshold);
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001500 rval->bg_ctrl = ti_bandgap_readl(bgp,
J Keerthy76d2cd32012-09-11 19:06:52 +03001501 tsr->bgap_mask_ctrl);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001502 }
1503
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001504 if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG))
1505 rval->tshut_threshold = ti_bandgap_readl(bgp,
J Keerthy76d2cd32012-09-11 19:06:52 +03001506 tsr->tshut_threshold);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001507 }
1508
1509 return 0;
1510}
1511
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001512static int ti_bandgap_restore_ctxt(struct ti_bandgap *bgp)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001513{
1514 int i;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001515
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001516 for (i = 0; i < bgp->conf->sensor_count; i++) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001517 struct temp_sensor_registers *tsr;
1518 struct temp_sensor_regval *rval;
1519 u32 val = 0;
1520
Eduardo Valentin9879b2c2013-03-19 10:54:23 -04001521 rval = &bgp->regval[i];
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001522 tsr = bgp->conf->sensors[i].registers;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001523
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001524 if (TI_BANDGAP_HAS(bgp, COUNTER))
1525 val = ti_bandgap_readl(bgp, tsr->bgap_counter);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001526
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001527 if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG))
1528 ti_bandgap_writel(bgp, rval->tshut_threshold,
1529 tsr->tshut_threshold);
Radhesh Fadnisb87ea752012-11-13 14:10:04 -04001530 /* Force immediate temperature measurement and update
1531 * of the DTEMP field
1532 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001533 ti_bandgap_force_single_read(bgp, i);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001534
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001535 if (TI_BANDGAP_HAS(bgp, COUNTER))
1536 ti_bandgap_writel(bgp, rval->bg_counter,
1537 tsr->bgap_counter);
1538 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
1539 ti_bandgap_writel(bgp, rval->bg_mode_ctrl,
1540 tsr->bgap_mode_ctrl);
1541 if (TI_BANDGAP_HAS(bgp, TALERT)) {
1542 ti_bandgap_writel(bgp, rval->bg_threshold,
1543 tsr->bgap_threshold);
1544 ti_bandgap_writel(bgp, rval->bg_ctrl,
1545 tsr->bgap_mask_ctrl);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001546 }
1547 }
1548
1549 return 0;
1550}
1551
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001552static int ti_bandgap_suspend(struct device *dev)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001553{
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001554 struct ti_bandgap *bgp = dev_get_drvdata(dev);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001555 int err;
1556
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001557 err = ti_bandgap_save_ctxt(bgp);
1558 ti_bandgap_power(bgp, false);
Radhesh Fadnis6c9c1d62013-02-26 18:53:25 -04001559
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001560 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001561 clk_disable_unprepare(bgp->fclock);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001562
1563 return err;
1564}
1565
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001566static int ti_bandgap_resume(struct device *dev)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001567{
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001568 struct ti_bandgap *bgp = dev_get_drvdata(dev);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001569
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001570 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001571 clk_prepare_enable(bgp->fclock);
Radhesh Fadnis6c9c1d62013-02-26 18:53:25 -04001572
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001573 ti_bandgap_power(bgp, true);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001574
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001575 return ti_bandgap_restore_ctxt(bgp);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001576}
Jingoo Han5204f8c2014-02-27 20:43:02 +09001577static SIMPLE_DEV_PM_OPS(ti_bandgap_dev_pm_ops, ti_bandgap_suspend,
1578 ti_bandgap_resume);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001579
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001580#define DEV_PM_OPS (&ti_bandgap_dev_pm_ops)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001581#else
1582#define DEV_PM_OPS NULL
1583#endif
1584
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001585static const struct of_device_id of_ti_bandgap_match[] = {
Eduardo Valentin1a312702012-07-12 19:02:31 +03001586#ifdef CONFIG_OMAP4_THERMAL
1587 {
1588 .compatible = "ti,omap4430-bandgap",
1589 .data = (void *)&omap4430_data,
1590 },
1591 {
1592 .compatible = "ti,omap4460-bandgap",
1593 .data = (void *)&omap4460_data,
1594 },
1595 {
1596 .compatible = "ti,omap4470-bandgap",
1597 .data = (void *)&omap4470_data,
1598 },
1599#endif
Eduardo Valentin949f5a52012-07-12 19:02:32 +03001600#ifdef CONFIG_OMAP5_THERMAL
1601 {
1602 .compatible = "ti,omap5430-bandgap",
1603 .data = (void *)&omap5430_data,
1604 },
1605#endif
Eduardo Valentin25870e62013-05-29 15:07:45 +00001606#ifdef CONFIG_DRA752_THERMAL
1607 {
1608 .compatible = "ti,dra752-bandgap",
1609 .data = (void *)&dra752_data,
1610 },
1611#endif
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001612 /* Sentinel */
1613 { },
1614};
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001615MODULE_DEVICE_TABLE(of, of_ti_bandgap_match);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001616
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001617static struct platform_driver ti_bandgap_sensor_driver = {
1618 .probe = ti_bandgap_probe,
1619 .remove = ti_bandgap_remove,
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001620 .driver = {
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001621 .name = "ti-soc-thermal",
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001622 .pm = DEV_PM_OPS,
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001623 .of_match_table = of_ti_bandgap_match,
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001624 },
1625};
1626
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001627module_platform_driver(ti_bandgap_sensor_driver);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001628
1629MODULE_DESCRIPTION("OMAP4+ bandgap temperature sensor driver");
1630MODULE_LICENSE("GPL v2");
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001631MODULE_ALIAS("platform:ti-soc-thermal");
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001632MODULE_AUTHOR("Texas Instrument Inc.");