blob: 10c47c048f7a3b0e4f5538c3698492fd5482c21b [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
Pavel Machek95d079e2015-03-24 23:20:21 +010046static int ti_bandgap_force_single_read(struct ti_bandgap *bgp, int id);
47
Eduardo Valentin8abbe712013-03-15 09:00:05 -040048/*** Helper functions to access registers and their bitfields ***/
49
Eduardo Valentin9c468aa2013-03-15 08:59:56 -040050/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -040051 * ti_bandgap_readl() - simple read helper function
52 * @bgp: pointer to ti_bandgap structure
Eduardo Valentin9c468aa2013-03-15 08:59:56 -040053 * @reg: desired register (offset) to be read
54 *
55 * Helper function to read bandgap registers. It uses the io remapped area.
Nishanth Menon169e8d02013-04-01 12:04:34 -040056 * Return: the register value.
Eduardo Valentin9c468aa2013-03-15 08:59:56 -040057 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -040058static u32 ti_bandgap_readl(struct ti_bandgap *bgp, u32 reg)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +030059{
Eduardo Valentind7f080e2013-03-19 10:54:18 -040060 return readl(bgp->base + reg);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +030061}
62
Eduardo Valentin9c468aa2013-03-15 08:59:56 -040063/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -040064 * ti_bandgap_writel() - simple write helper function
65 * @bgp: pointer to ti_bandgap structure
Eduardo Valentin9c468aa2013-03-15 08:59:56 -040066 * @val: desired register value to be written
67 * @reg: desired register (offset) to be written
68 *
69 * Helper function to write bandgap registers. It uses the io remapped area.
70 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -040071static void ti_bandgap_writel(struct ti_bandgap *bgp, u32 val, u32 reg)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +030072{
Eduardo Valentind7f080e2013-03-19 10:54:18 -040073 writel(val, bgp->base + reg);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +030074}
75
Eduardo Valentin9c468aa2013-03-15 08:59:56 -040076/**
77 * DOC: macro to update bits.
78 *
79 * RMW_BITS() - used to read, modify and update bandgap bitfields.
80 * The value passed will be shifted.
81 */
Eduardo Valentind7f080e2013-03-19 10:54:18 -040082#define RMW_BITS(bgp, id, reg, mask, val) \
Eduardo Valentind3c291a2013-03-15 08:59:55 -040083do { \
84 struct temp_sensor_registers *t; \
85 u32 r; \
86 \
Eduardo Valentind7f080e2013-03-19 10:54:18 -040087 t = bgp->conf->sensors[(id)].registers; \
Eduardo Valentin03e859d2013-03-19 10:54:21 -040088 r = ti_bandgap_readl(bgp, t->reg); \
Eduardo Valentind3c291a2013-03-15 08:59:55 -040089 r &= ~t->mask; \
90 r |= (val) << __ffs(t->mask); \
Eduardo Valentin03e859d2013-03-19 10:54:21 -040091 ti_bandgap_writel(bgp, r, t->reg); \
Eduardo Valentind3c291a2013-03-15 08:59:55 -040092} while (0)
93
Eduardo Valentin6ab52402013-03-15 09:00:06 -040094/*** Basic helper functions ***/
95
Eduardo Valentin7a556e62013-03-15 08:59:58 -040096/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -040097 * ti_bandgap_power() - controls the power state of a bandgap device
98 * @bgp: pointer to ti_bandgap structure
Eduardo Valentin7a556e62013-03-15 08:59:58 -040099 * @on: desired power state (1 - on, 0 - off)
100 *
101 * Used to power on/off a bandgap device instance. Only used on those
102 * that features tempsoff bit.
Nishanth Menon169e8d02013-04-01 12:04:34 -0400103 *
104 * Return: 0 on success, -ENOTSUPP if tempsoff is not supported.
Eduardo Valentin7a556e62013-03-15 08:59:58 -0400105 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400106static int ti_bandgap_power(struct ti_bandgap *bgp, bool on)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300107{
Pavel Macheke34238b2015-01-18 21:17:10 +0100108 int i;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300109
Pavel Macheke34238b2015-01-18 21:17:10 +0100110 if (!TI_BANDGAP_HAS(bgp, POWER_SWITCH))
111 return -ENOTSUPP;
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);
Pavel Macheke34238b2015-01-18 21:17:10 +0100116 return 0;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300117}
118
Eduardo Valentin4a6554e2013-03-15 08:59:59 -0400119/**
Keerthy79010632015-04-22 18:21:41 +0530120 * ti_errata814_bandgap_read_temp() - helper function to read dra7 sensor temperature
121 * @bgp: pointer to ti_bandgap structure
122 * @reg: desired register (offset) to be read
123 *
124 * Function to read dra7 bandgap sensor temperature. This is done separately
125 * so as to workaround the errata "Bandgap Temperature read Dtemp can be
126 * corrupted" - Errata ID: i814".
127 * Read accesses to registers listed below can be corrupted due to incorrect
128 * resynchronization between clock domains.
129 * Read access to registers below can be corrupted :
130 * CTRL_CORE_DTEMP_MPU/GPU/CORE/DSPEVE/IVA_n (n = 0 to 4)
131 * CTRL_CORE_TEMP_SENSOR_MPU/GPU/CORE/DSPEVE/IVA_n
132 *
133 * Return: the register value.
134 */
135static u32 ti_errata814_bandgap_read_temp(struct ti_bandgap *bgp, u32 reg)
136{
137 u32 val1, val2;
138
139 val1 = ti_bandgap_readl(bgp, reg);
140 val2 = ti_bandgap_readl(bgp, reg);
141
142 /* If both times we read the same value then that is right */
143 if (val1 == val2)
144 return val1;
145
146 /* if val1 and val2 are different read it third time */
147 return ti_bandgap_readl(bgp, reg);
148}
149
150/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400151 * ti_bandgap_read_temp() - helper function to read sensor temperature
152 * @bgp: pointer to ti_bandgap structure
Eduardo Valentin4a6554e2013-03-15 08:59:59 -0400153 * @id: bandgap sensor id
154 *
155 * Function to concentrate the steps to read sensor temperature register.
156 * This function is desired because, depending on bandgap device version,
157 * it might be needed to freeze the bandgap state machine, before fetching
158 * the register value.
Nishanth Menon169e8d02013-04-01 12:04:34 -0400159 *
160 * Return: temperature in ADC values.
Eduardo Valentin4a6554e2013-03-15 08:59:59 -0400161 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400162static u32 ti_bandgap_read_temp(struct ti_bandgap *bgp, int id)
Eduardo Valentin194a54f2013-02-26 18:53:33 -0400163{
164 struct temp_sensor_registers *tsr;
Eduardo Valentind3c291a2013-03-15 08:59:55 -0400165 u32 temp, reg;
Eduardo Valentin194a54f2013-02-26 18:53:33 -0400166
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400167 tsr = bgp->conf->sensors[id].registers;
Eduardo Valentin194a54f2013-02-26 18:53:33 -0400168 reg = tsr->temp_sensor_ctrl;
169
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400170 if (TI_BANDGAP_HAS(bgp, FREEZE_BIT)) {
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400171 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 1);
Eduardo Valentin194a54f2013-02-26 18:53:33 -0400172 /*
173 * In case we cannot read from cur_dtemp / dtemp_0,
174 * then we read from the last valid temp read
175 */
176 reg = tsr->ctrl_dtemp_1;
177 }
178
179 /* read temperature */
Keerthy79010632015-04-22 18:21:41 +0530180 if (TI_BANDGAP_HAS(bgp, ERRATA_814))
181 temp = ti_errata814_bandgap_read_temp(bgp, reg);
182 else
183 temp = ti_bandgap_readl(bgp, reg);
184
Eduardo Valentin194a54f2013-02-26 18:53:33 -0400185 temp &= tsr->bgap_dtemp_mask;
186
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400187 if (TI_BANDGAP_HAS(bgp, FREEZE_BIT))
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400188 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 0);
Eduardo Valentin194a54f2013-02-26 18:53:33 -0400189
190 return temp;
191}
192
Eduardo Valentinfb65b882013-03-15 09:00:07 -0400193/*** IRQ handlers ***/
194
Eduardo Valentinee07d552013-03-15 09:00:01 -0400195/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400196 * ti_bandgap_talert_irq_handler() - handles Temperature alert IRQs
Eduardo Valentinee07d552013-03-15 09:00:01 -0400197 * @irq: IRQ number
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400198 * @data: private data (struct ti_bandgap *)
Eduardo Valentinee07d552013-03-15 09:00:01 -0400199 *
200 * This is the Talert handler. Use it only if bandgap device features
201 * HAS(TALERT). This handler goes over all sensors and checks their
202 * conditions and acts accordingly. In case there are events pending,
203 * it will reset the event mask to wait for the opposite event (next event).
204 * Every time there is a new event, it will be reported to thermal layer.
Nishanth Menon169e8d02013-04-01 12:04:34 -0400205 *
206 * Return: IRQ_HANDLED
Eduardo Valentinee07d552013-03-15 09:00:01 -0400207 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400208static irqreturn_t ti_bandgap_talert_irq_handler(int irq, void *data)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300209{
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400210 struct ti_bandgap *bgp = data;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300211 struct temp_sensor_registers *tsr;
Eduardo Valentin194a54f2013-02-26 18:53:33 -0400212 u32 t_hot = 0, t_cold = 0, ctrl;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300213 int i;
214
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400215 spin_lock(&bgp->lock);
216 for (i = 0; i < bgp->conf->sensor_count; i++) {
217 tsr = bgp->conf->sensors[i].registers;
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400218 ctrl = ti_bandgap_readl(bgp, tsr->bgap_status);
Eduardo Valentine555c952013-03-15 09:00:04 -0400219
220 /* Read the status of t_hot */
221 t_hot = ctrl & tsr->status_hot_mask;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300222
223 /* Read the status of t_cold */
Eduardo Valentine555c952013-03-15 09:00:04 -0400224 t_cold = ctrl & tsr->status_cold_mask;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300225
226 if (!t_cold && !t_hot)
227 continue;
228
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400229 ctrl = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300230 /*
231 * One TALERT interrupt: Two sources
232 * If the interrupt is due to t_hot then mask t_hot and
233 * and unmask t_cold else mask t_cold and unmask t_hot
234 */
235 if (t_hot) {
236 ctrl &= ~tsr->mask_hot_mask;
237 ctrl |= tsr->mask_cold_mask;
238 } else if (t_cold) {
239 ctrl &= ~tsr->mask_cold_mask;
240 ctrl |= tsr->mask_hot_mask;
241 }
242
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400243 ti_bandgap_writel(bgp, ctrl, tsr->bgap_mask_ctrl);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300244
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400245 dev_dbg(bgp->dev,
Eduardo Valentin71e303f2012-11-13 14:10:03 -0400246 "%s: IRQ from %s sensor: hotevent %d coldevent %d\n",
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400247 __func__, bgp->conf->sensors[i].domain,
Eduardo Valentin71e303f2012-11-13 14:10:03 -0400248 t_hot, t_cold);
249
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300250 /* report temperature to whom may concern */
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400251 if (bgp->conf->report_temperature)
252 bgp->conf->report_temperature(bgp, i);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300253 }
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400254 spin_unlock(&bgp->lock);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300255
256 return IRQ_HANDLED;
257}
258
Eduardo Valentin79857cd22013-03-15 09:00:02 -0400259/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400260 * ti_bandgap_tshut_irq_handler() - handles Temperature shutdown signal
Eduardo Valentin79857cd22013-03-15 09:00:02 -0400261 * @irq: IRQ number
262 * @data: private data (unused)
263 *
264 * This is the Tshut handler. Use it only if bandgap device features
265 * HAS(TSHUT). If any sensor fires the Tshut signal, we simply shutdown
266 * the system.
Nishanth Menon169e8d02013-04-01 12:04:34 -0400267 *
268 * Return: IRQ_HANDLED
Eduardo Valentin79857cd22013-03-15 09:00:02 -0400269 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400270static irqreturn_t ti_bandgap_tshut_irq_handler(int irq, void *data)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300271{
Ruslan Ruslichenkob3bf0e92013-02-26 18:53:24 -0400272 pr_emerg("%s: TSHUT temperature reached. Needs shut down...\n",
273 __func__);
274
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300275 orderly_poweroff(true);
276
277 return IRQ_HANDLED;
278}
279
Eduardo Valentin2f6af4b2013-03-15 09:00:08 -0400280/*** Helper functions which manipulate conversion ADC <-> mi Celsius ***/
281
Eduardo Valentin2577e932013-03-15 09:00:11 -0400282/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400283 * ti_bandgap_adc_to_mcelsius() - converts an ADC value to mCelsius scale
284 * @bgp: struct ti_bandgap pointer
Eduardo Valentin2577e932013-03-15 09:00:11 -0400285 * @adc_val: value in ADC representation
286 * @t: address where to write the resulting temperature in mCelsius
287 *
288 * Simple conversion from ADC representation to mCelsius. In case the ADC value
289 * is out of the ADC conv table range, it returns -ERANGE, 0 on success.
290 * The conversion table is indexed by the ADC values.
Nishanth Menon169e8d02013-04-01 12:04:34 -0400291 *
292 * Return: 0 if conversion was successful, else -ERANGE in case the @adc_val
293 * argument is out of the ADC conv table range.
Eduardo Valentin2577e932013-03-15 09:00:11 -0400294 */
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300295static
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400296int ti_bandgap_adc_to_mcelsius(struct ti_bandgap *bgp, int adc_val, int *t)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300297{
Eduardo Valentin9879b2c2013-03-19 10:54:23 -0400298 const struct ti_bandgap_data *conf = bgp->conf;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300299
300 /* look up for temperature in the table and return the temperature */
Pavel Macheke34238b2015-01-18 21:17:10 +0100301 if (adc_val < conf->adc_start_val || adc_val > conf->adc_end_val)
302 return -ERANGE;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300303
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400304 *t = bgp->conf->conv_table[adc_val - conf->adc_start_val];
Pavel Macheke34238b2015-01-18 21:17:10 +0100305 return 0;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300306}
307
Eduardo Valentine7f60b52013-03-15 09:00:15 -0400308/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400309 * ti_bandgap_mcelsius_to_adc() - converts a mCelsius value to ADC scale
310 * @bgp: struct ti_bandgap pointer
Eduardo Valentine7f60b52013-03-15 09:00:15 -0400311 * @temp: value in mCelsius
312 * @adc: address where to write the resulting temperature in ADC representation
313 *
314 * Simple conversion from mCelsius to ADC values. In case the temp value
315 * is out of the ADC conv table range, it returns -ERANGE, 0 on success.
316 * The conversion table is indexed by the ADC values.
Nishanth Menon169e8d02013-04-01 12:04:34 -0400317 *
318 * Return: 0 if conversion was successful, else -ERANGE in case the @temp
319 * argument is out of the ADC conv table range.
Eduardo Valentine7f60b52013-03-15 09:00:15 -0400320 */
Eduardo Valentine16f0722013-03-15 09:00:12 -0400321static
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400322int ti_bandgap_mcelsius_to_adc(struct ti_bandgap *bgp, long temp, int *adc)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300323{
Eduardo Valentin9879b2c2013-03-19 10:54:23 -0400324 const struct ti_bandgap_data *conf = bgp->conf;
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400325 const int *conv_table = bgp->conf->conv_table;
Pavel Macheke34238b2015-01-18 21:17:10 +0100326 int high, low, mid;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300327
328 low = 0;
Eduardo Valentin26a70ed2013-03-15 09:00:14 -0400329 high = conf->adc_end_val - conf->adc_start_val;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300330 mid = (high + low) / 2;
331
Pavel Macheke34238b2015-01-18 21:17:10 +0100332 if (temp < conv_table[low] || temp > conv_table[high])
333 return -ERANGE;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300334
335 while (low < high) {
Eduardo Valentinc8a8f842013-02-26 18:53:36 -0400336 if (temp < conv_table[mid])
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300337 high = mid - 1;
338 else
339 low = mid + 1;
340 mid = (low + high) / 2;
341 }
342
Eduardo Valentin26a70ed2013-03-15 09:00:14 -0400343 *adc = conf->adc_start_val + low;
Pavel Macheke34238b2015-01-18 21:17:10 +0100344 return 0;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300345}
346
Eduardo Valentin8a1cefe2013-03-15 09:00:17 -0400347/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400348 * ti_bandgap_add_hyst() - add hysteresis (in mCelsius) to an ADC value
349 * @bgp: struct ti_bandgap pointer
Eduardo Valentin8a1cefe2013-03-15 09:00:17 -0400350 * @adc_val: temperature value in ADC representation
351 * @hyst_val: hysteresis value in mCelsius
352 * @sum: address where to write the resulting temperature (in ADC scale)
353 *
354 * Adds an hysteresis value (in mCelsius) to a ADC temperature value.
Nishanth Menon169e8d02013-04-01 12:04:34 -0400355 *
356 * Return: 0 on success, -ERANGE otherwise.
Eduardo Valentin8a1cefe2013-03-15 09:00:17 -0400357 */
Eduardo Valentin0f0ed7d2013-03-15 09:00:16 -0400358static
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400359int ti_bandgap_add_hyst(struct ti_bandgap *bgp, int adc_val, int hyst_val,
360 u32 *sum)
Eduardo Valentin0f0ed7d2013-03-15 09:00:16 -0400361{
362 int temp, ret;
363
364 /*
365 * Need to add in the mcelsius domain, so we have a temperature
366 * the conv_table range
367 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400368 ret = ti_bandgap_adc_to_mcelsius(bgp, adc_val, &temp);
Eduardo Valentin0f0ed7d2013-03-15 09:00:16 -0400369 if (ret < 0)
Pavel Macheke34238b2015-01-18 21:17:10 +0100370 return ret;
Eduardo Valentin0f0ed7d2013-03-15 09:00:16 -0400371
372 temp += hyst_val;
373
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400374 ret = ti_bandgap_mcelsius_to_adc(bgp, temp, sum);
Eduardo Valentin0f0ed7d2013-03-15 09:00:16 -0400375 return ret;
376}
377
Eduardo Valentinf8ccce22013-03-15 09:00:18 -0400378/*** Helper functions handling device Alert/Shutdown signals ***/
379
Eduardo Valentinf47f6d32013-03-15 09:00:20 -0400380/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400381 * ti_bandgap_unmask_interrupts() - unmasks the events of thot & tcold
382 * @bgp: struct ti_bandgap pointer
Eduardo Valentin61603af2013-03-19 10:54:25 -0400383 * @id: bandgap sensor id
Eduardo Valentinf47f6d32013-03-15 09:00:20 -0400384 * @t_hot: hot temperature value to trigger alert signal
385 * @t_cold: cold temperature value to trigger alert signal
386 *
387 * Checks the requested t_hot and t_cold values and configures the IRQ event
388 * masks accordingly. Call this function only if bandgap features HAS(TALERT).
389 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400390static void ti_bandgap_unmask_interrupts(struct ti_bandgap *bgp, int id,
391 u32 t_hot, u32 t_cold)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300392{
393 struct temp_sensor_registers *tsr;
394 u32 temp, reg_val;
395
396 /* Read the current on die temperature */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400397 temp = ti_bandgap_read_temp(bgp, id);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300398
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400399 tsr = bgp->conf->sensors[id].registers;
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400400 reg_val = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl);
Eduardo Valentin194a54f2013-02-26 18:53:33 -0400401
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300402 if (temp < t_hot)
403 reg_val |= tsr->mask_hot_mask;
404 else
405 reg_val &= ~tsr->mask_hot_mask;
406
407 if (t_cold < temp)
408 reg_val |= tsr->mask_cold_mask;
409 else
410 reg_val &= ~tsr->mask_cold_mask;
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400411 ti_bandgap_writel(bgp, reg_val, tsr->bgap_mask_ctrl);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300412}
413
Eduardo Valentin38d99e82013-03-15 09:00:27 -0400414/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400415 * ti_bandgap_update_alert_threshold() - sequence to update thresholds
416 * @bgp: struct ti_bandgap pointer
Eduardo Valentin38d99e82013-03-15 09:00:27 -0400417 * @id: bandgap sensor id
418 * @val: value (ADC) of a new threshold
419 * @hot: desired threshold to be updated. true if threshold hot, false if
420 * threshold cold
421 *
422 * It will program the required thresholds (hot and cold) for TALERT signal.
423 * This function can be used to update t_hot or t_cold, depending on @hot value.
424 * It checks the resulting t_hot and t_cold values, based on the new passed @val
425 * and configures the thresholds so that t_hot is always greater than t_cold.
426 * Call this function only if bandgap features HAS(TALERT).
Nishanth Menon169e8d02013-04-01 12:04:34 -0400427 *
428 * Return: 0 if no error, else corresponding error
Eduardo Valentin38d99e82013-03-15 09:00:27 -0400429 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400430static int ti_bandgap_update_alert_threshold(struct ti_bandgap *bgp, int id,
431 int val, bool hot)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300432{
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400433 struct temp_sensor_data *ts_data = bgp->conf->sensors[id].ts_data;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300434 struct temp_sensor_registers *tsr;
Keerthye9a90d02015-04-22 18:21:42 +0530435 u32 thresh_val, reg_val, t_hot, t_cold, ctrl;
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400436 int err = 0;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300437
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400438 tsr = bgp->conf->sensors[id].registers;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300439
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400440 /* obtain the current value */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400441 thresh_val = ti_bandgap_readl(bgp, tsr->bgap_threshold);
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400442 t_cold = (thresh_val & tsr->threshold_tcold_mask) >>
443 __ffs(tsr->threshold_tcold_mask);
444 t_hot = (thresh_val & tsr->threshold_thot_mask) >>
445 __ffs(tsr->threshold_thot_mask);
446 if (hot)
447 t_hot = val;
448 else
449 t_cold = val;
450
Eduardo Valentinf5d43b72013-03-19 10:54:26 -0400451 if (t_cold > t_hot) {
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400452 if (hot)
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400453 err = ti_bandgap_add_hyst(bgp, t_hot,
454 -ts_data->hyst_val,
455 &t_cold);
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400456 else
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400457 err = ti_bandgap_add_hyst(bgp, t_cold,
458 ts_data->hyst_val,
459 &t_hot);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300460 }
461
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400462 /* write the new threshold values */
Eduardo Valentin0fb3c242013-03-19 10:54:27 -0400463 reg_val = thresh_val &
464 ~(tsr->threshold_thot_mask | tsr->threshold_tcold_mask);
465 reg_val |= (t_hot << __ffs(tsr->threshold_thot_mask)) |
466 (t_cold << __ffs(tsr->threshold_tcold_mask));
Keerthye9a90d02015-04-22 18:21:42 +0530467
468 /**
469 * Errata i813:
470 * Spurious Thermal Alert: Talert can happen randomly while the device
471 * remains under the temperature limit defined for this event to trig.
472 * This spurious event is caused by a incorrect re-synchronization
473 * between clock domains. The comparison between configured threshold
474 * and current temperature value can happen while the value is
475 * transitioning (metastable), thus causing inappropriate event
476 * generation. No spurious event occurs as long as the threshold value
477 * stays unchanged. Spurious event can be generated while a thermal
478 * alert threshold is modified in
479 * CONTROL_BANDGAP_THRESHOLD_MPU/GPU/CORE/DSPEVE/IVA_n.
480 */
481
482 if (TI_BANDGAP_HAS(bgp, ERRATA_813)) {
483 /* Mask t_hot and t_cold events at the IP Level */
484 ctrl = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl);
485
486 if (hot)
487 ctrl &= ~tsr->mask_hot_mask;
488 else
489 ctrl &= ~tsr->mask_cold_mask;
490
491 ti_bandgap_writel(bgp, ctrl, tsr->bgap_mask_ctrl);
492 }
493
494 /* Write the threshold value */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400495 ti_bandgap_writel(bgp, reg_val, tsr->bgap_threshold);
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400496
Keerthye9a90d02015-04-22 18:21:42 +0530497 if (TI_BANDGAP_HAS(bgp, ERRATA_813)) {
498 /* Unmask t_hot and t_cold events at the IP Level */
499 ctrl = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl);
500 if (hot)
501 ctrl |= tsr->mask_hot_mask;
502 else
503 ctrl |= tsr->mask_cold_mask;
504
505 ti_bandgap_writel(bgp, ctrl, tsr->bgap_mask_ctrl);
506 }
507
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300508 if (err) {
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400509 dev_err(bgp->dev, "failed to reprogram thot threshold\n");
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400510 err = -EIO;
511 goto exit;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300512 }
513
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400514 ti_bandgap_unmask_interrupts(bgp, id, t_hot, t_cold);
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400515exit:
516 return err;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300517}
518
Eduardo Valentine72b7bb2013-03-15 09:00:38 -0400519/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400520 * ti_bandgap_validate() - helper to check the sanity of a struct ti_bandgap
521 * @bgp: struct ti_bandgap pointer
Eduardo Valentine72b7bb2013-03-15 09:00:38 -0400522 * @id: bandgap sensor id
523 *
524 * Checks if the bandgap pointer is valid and if the sensor id is also
525 * applicable.
Nishanth Menon169e8d02013-04-01 12:04:34 -0400526 *
527 * Return: 0 if no errors, -EINVAL for invalid @bgp pointer or -ERANGE if
528 * @id cannot index @bgp sensors.
Eduardo Valentine72b7bb2013-03-15 09:00:38 -0400529 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400530static inline int ti_bandgap_validate(struct ti_bandgap *bgp, int id)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300531{
Eduardo Valentin0c12b5a2013-05-29 15:07:43 +0000532 if (!bgp || IS_ERR(bgp)) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300533 pr_err("%s: invalid bandgap pointer\n", __func__);
Pavel Macheke34238b2015-01-18 21:17:10 +0100534 return -EINVAL;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300535 }
536
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400537 if ((id < 0) || (id >= bgp->conf->sensor_count)) {
538 dev_err(bgp->dev, "%s: sensor id out of range (%d)\n",
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300539 __func__, id);
Pavel Macheke34238b2015-01-18 21:17:10 +0100540 return -ERANGE;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300541 }
542
Pavel Macheke34238b2015-01-18 21:17:10 +0100543 return 0;
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400544}
545
Eduardo Valentin9efa93b2013-03-15 09:00:28 -0400546/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400547 * _ti_bandgap_write_threshold() - helper to update TALERT t_cold or t_hot
548 * @bgp: struct ti_bandgap pointer
Eduardo Valentin9efa93b2013-03-15 09:00:28 -0400549 * @id: bandgap sensor id
550 * @val: value (mCelsius) of a new threshold
551 * @hot: desired threshold to be updated. true if threshold hot, false if
552 * threshold cold
553 *
554 * It will update the required thresholds (hot and cold) for TALERT signal.
555 * This function can be used to update t_hot or t_cold, depending on @hot value.
556 * Validates the mCelsius range and update the requested threshold.
557 * Call this function only if bandgap features HAS(TALERT).
Nishanth Menon169e8d02013-04-01 12:04:34 -0400558 *
559 * Return: 0 if no error, else corresponding error value.
Eduardo Valentin9efa93b2013-03-15 09:00:28 -0400560 */
Eduardo Valentin2f8ec2a2013-03-19 10:54:22 -0400561static int _ti_bandgap_write_threshold(struct ti_bandgap *bgp, int id, int val,
562 bool hot)
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400563{
564 struct temp_sensor_data *ts_data;
565 struct temp_sensor_registers *tsr;
566 u32 adc_val;
567 int ret;
568
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400569 ret = ti_bandgap_validate(bgp, id);
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400570 if (ret)
Pavel Macheke34238b2015-01-18 21:17:10 +0100571 return ret;
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400572
Pavel Macheke34238b2015-01-18 21:17:10 +0100573 if (!TI_BANDGAP_HAS(bgp, TALERT))
574 return -ENOTSUPP;
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400575
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400576 ts_data = bgp->conf->sensors[id].ts_data;
577 tsr = bgp->conf->sensors[id].registers;
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400578 if (hot) {
579 if (val < ts_data->min_temp + ts_data->hyst_val)
580 ret = -EINVAL;
581 } else {
582 if (val > ts_data->max_temp + ts_data->hyst_val)
583 ret = -EINVAL;
584 }
585
586 if (ret)
Pavel Macheke34238b2015-01-18 21:17:10 +0100587 return ret;
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400588
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400589 ret = ti_bandgap_mcelsius_to_adc(bgp, val, &adc_val);
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400590 if (ret < 0)
Pavel Macheke34238b2015-01-18 21:17:10 +0100591 return ret;
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400592
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400593 spin_lock(&bgp->lock);
Eduardo Valentind52361c2013-03-19 10:54:28 -0400594 ret = ti_bandgap_update_alert_threshold(bgp, id, adc_val, hot);
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400595 spin_unlock(&bgp->lock);
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400596 return ret;
597}
598
Eduardo Valentin7a681a52013-03-15 09:00:29 -0400599/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400600 * _ti_bandgap_read_threshold() - helper to read TALERT t_cold or t_hot
601 * @bgp: struct ti_bandgap pointer
Eduardo Valentin7a681a52013-03-15 09:00:29 -0400602 * @id: bandgap sensor id
603 * @val: value (mCelsius) of a threshold
604 * @hot: desired threshold to be read. true if threshold hot, false if
605 * threshold cold
606 *
607 * It will fetch the required thresholds (hot and cold) for TALERT signal.
608 * This function can be used to read t_hot or t_cold, depending on @hot value.
609 * Call this function only if bandgap features HAS(TALERT).
Nishanth Menon169e8d02013-04-01 12:04:34 -0400610 *
611 * Return: 0 if no error, -ENOTSUPP if it has no TALERT support, or the
612 * corresponding error value if some operation fails.
Eduardo Valentin7a681a52013-03-15 09:00:29 -0400613 */
Eduardo Valentin2f8ec2a2013-03-19 10:54:22 -0400614static int _ti_bandgap_read_threshold(struct ti_bandgap *bgp, int id,
615 int *val, bool hot)
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400616{
617 struct temp_sensor_registers *tsr;
618 u32 temp, mask;
619 int ret = 0;
620
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400621 ret = ti_bandgap_validate(bgp, id);
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400622 if (ret)
623 goto exit;
624
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400625 if (!TI_BANDGAP_HAS(bgp, TALERT)) {
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400626 ret = -ENOTSUPP;
627 goto exit;
628 }
629
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400630 tsr = bgp->conf->sensors[id].registers;
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400631 if (hot)
632 mask = tsr->threshold_thot_mask;
633 else
634 mask = tsr->threshold_tcold_mask;
635
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400636 temp = ti_bandgap_readl(bgp, tsr->bgap_threshold);
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400637 temp = (temp & mask) >> __ffs(mask);
Pavel Macheke34238b2015-01-18 21:17:10 +0100638 ret = ti_bandgap_adc_to_mcelsius(bgp, temp, &temp);
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400639 if (ret) {
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400640 dev_err(bgp->dev, "failed to read thot\n");
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400641 ret = -EIO;
642 goto exit;
643 }
644
645 *val = temp;
646
647exit:
Eduardo Valentin648b4c62013-03-19 10:54:17 -0400648 return ret;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300649}
650
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400651/*** Exposed APIs ***/
652
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300653/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400654 * ti_bandgap_read_thot() - reads sensor current thot
Eduardo Valentin61603af2013-03-19 10:54:25 -0400655 * @bgp: pointer to bandgap instance
656 * @id: sensor id
657 * @thot: resulting current thot value
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300658 *
Nishanth Menon169e8d02013-04-01 12:04:34 -0400659 * Return: 0 on success or the proper error code
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300660 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400661int ti_bandgap_read_thot(struct ti_bandgap *bgp, int id, int *thot)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300662{
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400663 return _ti_bandgap_read_threshold(bgp, id, thot, true);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300664}
665
666/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400667 * ti_bandgap_write_thot() - sets sensor current thot
Eduardo Valentin61603af2013-03-19 10:54:25 -0400668 * @bgp: pointer to bandgap instance
669 * @id: sensor id
670 * @val: desired thot value
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300671 *
Nishanth Menon169e8d02013-04-01 12:04:34 -0400672 * Return: 0 on success or the proper error code
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300673 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400674int ti_bandgap_write_thot(struct ti_bandgap *bgp, int id, int val)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300675{
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400676 return _ti_bandgap_write_threshold(bgp, id, val, true);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300677}
678
679/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400680 * ti_bandgap_read_tcold() - reads sensor current tcold
Eduardo Valentin61603af2013-03-19 10:54:25 -0400681 * @bgp: pointer to bandgap instance
682 * @id: sensor id
683 * @tcold: resulting current tcold value
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300684 *
Nishanth Menon169e8d02013-04-01 12:04:34 -0400685 * Return: 0 on success or the proper error code
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300686 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400687int ti_bandgap_read_tcold(struct ti_bandgap *bgp, int id, int *tcold)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300688{
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400689 return _ti_bandgap_read_threshold(bgp, id, tcold, false);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300690}
691
692/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400693 * ti_bandgap_write_tcold() - sets the sensor tcold
Eduardo Valentin61603af2013-03-19 10:54:25 -0400694 * @bgp: pointer to bandgap instance
695 * @id: sensor id
696 * @val: desired tcold value
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300697 *
Nishanth Menon169e8d02013-04-01 12:04:34 -0400698 * Return: 0 on success or the proper error code
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300699 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400700int ti_bandgap_write_tcold(struct ti_bandgap *bgp, int id, int val)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300701{
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400702 return _ti_bandgap_write_threshold(bgp, id, val, false);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300703}
704
705/**
J Keerthy58bccd02013-04-01 12:04:42 -0400706 * ti_bandgap_read_counter() - read the sensor counter
707 * @bgp: pointer to bandgap instance
708 * @id: sensor id
709 * @interval: resulting update interval in miliseconds
710 */
711static void ti_bandgap_read_counter(struct ti_bandgap *bgp, int id,
712 int *interval)
713{
714 struct temp_sensor_registers *tsr;
715 int time;
716
717 tsr = bgp->conf->sensors[id].registers;
718 time = ti_bandgap_readl(bgp, tsr->bgap_counter);
719 time = (time & tsr->counter_mask) >>
720 __ffs(tsr->counter_mask);
721 time = time * 1000 / bgp->clk_rate;
722 *interval = time;
723}
724
725/**
726 * ti_bandgap_read_counter_delay() - read the sensor counter delay
727 * @bgp: pointer to bandgap instance
728 * @id: sensor id
729 * @interval: resulting update interval in miliseconds
730 */
731static void ti_bandgap_read_counter_delay(struct ti_bandgap *bgp, int id,
732 int *interval)
733{
734 struct temp_sensor_registers *tsr;
735 int reg_val;
736
737 tsr = bgp->conf->sensors[id].registers;
738
739 reg_val = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl);
740 reg_val = (reg_val & tsr->mask_counter_delay_mask) >>
741 __ffs(tsr->mask_counter_delay_mask);
742 switch (reg_val) {
743 case 0:
744 *interval = 0;
745 break;
746 case 1:
747 *interval = 1;
748 break;
749 case 2:
750 *interval = 10;
751 break;
752 case 3:
753 *interval = 100;
754 break;
755 case 4:
756 *interval = 250;
757 break;
758 case 5:
759 *interval = 500;
760 break;
761 default:
762 dev_warn(bgp->dev, "Wrong counter delay value read from register %X",
763 reg_val);
764 }
765}
766
767/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400768 * ti_bandgap_read_update_interval() - read the sensor update interval
Eduardo Valentin61603af2013-03-19 10:54:25 -0400769 * @bgp: pointer to bandgap instance
770 * @id: sensor id
771 * @interval: resulting update interval in miliseconds
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300772 *
Nishanth Menon169e8d02013-04-01 12:04:34 -0400773 * Return: 0 on success or the proper error code
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300774 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400775int ti_bandgap_read_update_interval(struct ti_bandgap *bgp, int id,
776 int *interval)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300777{
J Keerthy58bccd02013-04-01 12:04:42 -0400778 int ret = 0;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300779
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400780 ret = ti_bandgap_validate(bgp, id);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300781 if (ret)
J Keerthy58bccd02013-04-01 12:04:42 -0400782 goto exit;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300783
J Keerthy58bccd02013-04-01 12:04:42 -0400784 if (!TI_BANDGAP_HAS(bgp, COUNTER) &&
785 !TI_BANDGAP_HAS(bgp, COUNTER_DELAY)) {
786 ret = -ENOTSUPP;
787 goto exit;
788 }
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300789
J Keerthy58bccd02013-04-01 12:04:42 -0400790 if (TI_BANDGAP_HAS(bgp, COUNTER)) {
791 ti_bandgap_read_counter(bgp, id, interval);
792 goto exit;
793 }
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300794
J Keerthy58bccd02013-04-01 12:04:42 -0400795 ti_bandgap_read_counter_delay(bgp, id, interval);
796exit:
797 return ret;
798}
799
800/**
801 * ti_bandgap_write_counter_delay() - set the counter_delay
802 * @bgp: pointer to bandgap instance
803 * @id: sensor id
804 * @interval: desired update interval in miliseconds
805 *
806 * Return: 0 on success or the proper error code
807 */
808static int ti_bandgap_write_counter_delay(struct ti_bandgap *bgp, int id,
809 u32 interval)
810{
811 int rval;
812
813 switch (interval) {
814 case 0: /* Immediate conversion */
815 rval = 0x0;
816 break;
817 case 1: /* Conversion after ever 1ms */
818 rval = 0x1;
819 break;
820 case 10: /* Conversion after ever 10ms */
821 rval = 0x2;
822 break;
823 case 100: /* Conversion after ever 100ms */
824 rval = 0x3;
825 break;
826 case 250: /* Conversion after ever 250ms */
827 rval = 0x4;
828 break;
829 case 500: /* Conversion after ever 500ms */
830 rval = 0x5;
831 break;
832 default:
833 dev_warn(bgp->dev, "Delay %d ms is not supported\n", interval);
834 return -EINVAL;
835 }
836
837 spin_lock(&bgp->lock);
838 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_counter_delay_mask, rval);
839 spin_unlock(&bgp->lock);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300840
841 return 0;
842}
843
844/**
J Keerthy58bccd02013-04-01 12:04:42 -0400845 * ti_bandgap_write_counter() - set the bandgap sensor counter
846 * @bgp: pointer to bandgap instance
847 * @id: sensor id
848 * @interval: desired update interval in miliseconds
849 */
850static void ti_bandgap_write_counter(struct ti_bandgap *bgp, int id,
851 u32 interval)
852{
853 interval = interval * bgp->clk_rate / 1000;
854 spin_lock(&bgp->lock);
855 RMW_BITS(bgp, id, bgap_counter, counter_mask, interval);
856 spin_unlock(&bgp->lock);
857}
858
859/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400860 * ti_bandgap_write_update_interval() - set the update interval
Eduardo Valentin61603af2013-03-19 10:54:25 -0400861 * @bgp: pointer to bandgap instance
862 * @id: sensor id
863 * @interval: desired update interval in miliseconds
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300864 *
Nishanth Menon169e8d02013-04-01 12:04:34 -0400865 * Return: 0 on success or the proper error code
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300866 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400867int ti_bandgap_write_update_interval(struct ti_bandgap *bgp,
868 int id, u32 interval)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300869{
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400870 int ret = ti_bandgap_validate(bgp, id);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300871 if (ret)
J Keerthy58bccd02013-04-01 12:04:42 -0400872 goto exit;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300873
J Keerthy58bccd02013-04-01 12:04:42 -0400874 if (!TI_BANDGAP_HAS(bgp, COUNTER) &&
875 !TI_BANDGAP_HAS(bgp, COUNTER_DELAY)) {
876 ret = -ENOTSUPP;
877 goto exit;
878 }
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300879
J Keerthy58bccd02013-04-01 12:04:42 -0400880 if (TI_BANDGAP_HAS(bgp, COUNTER)) {
881 ti_bandgap_write_counter(bgp, id, interval);
882 goto exit;
883 }
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300884
J Keerthy58bccd02013-04-01 12:04:42 -0400885 ret = ti_bandgap_write_counter_delay(bgp, id, interval);
886exit:
887 return ret;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300888}
889
890/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400891 * ti_bandgap_read_temperature() - report current temperature
Eduardo Valentin61603af2013-03-19 10:54:25 -0400892 * @bgp: pointer to bandgap instance
893 * @id: sensor id
894 * @temperature: resulting temperature
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300895 *
Nishanth Menon169e8d02013-04-01 12:04:34 -0400896 * Return: 0 on success or the proper error code
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300897 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400898int ti_bandgap_read_temperature(struct ti_bandgap *bgp, int id,
899 int *temperature)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300900{
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300901 u32 temp;
902 int ret;
903
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400904 ret = ti_bandgap_validate(bgp, id);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300905 if (ret)
906 return ret;
907
Pavel Machek95d079e2015-03-24 23:20:21 +0100908 if (!TI_BANDGAP_HAS(bgp, MODE_CONFIG)) {
909 ret = ti_bandgap_force_single_read(bgp, id);
910 if (ret)
911 return ret;
912 }
913
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400914 spin_lock(&bgp->lock);
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400915 temp = ti_bandgap_read_temp(bgp, id);
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400916 spin_unlock(&bgp->lock);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300917
Pavel Macheke34238b2015-01-18 21:17:10 +0100918 ret = ti_bandgap_adc_to_mcelsius(bgp, temp, &temp);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300919 if (ret)
920 return -EIO;
921
922 *temperature = temp;
923
924 return 0;
925}
926
927/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400928 * ti_bandgap_set_sensor_data() - helper function to store thermal
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300929 * framework related data.
Eduardo Valentin61603af2013-03-19 10:54:25 -0400930 * @bgp: pointer to bandgap instance
931 * @id: sensor id
932 * @data: thermal framework related data to be stored
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300933 *
Nishanth Menon169e8d02013-04-01 12:04:34 -0400934 * Return: 0 on success or the proper error code
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300935 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400936int ti_bandgap_set_sensor_data(struct ti_bandgap *bgp, int id, void *data)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300937{
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400938 int ret = ti_bandgap_validate(bgp, id);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300939 if (ret)
940 return ret;
941
Eduardo Valentin9879b2c2013-03-19 10:54:23 -0400942 bgp->regval[id].data = data;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300943
944 return 0;
945}
946
947/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400948 * ti_bandgap_get_sensor_data() - helper function to get thermal
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300949 * framework related data.
Eduardo Valentin61603af2013-03-19 10:54:25 -0400950 * @bgp: pointer to bandgap instance
951 * @id: sensor id
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300952 *
Nishanth Menon169e8d02013-04-01 12:04:34 -0400953 * Return: data stored by set function with sensor id on success or NULL
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300954 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400955void *ti_bandgap_get_sensor_data(struct ti_bandgap *bgp, int id)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300956{
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400957 int ret = ti_bandgap_validate(bgp, id);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300958 if (ret)
959 return ERR_PTR(ret);
960
Eduardo Valentin9879b2c2013-03-19 10:54:23 -0400961 return bgp->regval[id].data;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300962}
963
Eduardo Valentine195aba2013-03-15 09:00:22 -0400964/*** Helper functions used during device initialization ***/
965
Eduardo Valentin31102a72013-03-15 09:00:26 -0400966/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400967 * ti_bandgap_force_single_read() - executes 1 single ADC conversion
968 * @bgp: pointer to struct ti_bandgap
Eduardo Valentin31102a72013-03-15 09:00:26 -0400969 * @id: sensor id which it is desired to read 1 temperature
970 *
971 * Used to initialize the conversion state machine and set it to a valid
972 * state. Called during device initialization and context restore events.
Nishanth Menon169e8d02013-04-01 12:04:34 -0400973 *
974 * Return: 0
Eduardo Valentin31102a72013-03-15 09:00:26 -0400975 */
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300976static int
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400977ti_bandgap_force_single_read(struct ti_bandgap *bgp, int id)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300978{
Pavel Macheka4296d12015-01-18 21:20:51 +0100979 u32 counter = 1000;
980 struct temp_sensor_registers *tsr;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300981
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300982 /* Select single conversion mode */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400983 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400984 RMW_BITS(bgp, id, bgap_mode_ctrl, mode_ctrl_mask, 0);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300985
986 /* Start of Conversion = 1 */
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400987 RMW_BITS(bgp, id, temp_sensor_ctrl, bgap_soc_mask, 1);
Eduardo Valentin194a54f2013-02-26 18:53:33 -0400988
Pavel Macheka4296d12015-01-18 21:20:51 +0100989 /* Wait for EOCZ going up */
990 tsr = bgp->conf->sensors[id].registers;
991
992 while (--counter) {
993 if (ti_bandgap_readl(bgp, tsr->temp_sensor_ctrl) &
994 tsr->bgap_eocz_mask)
995 break;
996 }
Eduardo Valentin194a54f2013-02-26 18:53:33 -0400997
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300998 /* Start of Conversion = 0 */
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400999 RMW_BITS(bgp, id, temp_sensor_ctrl, bgap_soc_mask, 0);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001000
Pavel Macheka4296d12015-01-18 21:20:51 +01001001 /* Wait for EOCZ going down */
1002 counter = 1000;
1003 while (--counter) {
1004 if (!(ti_bandgap_readl(bgp, tsr->temp_sensor_ctrl) &
1005 tsr->bgap_eocz_mask))
1006 break;
1007 }
1008
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001009 return 0;
1010}
1011
1012/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001013 * ti_bandgap_set_continous_mode() - One time enabling of continuous mode
1014 * @bgp: pointer to struct ti_bandgap
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001015 *
Eduardo Valentina84b6f42013-03-15 09:00:25 -04001016 * Call this function only if HAS(MODE_CONFIG) is set. As this driver may
1017 * be used for junction temperature monitoring, it is desirable that the
1018 * sensors are operational all the time, so that alerts are generated
1019 * properly.
Nishanth Menon169e8d02013-04-01 12:04:34 -04001020 *
1021 * Return: 0
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001022 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001023static int ti_bandgap_set_continuous_mode(struct ti_bandgap *bgp)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001024{
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001025 int i;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001026
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001027 for (i = 0; i < bgp->conf->sensor_count; i++) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001028 /* Perform a single read just before enabling continuous */
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001029 ti_bandgap_force_single_read(bgp, i);
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001030 RMW_BITS(bgp, i, bgap_mode_ctrl, mode_ctrl_mask, 1);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001031 }
1032
1033 return 0;
1034}
1035
Eduardo Valentind3790b32013-03-15 09:00:30 -04001036/**
J Keerthy2f440b02013-04-01 12:04:45 -04001037 * ti_bandgap_get_trend() - To fetch the temperature trend of a sensor
1038 * @bgp: pointer to struct ti_bandgap
1039 * @id: id of the individual sensor
1040 * @trend: Pointer to trend.
1041 *
1042 * This function needs to be called to fetch the temperature trend of a
1043 * Particular sensor. The function computes the difference in temperature
1044 * w.r.t time. For the bandgaps with built in history buffer the temperatures
1045 * are read from the buffer and for those without the Buffer -ENOTSUPP is
1046 * returned.
1047 *
1048 * Return: 0 if no error, else return corresponding error. If no
1049 * error then the trend value is passed on to trend parameter
1050 */
1051int ti_bandgap_get_trend(struct ti_bandgap *bgp, int id, int *trend)
1052{
1053 struct temp_sensor_registers *tsr;
1054 u32 temp1, temp2, reg1, reg2;
1055 int t1, t2, interval, ret = 0;
1056
1057 ret = ti_bandgap_validate(bgp, id);
1058 if (ret)
1059 goto exit;
1060
1061 if (!TI_BANDGAP_HAS(bgp, HISTORY_BUFFER) ||
1062 !TI_BANDGAP_HAS(bgp, FREEZE_BIT)) {
1063 ret = -ENOTSUPP;
1064 goto exit;
1065 }
1066
Eduardo Valentinba0049e2013-06-07 19:13:13 +00001067 spin_lock(&bgp->lock);
1068
J Keerthy2f440b02013-04-01 12:04:45 -04001069 tsr = bgp->conf->sensors[id].registers;
1070
1071 /* Freeze and read the last 2 valid readings */
Eduardo Valentinba0049e2013-06-07 19:13:13 +00001072 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 1);
J Keerthy2f440b02013-04-01 12:04:45 -04001073 reg1 = tsr->ctrl_dtemp_1;
1074 reg2 = tsr->ctrl_dtemp_2;
1075
1076 /* read temperature from history buffer */
1077 temp1 = ti_bandgap_readl(bgp, reg1);
1078 temp1 &= tsr->bgap_dtemp_mask;
1079
1080 temp2 = ti_bandgap_readl(bgp, reg2);
1081 temp2 &= tsr->bgap_dtemp_mask;
1082
1083 /* Convert from adc values to mCelsius temperature */
1084 ret = ti_bandgap_adc_to_mcelsius(bgp, temp1, &t1);
1085 if (ret)
Eduardo Valentinba0049e2013-06-07 19:13:13 +00001086 goto unfreeze;
J Keerthy2f440b02013-04-01 12:04:45 -04001087
1088 ret = ti_bandgap_adc_to_mcelsius(bgp, temp2, &t2);
1089 if (ret)
Eduardo Valentinba0049e2013-06-07 19:13:13 +00001090 goto unfreeze;
J Keerthy2f440b02013-04-01 12:04:45 -04001091
1092 /* Fetch the update interval */
1093 ret = ti_bandgap_read_update_interval(bgp, id, &interval);
Ranganath Krishnane838ff82013-08-23 11:08:23 -05001094 if (ret)
Eduardo Valentinba0049e2013-06-07 19:13:13 +00001095 goto unfreeze;
J Keerthy2f440b02013-04-01 12:04:45 -04001096
Ranganath Krishnane838ff82013-08-23 11:08:23 -05001097 /* Set the interval to 1 ms if bandgap counter delay is not set */
1098 if (interval == 0)
1099 interval = 1;
1100
J Keerthy2f440b02013-04-01 12:04:45 -04001101 *trend = (t1 - t2) / interval;
1102
1103 dev_dbg(bgp->dev, "The temperatures are t1 = %d and t2 = %d and trend =%d\n",
1104 t1, t2, *trend);
1105
Eduardo Valentinba0049e2013-06-07 19:13:13 +00001106unfreeze:
1107 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 0);
1108 spin_unlock(&bgp->lock);
J Keerthy2f440b02013-04-01 12:04:45 -04001109exit:
1110 return ret;
1111}
1112
1113/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001114 * ti_bandgap_tshut_init() - setup and initialize tshut handling
1115 * @bgp: pointer to struct ti_bandgap
Eduardo Valentind3790b32013-03-15 09:00:30 -04001116 * @pdev: pointer to device struct platform_device
1117 *
1118 * Call this function only in case the bandgap features HAS(TSHUT).
1119 * In this case, the driver needs to handle the TSHUT signal as an IRQ.
1120 * The IRQ is wired as a GPIO, and for this purpose, it is required
1121 * to specify which GPIO line is used. TSHUT IRQ is fired anytime
1122 * one of the bandgap sensors violates the TSHUT high/hot threshold.
1123 * And in that case, the system must go off.
Nishanth Menon169e8d02013-04-01 12:04:34 -04001124 *
1125 * Return: 0 if no error, else error status
Eduardo Valentind3790b32013-03-15 09:00:30 -04001126 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001127static int ti_bandgap_tshut_init(struct ti_bandgap *bgp,
1128 struct platform_device *pdev)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001129{
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001130 int gpio_nr = bgp->tshut_gpio;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001131 int status;
1132
1133 /* Request for gpio_86 line */
1134 status = gpio_request(gpio_nr, "tshut");
1135 if (status < 0) {
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001136 dev_err(bgp->dev, "Could not request for TSHUT GPIO:%i\n", 86);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001137 return status;
1138 }
1139 status = gpio_direction_input(gpio_nr);
1140 if (status) {
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001141 dev_err(bgp->dev, "Cannot set input TSHUT GPIO %d\n", gpio_nr);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001142 return status;
1143 }
1144
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001145 status = request_irq(gpio_to_irq(gpio_nr), ti_bandgap_tshut_irq_handler,
1146 IRQF_TRIGGER_RISING, "tshut", NULL);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001147 if (status) {
1148 gpio_free(gpio_nr);
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001149 dev_err(bgp->dev, "request irq failed for TSHUT");
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001150 }
1151
1152 return 0;
1153}
1154
Eduardo Valentin094b8ac2013-03-15 09:00:31 -04001155/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001156 * ti_bandgap_alert_init() - setup and initialize talert handling
1157 * @bgp: pointer to struct ti_bandgap
Eduardo Valentin094b8ac2013-03-15 09:00:31 -04001158 * @pdev: pointer to device struct platform_device
1159 *
1160 * Call this function only in case the bandgap features HAS(TALERT).
1161 * In this case, the driver needs to handle the TALERT signals as an IRQs.
1162 * TALERT is a normal IRQ and it is fired any time thresholds (hot or cold)
1163 * are violated. In these situation, the driver must reprogram the thresholds,
1164 * accordingly to specified policy.
Nishanth Menon169e8d02013-04-01 12:04:34 -04001165 *
1166 * Return: 0 if no error, else return corresponding error.
Eduardo Valentin094b8ac2013-03-15 09:00:31 -04001167 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001168static int ti_bandgap_talert_init(struct ti_bandgap *bgp,
1169 struct platform_device *pdev)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001170{
1171 int ret;
1172
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001173 bgp->irq = platform_get_irq(pdev, 0);
1174 if (bgp->irq < 0) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001175 dev_err(&pdev->dev, "get_irq failed\n");
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001176 return bgp->irq;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001177 }
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001178 ret = request_threaded_irq(bgp->irq, NULL,
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001179 ti_bandgap_talert_irq_handler,
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001180 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001181 "talert", bgp);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001182 if (ret) {
1183 dev_err(&pdev->dev, "Request threaded irq failed.\n");
1184 return ret;
1185 }
1186
1187 return 0;
1188}
1189
Eduardo Valentin61603af2013-03-19 10:54:25 -04001190static const struct of_device_id of_ti_bandgap_match[];
Eduardo Valentine9b6f8c2013-03-15 09:00:32 -04001191/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001192 * ti_bandgap_build() - parse DT and setup a struct ti_bandgap
Eduardo Valentine9b6f8c2013-03-15 09:00:32 -04001193 * @pdev: pointer to device struct platform_device
1194 *
1195 * Used to read the device tree properties accordingly to the bandgap
1196 * matching version. Based on bandgap version and its capabilities it
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001197 * will build a struct ti_bandgap out of the required DT entries.
Nishanth Menon169e8d02013-04-01 12:04:34 -04001198 *
1199 * Return: valid bandgap structure if successful, else returns ERR_PTR
1200 * return value must be verified with IS_ERR.
Eduardo Valentine9b6f8c2013-03-15 09:00:32 -04001201 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001202static struct ti_bandgap *ti_bandgap_build(struct platform_device *pdev)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001203{
1204 struct device_node *node = pdev->dev.of_node;
1205 const struct of_device_id *of_id;
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001206 struct ti_bandgap *bgp;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001207 struct resource *res;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001208 int i;
1209
1210 /* just for the sake */
1211 if (!node) {
1212 dev_err(&pdev->dev, "no platform information available\n");
1213 return ERR_PTR(-EINVAL);
1214 }
1215
Eduardo Valentinf6843562013-03-19 10:54:24 -04001216 bgp = devm_kzalloc(&pdev->dev, sizeof(*bgp), GFP_KERNEL);
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001217 if (!bgp) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001218 dev_err(&pdev->dev, "Unable to allocate mem for driver ref\n");
1219 return ERR_PTR(-ENOMEM);
1220 }
1221
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001222 of_id = of_match_device(of_ti_bandgap_match, &pdev->dev);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001223 if (of_id)
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001224 bgp->conf = of_id->data;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001225
Eduardo Valentin9879b2c2013-03-19 10:54:23 -04001226 /* register shadow for context save and restore */
1227 bgp->regval = devm_kzalloc(&pdev->dev, sizeof(*bgp->regval) *
1228 bgp->conf->sensor_count, GFP_KERNEL);
Rickard Strandqvistfbe2ddc2014-06-02 23:25:30 +02001229 if (!bgp->regval) {
Eduardo Valentin9879b2c2013-03-19 10:54:23 -04001230 dev_err(&pdev->dev, "Unable to allocate mem for driver ref\n");
1231 return ERR_PTR(-ENOMEM);
1232 }
1233
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001234 i = 0;
1235 do {
1236 void __iomem *chunk;
1237
1238 res = platform_get_resource(pdev, IORESOURCE_MEM, i);
1239 if (!res)
1240 break;
Thierry Reding97f4be602013-01-21 11:09:19 +01001241 chunk = devm_ioremap_resource(&pdev->dev, res);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001242 if (i == 0)
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001243 bgp->base = chunk;
Thierry Reding97f4be602013-01-21 11:09:19 +01001244 if (IS_ERR(chunk))
1245 return ERR_CAST(chunk);
Eduardo Valentin24796e12013-03-15 08:59:53 -04001246
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001247 i++;
1248 } while (res);
1249
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001250 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
Eduardo Valentin57d16172013-06-07 11:11:53 -04001251 bgp->tshut_gpio = of_get_gpio(node, 0);
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001252 if (!gpio_is_valid(bgp->tshut_gpio)) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001253 dev_err(&pdev->dev, "invalid gpio for tshut (%d)\n",
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001254 bgp->tshut_gpio);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001255 return ERR_PTR(-EINVAL);
1256 }
1257 }
1258
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001259 return bgp;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001260}
1261
Eduardo Valentinf91ddfe2013-03-15 09:00:23 -04001262/*** Device driver call backs ***/
1263
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001264static
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001265int ti_bandgap_probe(struct platform_device *pdev)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001266{
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001267 struct ti_bandgap *bgp;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001268 int clk_rate, ret = 0, i;
1269
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001270 bgp = ti_bandgap_build(pdev);
Eduardo Valentin0c12b5a2013-05-29 15:07:43 +00001271 if (IS_ERR(bgp)) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001272 dev_err(&pdev->dev, "failed to fetch platform data\n");
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001273 return PTR_ERR(bgp);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001274 }
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001275 bgp->dev = &pdev->dev;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001276
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001277 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
1278 ret = ti_bandgap_tshut_init(bgp, pdev);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001279 if (ret) {
1280 dev_err(&pdev->dev,
1281 "failed to initialize system tshut IRQ\n");
1282 return ret;
1283 }
1284 }
1285
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001286 bgp->fclock = clk_get(NULL, bgp->conf->fclock_name);
Eduardo Valentin0c12b5a2013-05-29 15:07:43 +00001287 ret = IS_ERR(bgp->fclock);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001288 if (ret) {
1289 dev_err(&pdev->dev, "failed to request fclock reference\n");
Eduardo Valentin0c12b5a2013-05-29 15:07:43 +00001290 ret = PTR_ERR(bgp->fclock);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001291 goto free_irqs;
1292 }
1293
Pavel Macheke34238b2015-01-18 21:17:10 +01001294 bgp->div_clk = clk_get(NULL, bgp->conf->div_ck_name);
Eduardo Valentin0c12b5a2013-05-29 15:07:43 +00001295 ret = IS_ERR(bgp->div_clk);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001296 if (ret) {
Pavel Macheke34238b2015-01-18 21:17:10 +01001297 dev_err(&pdev->dev, "failed to request div_ts_ck clock ref\n");
Eduardo Valentin0c12b5a2013-05-29 15:07:43 +00001298 ret = PTR_ERR(bgp->div_clk);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001299 goto free_irqs;
1300 }
1301
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001302 for (i = 0; i < bgp->conf->sensor_count; i++) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001303 struct temp_sensor_registers *tsr;
1304 u32 val;
1305
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001306 tsr = bgp->conf->sensors[i].registers;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001307 /*
1308 * check if the efuse has a non-zero value if not
1309 * it is an untrimmed sample and the temperatures
1310 * may not be accurate
1311 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001312 val = ti_bandgap_readl(bgp, tsr->bgap_efuse);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001313 if (ret || !val)
1314 dev_info(&pdev->dev,
1315 "Non-trimmed BGAP, Temp not accurate\n");
1316 }
1317
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001318 clk_rate = clk_round_rate(bgp->div_clk,
1319 bgp->conf->sensors[0].ts_data->max_freq);
1320 if (clk_rate < bgp->conf->sensors[0].ts_data->min_freq ||
Paul Walmsleyc68789e2013-12-09 18:09:22 -08001321 clk_rate <= 0) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001322 ret = -ENODEV;
1323 dev_err(&pdev->dev, "wrong clock rate (%d)\n", clk_rate);
1324 goto put_clks;
1325 }
1326
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001327 ret = clk_set_rate(bgp->div_clk, clk_rate);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001328 if (ret)
1329 dev_err(&pdev->dev, "Cannot re-set clock rate. Continuing\n");
1330
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001331 bgp->clk_rate = clk_rate;
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001332 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001333 clk_prepare_enable(bgp->fclock);
Radhesh Fadnis6c9c1d62013-02-26 18:53:25 -04001334
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001335
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001336 spin_lock_init(&bgp->lock);
1337 bgp->dev = &pdev->dev;
1338 platform_set_drvdata(pdev, bgp);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001339
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001340 ti_bandgap_power(bgp, true);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001341
1342 /* Set default counter to 1 for now */
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001343 if (TI_BANDGAP_HAS(bgp, COUNTER))
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001344 for (i = 0; i < bgp->conf->sensor_count; i++)
1345 RMW_BITS(bgp, i, bgap_counter, counter_mask, 1);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001346
Eduardo Valentind3c291a2013-03-15 08:59:55 -04001347 /* Set default thresholds for alert and shutdown */
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001348 for (i = 0; i < bgp->conf->sensor_count; i++) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001349 struct temp_sensor_data *ts_data;
1350
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001351 ts_data = bgp->conf->sensors[i].ts_data;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001352
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001353 if (TI_BANDGAP_HAS(bgp, TALERT)) {
Eduardo Valentind3c291a2013-03-15 08:59:55 -04001354 /* Set initial Talert thresholds */
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001355 RMW_BITS(bgp, i, bgap_threshold,
Eduardo Valentind3c291a2013-03-15 08:59:55 -04001356 threshold_tcold_mask, ts_data->t_cold);
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001357 RMW_BITS(bgp, i, bgap_threshold,
Eduardo Valentind3c291a2013-03-15 08:59:55 -04001358 threshold_thot_mask, ts_data->t_hot);
1359 /* Enable the alert events */
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001360 RMW_BITS(bgp, i, bgap_mask_ctrl, mask_hot_mask, 1);
1361 RMW_BITS(bgp, i, bgap_mask_ctrl, mask_cold_mask, 1);
Eduardo Valentind3c291a2013-03-15 08:59:55 -04001362 }
1363
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001364 if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG)) {
Eduardo Valentind3c291a2013-03-15 08:59:55 -04001365 /* Set initial Tshut thresholds */
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001366 RMW_BITS(bgp, i, tshut_threshold,
Eduardo Valentind3c291a2013-03-15 08:59:55 -04001367 tshut_hot_mask, ts_data->tshut_hot);
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001368 RMW_BITS(bgp, i, tshut_threshold,
Eduardo Valentind3c291a2013-03-15 08:59:55 -04001369 tshut_cold_mask, ts_data->tshut_cold);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001370 }
1371 }
1372
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001373 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
1374 ti_bandgap_set_continuous_mode(bgp);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001375
1376 /* Set .250 seconds time as default counter */
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001377 if (TI_BANDGAP_HAS(bgp, COUNTER))
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001378 for (i = 0; i < bgp->conf->sensor_count; i++)
1379 RMW_BITS(bgp, i, bgap_counter, counter_mask,
1380 bgp->clk_rate / 4);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001381
1382 /* Every thing is good? Then expose the sensors */
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001383 for (i = 0; i < bgp->conf->sensor_count; i++) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001384 char *domain;
1385
Eduardo Valentinf1553332013-04-08 08:19:13 -04001386 if (bgp->conf->sensors[i].register_cooling) {
1387 ret = bgp->conf->sensors[i].register_cooling(bgp, i);
1388 if (ret)
1389 goto remove_sensors;
1390 }
Eduardo Valentin04a4d102012-09-11 19:06:55 +03001391
Eduardo Valentinf1553332013-04-08 08:19:13 -04001392 if (bgp->conf->expose_sensor) {
1393 domain = bgp->conf->sensors[i].domain;
1394 ret = bgp->conf->expose_sensor(bgp, i, domain);
1395 if (ret)
1396 goto remove_last_cooling;
1397 }
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001398 }
1399
1400 /*
1401 * Enable the Interrupts once everything is set. Otherwise irq handler
1402 * might be called as soon as it is enabled where as rest of framework
1403 * is still getting initialised.
1404 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001405 if (TI_BANDGAP_HAS(bgp, TALERT)) {
1406 ret = ti_bandgap_talert_init(bgp, pdev);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001407 if (ret) {
1408 dev_err(&pdev->dev, "failed to initialize Talert IRQ\n");
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001409 i = bgp->conf->sensor_count;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001410 goto disable_clk;
1411 }
1412 }
1413
1414 return 0;
1415
Eduardo Valentinf1553332013-04-08 08:19:13 -04001416remove_last_cooling:
1417 if (bgp->conf->sensors[i].unregister_cooling)
1418 bgp->conf->sensors[i].unregister_cooling(bgp, i);
1419remove_sensors:
1420 for (i--; i >= 0; i--) {
1421 if (bgp->conf->sensors[i].unregister_cooling)
1422 bgp->conf->sensors[i].unregister_cooling(bgp, i);
1423 if (bgp->conf->remove_sensor)
1424 bgp->conf->remove_sensor(bgp, i);
1425 }
1426 ti_bandgap_power(bgp, false);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001427disable_clk:
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001428 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001429 clk_disable_unprepare(bgp->fclock);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001430put_clks:
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001431 clk_put(bgp->fclock);
1432 clk_put(bgp->div_clk);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001433free_irqs:
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001434 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001435 free_irq(gpio_to_irq(bgp->tshut_gpio), NULL);
1436 gpio_free(bgp->tshut_gpio);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001437 }
1438
1439 return ret;
1440}
1441
1442static
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001443int ti_bandgap_remove(struct platform_device *pdev)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001444{
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001445 struct ti_bandgap *bgp = platform_get_drvdata(pdev);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001446 int i;
1447
1448 /* First thing is to remove sensor interfaces */
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001449 for (i = 0; i < bgp->conf->sensor_count; i++) {
Eduardo Valentin262235b2013-04-08 08:19:14 -04001450 if (bgp->conf->sensors[i].unregister_cooling)
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001451 bgp->conf->sensors[i].unregister_cooling(bgp, i);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001452
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001453 if (bgp->conf->remove_sensor)
1454 bgp->conf->remove_sensor(bgp, i);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001455 }
1456
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001457 ti_bandgap_power(bgp, false);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001458
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001459 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001460 clk_disable_unprepare(bgp->fclock);
1461 clk_put(bgp->fclock);
1462 clk_put(bgp->div_clk);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001463
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001464 if (TI_BANDGAP_HAS(bgp, TALERT))
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001465 free_irq(bgp->irq, bgp);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001466
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001467 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001468 free_irq(gpio_to_irq(bgp->tshut_gpio), NULL);
1469 gpio_free(bgp->tshut_gpio);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001470 }
1471
1472 return 0;
1473}
1474
Grygorii Strashko3992b622015-02-06 16:55:46 +02001475#ifdef CONFIG_PM_SLEEP
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001476static int ti_bandgap_save_ctxt(struct ti_bandgap *bgp)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001477{
1478 int i;
1479
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001480 for (i = 0; i < bgp->conf->sensor_count; i++) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001481 struct temp_sensor_registers *tsr;
1482 struct temp_sensor_regval *rval;
1483
Eduardo Valentin9879b2c2013-03-19 10:54:23 -04001484 rval = &bgp->regval[i];
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001485 tsr = bgp->conf->sensors[i].registers;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001486
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001487 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
1488 rval->bg_mode_ctrl = ti_bandgap_readl(bgp,
J Keerthy76d2cd32012-09-11 19:06:52 +03001489 tsr->bgap_mode_ctrl);
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001490 if (TI_BANDGAP_HAS(bgp, COUNTER))
1491 rval->bg_counter = ti_bandgap_readl(bgp,
J Keerthy76d2cd32012-09-11 19:06:52 +03001492 tsr->bgap_counter);
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001493 if (TI_BANDGAP_HAS(bgp, TALERT)) {
1494 rval->bg_threshold = ti_bandgap_readl(bgp,
J Keerthy76d2cd32012-09-11 19:06:52 +03001495 tsr->bgap_threshold);
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001496 rval->bg_ctrl = ti_bandgap_readl(bgp,
J Keerthy76d2cd32012-09-11 19:06:52 +03001497 tsr->bgap_mask_ctrl);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001498 }
1499
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001500 if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG))
1501 rval->tshut_threshold = ti_bandgap_readl(bgp,
J Keerthy76d2cd32012-09-11 19:06:52 +03001502 tsr->tshut_threshold);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001503 }
1504
1505 return 0;
1506}
1507
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001508static int ti_bandgap_restore_ctxt(struct ti_bandgap *bgp)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001509{
1510 int i;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001511
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001512 for (i = 0; i < bgp->conf->sensor_count; i++) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001513 struct temp_sensor_registers *tsr;
1514 struct temp_sensor_regval *rval;
1515 u32 val = 0;
1516
Eduardo Valentin9879b2c2013-03-19 10:54:23 -04001517 rval = &bgp->regval[i];
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001518 tsr = bgp->conf->sensors[i].registers;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001519
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001520 if (TI_BANDGAP_HAS(bgp, COUNTER))
1521 val = ti_bandgap_readl(bgp, tsr->bgap_counter);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001522
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001523 if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG))
1524 ti_bandgap_writel(bgp, rval->tshut_threshold,
1525 tsr->tshut_threshold);
Radhesh Fadnisb87ea752012-11-13 14:10:04 -04001526 /* Force immediate temperature measurement and update
1527 * of the DTEMP field
1528 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001529 ti_bandgap_force_single_read(bgp, i);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001530
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001531 if (TI_BANDGAP_HAS(bgp, COUNTER))
1532 ti_bandgap_writel(bgp, rval->bg_counter,
1533 tsr->bgap_counter);
1534 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
1535 ti_bandgap_writel(bgp, rval->bg_mode_ctrl,
1536 tsr->bgap_mode_ctrl);
1537 if (TI_BANDGAP_HAS(bgp, TALERT)) {
1538 ti_bandgap_writel(bgp, rval->bg_threshold,
1539 tsr->bgap_threshold);
1540 ti_bandgap_writel(bgp, rval->bg_ctrl,
1541 tsr->bgap_mask_ctrl);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001542 }
1543 }
1544
1545 return 0;
1546}
1547
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001548static int ti_bandgap_suspend(struct device *dev)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001549{
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001550 struct ti_bandgap *bgp = dev_get_drvdata(dev);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001551 int err;
1552
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001553 err = ti_bandgap_save_ctxt(bgp);
1554 ti_bandgap_power(bgp, false);
Radhesh Fadnis6c9c1d62013-02-26 18:53:25 -04001555
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001556 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001557 clk_disable_unprepare(bgp->fclock);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001558
1559 return err;
1560}
1561
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001562static int ti_bandgap_resume(struct device *dev)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001563{
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001564 struct ti_bandgap *bgp = dev_get_drvdata(dev);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001565
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001566 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001567 clk_prepare_enable(bgp->fclock);
Radhesh Fadnis6c9c1d62013-02-26 18:53:25 -04001568
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001569 ti_bandgap_power(bgp, true);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001570
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001571 return ti_bandgap_restore_ctxt(bgp);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001572}
Jingoo Han5204f8c2014-02-27 20:43:02 +09001573static SIMPLE_DEV_PM_OPS(ti_bandgap_dev_pm_ops, ti_bandgap_suspend,
1574 ti_bandgap_resume);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001575
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001576#define DEV_PM_OPS (&ti_bandgap_dev_pm_ops)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001577#else
1578#define DEV_PM_OPS NULL
1579#endif
1580
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001581static const struct of_device_id of_ti_bandgap_match[] = {
Eduardo Valentin1a312702012-07-12 19:02:31 +03001582#ifdef CONFIG_OMAP4_THERMAL
1583 {
1584 .compatible = "ti,omap4430-bandgap",
1585 .data = (void *)&omap4430_data,
1586 },
1587 {
1588 .compatible = "ti,omap4460-bandgap",
1589 .data = (void *)&omap4460_data,
1590 },
1591 {
1592 .compatible = "ti,omap4470-bandgap",
1593 .data = (void *)&omap4470_data,
1594 },
1595#endif
Eduardo Valentin949f5a52012-07-12 19:02:32 +03001596#ifdef CONFIG_OMAP5_THERMAL
1597 {
1598 .compatible = "ti,omap5430-bandgap",
1599 .data = (void *)&omap5430_data,
1600 },
1601#endif
Eduardo Valentin25870e62013-05-29 15:07:45 +00001602#ifdef CONFIG_DRA752_THERMAL
1603 {
1604 .compatible = "ti,dra752-bandgap",
1605 .data = (void *)&dra752_data,
1606 },
1607#endif
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001608 /* Sentinel */
1609 { },
1610};
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001611MODULE_DEVICE_TABLE(of, of_ti_bandgap_match);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001612
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001613static struct platform_driver ti_bandgap_sensor_driver = {
1614 .probe = ti_bandgap_probe,
1615 .remove = ti_bandgap_remove,
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001616 .driver = {
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001617 .name = "ti-soc-thermal",
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001618 .pm = DEV_PM_OPS,
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001619 .of_match_table = of_ti_bandgap_match,
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001620 },
1621};
1622
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001623module_platform_driver(ti_bandgap_sensor_driver);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001624
1625MODULE_DESCRIPTION("OMAP4+ bandgap temperature sensor driver");
1626MODULE_LICENSE("GPL v2");
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001627MODULE_ALIAS("platform:ti-soc-thermal");
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001628MODULE_AUTHOR("Texas Instrument Inc.");