blob: 097328d8e943c4438207f8b50809d09ee11207ef [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 Macheke34238bf2015-01-18 21:17:10 +0100108 int i;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300109
Pavel Macheke34238bf2015-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 Macheke34238bf2015-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 Macheke34238bf2015-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 Macheke34238bf2015-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_validate() - helper to check the sanity of a struct ti_bandgap
310 * @bgp: struct ti_bandgap pointer
Eduardo Valentine72b7bb2013-03-15 09:00:38 -0400311 * @id: bandgap sensor id
312 *
313 * Checks if the bandgap pointer is valid and if the sensor id is also
314 * applicable.
Nishanth Menon169e8d02013-04-01 12:04:34 -0400315 *
316 * Return: 0 if no errors, -EINVAL for invalid @bgp pointer or -ERANGE if
317 * @id cannot index @bgp sensors.
Eduardo Valentine72b7bb2013-03-15 09:00:38 -0400318 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400319static inline int ti_bandgap_validate(struct ti_bandgap *bgp, int id)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300320{
Eduardo Valentin0c12b5a2013-05-29 15:07:43 +0000321 if (!bgp || IS_ERR(bgp)) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300322 pr_err("%s: invalid bandgap pointer\n", __func__);
Pavel Macheke34238bf2015-01-18 21:17:10 +0100323 return -EINVAL;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300324 }
325
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400326 if ((id < 0) || (id >= bgp->conf->sensor_count)) {
327 dev_err(bgp->dev, "%s: sensor id out of range (%d)\n",
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300328 __func__, id);
Pavel Macheke34238bf2015-01-18 21:17:10 +0100329 return -ERANGE;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300330 }
331
Pavel Macheke34238bf2015-01-18 21:17:10 +0100332 return 0;
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400333}
334
Eduardo Valentin9efa93b2013-03-15 09:00:28 -0400335/**
J Keerthy58bccd02013-04-01 12:04:42 -0400336 * ti_bandgap_read_counter() - read the sensor counter
337 * @bgp: pointer to bandgap instance
338 * @id: sensor id
339 * @interval: resulting update interval in miliseconds
340 */
341static void ti_bandgap_read_counter(struct ti_bandgap *bgp, int id,
342 int *interval)
343{
344 struct temp_sensor_registers *tsr;
345 int time;
346
347 tsr = bgp->conf->sensors[id].registers;
348 time = ti_bandgap_readl(bgp, tsr->bgap_counter);
349 time = (time & tsr->counter_mask) >>
350 __ffs(tsr->counter_mask);
351 time = time * 1000 / bgp->clk_rate;
352 *interval = time;
353}
354
355/**
356 * ti_bandgap_read_counter_delay() - read the sensor counter delay
357 * @bgp: pointer to bandgap instance
358 * @id: sensor id
359 * @interval: resulting update interval in miliseconds
360 */
361static void ti_bandgap_read_counter_delay(struct ti_bandgap *bgp, int id,
362 int *interval)
363{
364 struct temp_sensor_registers *tsr;
365 int reg_val;
366
367 tsr = bgp->conf->sensors[id].registers;
368
369 reg_val = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl);
370 reg_val = (reg_val & tsr->mask_counter_delay_mask) >>
371 __ffs(tsr->mask_counter_delay_mask);
372 switch (reg_val) {
373 case 0:
374 *interval = 0;
375 break;
376 case 1:
377 *interval = 1;
378 break;
379 case 2:
380 *interval = 10;
381 break;
382 case 3:
383 *interval = 100;
384 break;
385 case 4:
386 *interval = 250;
387 break;
388 case 5:
389 *interval = 500;
390 break;
391 default:
392 dev_warn(bgp->dev, "Wrong counter delay value read from register %X",
393 reg_val);
394 }
395}
396
397/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400398 * ti_bandgap_read_update_interval() - read the sensor update interval
Eduardo Valentin61603af2013-03-19 10:54:25 -0400399 * @bgp: pointer to bandgap instance
400 * @id: sensor id
401 * @interval: resulting update interval in miliseconds
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300402 *
Nishanth Menon169e8d02013-04-01 12:04:34 -0400403 * Return: 0 on success or the proper error code
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300404 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400405int ti_bandgap_read_update_interval(struct ti_bandgap *bgp, int id,
406 int *interval)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300407{
J Keerthy58bccd02013-04-01 12:04:42 -0400408 int ret = 0;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300409
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400410 ret = ti_bandgap_validate(bgp, id);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300411 if (ret)
J Keerthy58bccd02013-04-01 12:04:42 -0400412 goto exit;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300413
J Keerthy58bccd02013-04-01 12:04:42 -0400414 if (!TI_BANDGAP_HAS(bgp, COUNTER) &&
415 !TI_BANDGAP_HAS(bgp, COUNTER_DELAY)) {
416 ret = -ENOTSUPP;
417 goto exit;
418 }
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300419
J Keerthy58bccd02013-04-01 12:04:42 -0400420 if (TI_BANDGAP_HAS(bgp, COUNTER)) {
421 ti_bandgap_read_counter(bgp, id, interval);
422 goto exit;
423 }
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300424
J Keerthy58bccd02013-04-01 12:04:42 -0400425 ti_bandgap_read_counter_delay(bgp, id, interval);
426exit:
427 return ret;
428}
429
430/**
431 * ti_bandgap_write_counter_delay() - set the counter_delay
432 * @bgp: pointer to bandgap instance
433 * @id: sensor id
434 * @interval: desired update interval in miliseconds
435 *
436 * Return: 0 on success or the proper error code
437 */
438static int ti_bandgap_write_counter_delay(struct ti_bandgap *bgp, int id,
439 u32 interval)
440{
441 int rval;
442
443 switch (interval) {
444 case 0: /* Immediate conversion */
445 rval = 0x0;
446 break;
447 case 1: /* Conversion after ever 1ms */
448 rval = 0x1;
449 break;
450 case 10: /* Conversion after ever 10ms */
451 rval = 0x2;
452 break;
453 case 100: /* Conversion after ever 100ms */
454 rval = 0x3;
455 break;
456 case 250: /* Conversion after ever 250ms */
457 rval = 0x4;
458 break;
459 case 500: /* Conversion after ever 500ms */
460 rval = 0x5;
461 break;
462 default:
463 dev_warn(bgp->dev, "Delay %d ms is not supported\n", interval);
464 return -EINVAL;
465 }
466
467 spin_lock(&bgp->lock);
468 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_counter_delay_mask, rval);
469 spin_unlock(&bgp->lock);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300470
471 return 0;
472}
473
474/**
J Keerthy58bccd02013-04-01 12:04:42 -0400475 * ti_bandgap_write_counter() - set the bandgap sensor counter
476 * @bgp: pointer to bandgap instance
477 * @id: sensor id
478 * @interval: desired update interval in miliseconds
479 */
480static void ti_bandgap_write_counter(struct ti_bandgap *bgp, int id,
481 u32 interval)
482{
483 interval = interval * bgp->clk_rate / 1000;
484 spin_lock(&bgp->lock);
485 RMW_BITS(bgp, id, bgap_counter, counter_mask, interval);
486 spin_unlock(&bgp->lock);
487}
488
489/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400490 * ti_bandgap_write_update_interval() - set the update interval
Eduardo Valentin61603af2013-03-19 10:54:25 -0400491 * @bgp: pointer to bandgap instance
492 * @id: sensor id
493 * @interval: desired update interval in miliseconds
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300494 *
Nishanth Menon169e8d02013-04-01 12:04:34 -0400495 * Return: 0 on success or the proper error code
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300496 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400497int ti_bandgap_write_update_interval(struct ti_bandgap *bgp,
498 int id, u32 interval)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300499{
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400500 int ret = ti_bandgap_validate(bgp, id);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300501 if (ret)
J Keerthy58bccd02013-04-01 12:04:42 -0400502 goto exit;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300503
J Keerthy58bccd02013-04-01 12:04:42 -0400504 if (!TI_BANDGAP_HAS(bgp, COUNTER) &&
505 !TI_BANDGAP_HAS(bgp, COUNTER_DELAY)) {
506 ret = -ENOTSUPP;
507 goto exit;
508 }
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300509
J Keerthy58bccd02013-04-01 12:04:42 -0400510 if (TI_BANDGAP_HAS(bgp, COUNTER)) {
511 ti_bandgap_write_counter(bgp, id, interval);
512 goto exit;
513 }
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300514
J Keerthy58bccd02013-04-01 12:04:42 -0400515 ret = ti_bandgap_write_counter_delay(bgp, id, interval);
516exit:
517 return ret;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300518}
519
520/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400521 * ti_bandgap_read_temperature() - report current temperature
Eduardo Valentin61603af2013-03-19 10:54:25 -0400522 * @bgp: pointer to bandgap instance
523 * @id: sensor id
524 * @temperature: resulting temperature
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300525 *
Nishanth Menon169e8d02013-04-01 12:04:34 -0400526 * Return: 0 on success or the proper error code
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300527 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400528int ti_bandgap_read_temperature(struct ti_bandgap *bgp, int id,
529 int *temperature)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300530{
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300531 u32 temp;
532 int ret;
533
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400534 ret = ti_bandgap_validate(bgp, id);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300535 if (ret)
536 return ret;
537
Pavel Machek95d079e2015-03-24 23:20:21 +0100538 if (!TI_BANDGAP_HAS(bgp, MODE_CONFIG)) {
539 ret = ti_bandgap_force_single_read(bgp, id);
540 if (ret)
541 return ret;
542 }
543
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400544 spin_lock(&bgp->lock);
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400545 temp = ti_bandgap_read_temp(bgp, id);
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400546 spin_unlock(&bgp->lock);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300547
Pavel Macheke34238bf2015-01-18 21:17:10 +0100548 ret = ti_bandgap_adc_to_mcelsius(bgp, temp, &temp);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300549 if (ret)
550 return -EIO;
551
552 *temperature = temp;
553
554 return 0;
555}
556
557/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400558 * ti_bandgap_set_sensor_data() - helper function to store thermal
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300559 * framework related data.
Eduardo Valentin61603af2013-03-19 10:54:25 -0400560 * @bgp: pointer to bandgap instance
561 * @id: sensor id
562 * @data: thermal framework related data to be stored
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300563 *
Nishanth Menon169e8d02013-04-01 12:04:34 -0400564 * Return: 0 on success or the proper error code
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300565 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400566int ti_bandgap_set_sensor_data(struct ti_bandgap *bgp, int id, void *data)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300567{
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400568 int ret = ti_bandgap_validate(bgp, id);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300569 if (ret)
570 return ret;
571
Eduardo Valentin9879b2c2013-03-19 10:54:23 -0400572 bgp->regval[id].data = data;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300573
574 return 0;
575}
576
577/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400578 * ti_bandgap_get_sensor_data() - helper function to get thermal
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300579 * framework related data.
Eduardo Valentin61603af2013-03-19 10:54:25 -0400580 * @bgp: pointer to bandgap instance
581 * @id: sensor id
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300582 *
Nishanth Menon169e8d02013-04-01 12:04:34 -0400583 * Return: data stored by set function with sensor id on success or NULL
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300584 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400585void *ti_bandgap_get_sensor_data(struct ti_bandgap *bgp, int id)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300586{
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400587 int ret = ti_bandgap_validate(bgp, id);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300588 if (ret)
589 return ERR_PTR(ret);
590
Eduardo Valentin9879b2c2013-03-19 10:54:23 -0400591 return bgp->regval[id].data;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300592}
593
Eduardo Valentine195aba2013-03-15 09:00:22 -0400594/*** Helper functions used during device initialization ***/
595
Eduardo Valentin31102a72013-03-15 09:00:26 -0400596/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400597 * ti_bandgap_force_single_read() - executes 1 single ADC conversion
598 * @bgp: pointer to struct ti_bandgap
Eduardo Valentin31102a72013-03-15 09:00:26 -0400599 * @id: sensor id which it is desired to read 1 temperature
600 *
601 * Used to initialize the conversion state machine and set it to a valid
602 * state. Called during device initialization and context restore events.
Nishanth Menon169e8d02013-04-01 12:04:34 -0400603 *
604 * Return: 0
Eduardo Valentin31102a72013-03-15 09:00:26 -0400605 */
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300606static int
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400607ti_bandgap_force_single_read(struct ti_bandgap *bgp, int id)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300608{
Pavel Macheka4296d12015-01-18 21:20:51 +0100609 u32 counter = 1000;
610 struct temp_sensor_registers *tsr;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300611
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300612 /* Select single conversion mode */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400613 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400614 RMW_BITS(bgp, id, bgap_mode_ctrl, mode_ctrl_mask, 0);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300615
616 /* Start of Conversion = 1 */
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400617 RMW_BITS(bgp, id, temp_sensor_ctrl, bgap_soc_mask, 1);
Eduardo Valentin194a54f2013-02-26 18:53:33 -0400618
Pavel Macheka4296d12015-01-18 21:20:51 +0100619 /* Wait for EOCZ going up */
620 tsr = bgp->conf->sensors[id].registers;
621
622 while (--counter) {
623 if (ti_bandgap_readl(bgp, tsr->temp_sensor_ctrl) &
624 tsr->bgap_eocz_mask)
625 break;
626 }
Eduardo Valentin194a54f2013-02-26 18:53:33 -0400627
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300628 /* Start of Conversion = 0 */
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400629 RMW_BITS(bgp, id, temp_sensor_ctrl, bgap_soc_mask, 0);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300630
Pavel Macheka4296d12015-01-18 21:20:51 +0100631 /* Wait for EOCZ going down */
632 counter = 1000;
633 while (--counter) {
634 if (!(ti_bandgap_readl(bgp, tsr->temp_sensor_ctrl) &
635 tsr->bgap_eocz_mask))
636 break;
637 }
638
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300639 return 0;
640}
641
642/**
Markus Elfring8b8656d62017-04-26 17:11:28 +0200643 * ti_bandgap_set_continuous_mode() - One time enabling of continuous mode
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400644 * @bgp: pointer to struct ti_bandgap
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300645 *
Eduardo Valentina84b6f42013-03-15 09:00:25 -0400646 * Call this function only if HAS(MODE_CONFIG) is set. As this driver may
647 * be used for junction temperature monitoring, it is desirable that the
648 * sensors are operational all the time, so that alerts are generated
649 * properly.
Nishanth Menon169e8d02013-04-01 12:04:34 -0400650 *
651 * Return: 0
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300652 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400653static int ti_bandgap_set_continuous_mode(struct ti_bandgap *bgp)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300654{
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300655 int i;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300656
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400657 for (i = 0; i < bgp->conf->sensor_count; i++) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300658 /* Perform a single read just before enabling continuous */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400659 ti_bandgap_force_single_read(bgp, i);
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400660 RMW_BITS(bgp, i, bgap_mode_ctrl, mode_ctrl_mask, 1);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300661 }
662
663 return 0;
664}
665
Eduardo Valentind3790b32013-03-15 09:00:30 -0400666/**
J Keerthy2f440b02013-04-01 12:04:45 -0400667 * ti_bandgap_get_trend() - To fetch the temperature trend of a sensor
668 * @bgp: pointer to struct ti_bandgap
669 * @id: id of the individual sensor
670 * @trend: Pointer to trend.
671 *
672 * This function needs to be called to fetch the temperature trend of a
673 * Particular sensor. The function computes the difference in temperature
674 * w.r.t time. For the bandgaps with built in history buffer the temperatures
675 * are read from the buffer and for those without the Buffer -ENOTSUPP is
676 * returned.
677 *
678 * Return: 0 if no error, else return corresponding error. If no
679 * error then the trend value is passed on to trend parameter
680 */
681int ti_bandgap_get_trend(struct ti_bandgap *bgp, int id, int *trend)
682{
683 struct temp_sensor_registers *tsr;
684 u32 temp1, temp2, reg1, reg2;
685 int t1, t2, interval, ret = 0;
686
687 ret = ti_bandgap_validate(bgp, id);
688 if (ret)
689 goto exit;
690
691 if (!TI_BANDGAP_HAS(bgp, HISTORY_BUFFER) ||
692 !TI_BANDGAP_HAS(bgp, FREEZE_BIT)) {
693 ret = -ENOTSUPP;
694 goto exit;
695 }
696
Eduardo Valentinba0049e2013-06-07 19:13:13 +0000697 spin_lock(&bgp->lock);
698
J Keerthy2f440b02013-04-01 12:04:45 -0400699 tsr = bgp->conf->sensors[id].registers;
700
701 /* Freeze and read the last 2 valid readings */
Eduardo Valentinba0049e2013-06-07 19:13:13 +0000702 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 1);
J Keerthy2f440b02013-04-01 12:04:45 -0400703 reg1 = tsr->ctrl_dtemp_1;
704 reg2 = tsr->ctrl_dtemp_2;
705
706 /* read temperature from history buffer */
707 temp1 = ti_bandgap_readl(bgp, reg1);
708 temp1 &= tsr->bgap_dtemp_mask;
709
710 temp2 = ti_bandgap_readl(bgp, reg2);
711 temp2 &= tsr->bgap_dtemp_mask;
712
713 /* Convert from adc values to mCelsius temperature */
714 ret = ti_bandgap_adc_to_mcelsius(bgp, temp1, &t1);
715 if (ret)
Eduardo Valentinba0049e2013-06-07 19:13:13 +0000716 goto unfreeze;
J Keerthy2f440b02013-04-01 12:04:45 -0400717
718 ret = ti_bandgap_adc_to_mcelsius(bgp, temp2, &t2);
719 if (ret)
Eduardo Valentinba0049e2013-06-07 19:13:13 +0000720 goto unfreeze;
J Keerthy2f440b02013-04-01 12:04:45 -0400721
722 /* Fetch the update interval */
723 ret = ti_bandgap_read_update_interval(bgp, id, &interval);
Ranganath Krishnane838ff82013-08-23 11:08:23 -0500724 if (ret)
Eduardo Valentinba0049e2013-06-07 19:13:13 +0000725 goto unfreeze;
J Keerthy2f440b02013-04-01 12:04:45 -0400726
Ranganath Krishnane838ff82013-08-23 11:08:23 -0500727 /* Set the interval to 1 ms if bandgap counter delay is not set */
728 if (interval == 0)
729 interval = 1;
730
J Keerthy2f440b02013-04-01 12:04:45 -0400731 *trend = (t1 - t2) / interval;
732
733 dev_dbg(bgp->dev, "The temperatures are t1 = %d and t2 = %d and trend =%d\n",
734 t1, t2, *trend);
735
Eduardo Valentinba0049e2013-06-07 19:13:13 +0000736unfreeze:
737 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 0);
738 spin_unlock(&bgp->lock);
J Keerthy2f440b02013-04-01 12:04:45 -0400739exit:
740 return ret;
741}
742
743/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400744 * ti_bandgap_tshut_init() - setup and initialize tshut handling
745 * @bgp: pointer to struct ti_bandgap
Eduardo Valentind3790b32013-03-15 09:00:30 -0400746 * @pdev: pointer to device struct platform_device
747 *
748 * Call this function only in case the bandgap features HAS(TSHUT).
749 * In this case, the driver needs to handle the TSHUT signal as an IRQ.
750 * The IRQ is wired as a GPIO, and for this purpose, it is required
751 * to specify which GPIO line is used. TSHUT IRQ is fired anytime
752 * one of the bandgap sensors violates the TSHUT high/hot threshold.
753 * And in that case, the system must go off.
Nishanth Menon169e8d02013-04-01 12:04:34 -0400754 *
755 * Return: 0 if no error, else error status
Eduardo Valentind3790b32013-03-15 09:00:30 -0400756 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400757static int ti_bandgap_tshut_init(struct ti_bandgap *bgp,
758 struct platform_device *pdev)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300759{
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400760 int gpio_nr = bgp->tshut_gpio;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300761 int status;
762
763 /* Request for gpio_86 line */
764 status = gpio_request(gpio_nr, "tshut");
765 if (status < 0) {
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400766 dev_err(bgp->dev, "Could not request for TSHUT GPIO:%i\n", 86);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300767 return status;
768 }
769 status = gpio_direction_input(gpio_nr);
770 if (status) {
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400771 dev_err(bgp->dev, "Cannot set input TSHUT GPIO %d\n", gpio_nr);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300772 return status;
773 }
774
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400775 status = request_irq(gpio_to_irq(gpio_nr), ti_bandgap_tshut_irq_handler,
776 IRQF_TRIGGER_RISING, "tshut", NULL);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300777 if (status) {
778 gpio_free(gpio_nr);
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400779 dev_err(bgp->dev, "request irq failed for TSHUT");
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300780 }
781
782 return 0;
783}
784
Eduardo Valentin094b8ac2013-03-15 09:00:31 -0400785/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400786 * ti_bandgap_alert_init() - setup and initialize talert handling
787 * @bgp: pointer to struct ti_bandgap
Eduardo Valentin094b8ac2013-03-15 09:00:31 -0400788 * @pdev: pointer to device struct platform_device
789 *
790 * Call this function only in case the bandgap features HAS(TALERT).
791 * In this case, the driver needs to handle the TALERT signals as an IRQs.
792 * TALERT is a normal IRQ and it is fired any time thresholds (hot or cold)
793 * are violated. In these situation, the driver must reprogram the thresholds,
794 * accordingly to specified policy.
Nishanth Menon169e8d02013-04-01 12:04:34 -0400795 *
796 * Return: 0 if no error, else return corresponding error.
Eduardo Valentin094b8ac2013-03-15 09:00:31 -0400797 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400798static int ti_bandgap_talert_init(struct ti_bandgap *bgp,
799 struct platform_device *pdev)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300800{
801 int ret;
802
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400803 bgp->irq = platform_get_irq(pdev, 0);
804 if (bgp->irq < 0) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300805 dev_err(&pdev->dev, "get_irq failed\n");
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400806 return bgp->irq;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300807 }
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400808 ret = request_threaded_irq(bgp->irq, NULL,
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400809 ti_bandgap_talert_irq_handler,
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300810 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400811 "talert", bgp);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300812 if (ret) {
813 dev_err(&pdev->dev, "Request threaded irq failed.\n");
814 return ret;
815 }
816
817 return 0;
818}
819
Eduardo Valentin61603af2013-03-19 10:54:25 -0400820static const struct of_device_id of_ti_bandgap_match[];
Eduardo Valentine9b6f8c2013-03-15 09:00:32 -0400821/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400822 * ti_bandgap_build() - parse DT and setup a struct ti_bandgap
Eduardo Valentine9b6f8c2013-03-15 09:00:32 -0400823 * @pdev: pointer to device struct platform_device
824 *
825 * Used to read the device tree properties accordingly to the bandgap
826 * matching version. Based on bandgap version and its capabilities it
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400827 * will build a struct ti_bandgap out of the required DT entries.
Nishanth Menon169e8d02013-04-01 12:04:34 -0400828 *
829 * Return: valid bandgap structure if successful, else returns ERR_PTR
830 * return value must be verified with IS_ERR.
Eduardo Valentine9b6f8c2013-03-15 09:00:32 -0400831 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400832static struct ti_bandgap *ti_bandgap_build(struct platform_device *pdev)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300833{
834 struct device_node *node = pdev->dev.of_node;
835 const struct of_device_id *of_id;
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400836 struct ti_bandgap *bgp;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300837 struct resource *res;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300838 int i;
839
840 /* just for the sake */
841 if (!node) {
842 dev_err(&pdev->dev, "no platform information available\n");
843 return ERR_PTR(-EINVAL);
844 }
845
Eduardo Valentinf6843562013-03-19 10:54:24 -0400846 bgp = devm_kzalloc(&pdev->dev, sizeof(*bgp), GFP_KERNEL);
Markus Elfring57e52112017-04-26 17:03:07 +0200847 if (!bgp)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300848 return ERR_PTR(-ENOMEM);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300849
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400850 of_id = of_match_device(of_ti_bandgap_match, &pdev->dev);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300851 if (of_id)
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400852 bgp->conf = of_id->data;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300853
Eduardo Valentin9879b2c2013-03-19 10:54:23 -0400854 /* register shadow for context save and restore */
Markus Elfring748c23d2017-04-26 16:45:25 +0200855 bgp->regval = devm_kcalloc(&pdev->dev, bgp->conf->sensor_count,
856 sizeof(*bgp->regval), GFP_KERNEL);
Markus Elfring57e52112017-04-26 17:03:07 +0200857 if (!bgp->regval)
Eduardo Valentin9879b2c2013-03-19 10:54:23 -0400858 return ERR_PTR(-ENOMEM);
Eduardo Valentin9879b2c2013-03-19 10:54:23 -0400859
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300860 i = 0;
861 do {
862 void __iomem *chunk;
863
864 res = platform_get_resource(pdev, IORESOURCE_MEM, i);
865 if (!res)
866 break;
Thierry Reding97f4be602013-01-21 11:09:19 +0100867 chunk = devm_ioremap_resource(&pdev->dev, res);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300868 if (i == 0)
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400869 bgp->base = chunk;
Thierry Reding97f4be602013-01-21 11:09:19 +0100870 if (IS_ERR(chunk))
871 return ERR_CAST(chunk);
Eduardo Valentin24796e12013-03-15 08:59:53 -0400872
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300873 i++;
874 } while (res);
875
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400876 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
Eduardo Valentin57d16172013-06-07 11:11:53 -0400877 bgp->tshut_gpio = of_get_gpio(node, 0);
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400878 if (!gpio_is_valid(bgp->tshut_gpio)) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300879 dev_err(&pdev->dev, "invalid gpio for tshut (%d)\n",
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400880 bgp->tshut_gpio);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300881 return ERR_PTR(-EINVAL);
882 }
883 }
884
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400885 return bgp;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300886}
887
Eduardo Valentinf91ddfe2013-03-15 09:00:23 -0400888/*** Device driver call backs ***/
889
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300890static
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400891int ti_bandgap_probe(struct platform_device *pdev)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300892{
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400893 struct ti_bandgap *bgp;
Dan Carpenter13369192016-03-02 13:00:55 +0300894 int clk_rate, ret, i;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300895
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400896 bgp = ti_bandgap_build(pdev);
Eduardo Valentin0c12b5a2013-05-29 15:07:43 +0000897 if (IS_ERR(bgp)) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300898 dev_err(&pdev->dev, "failed to fetch platform data\n");
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400899 return PTR_ERR(bgp);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300900 }
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400901 bgp->dev = &pdev->dev;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300902
Pavel Machek9c5c87e2015-04-02 16:49:07 +0200903 if (TI_BANDGAP_HAS(bgp, UNRELIABLE))
904 dev_warn(&pdev->dev,
905 "This OMAP thermal sensor is unreliable. You've been warned\n");
906
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400907 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
908 ret = ti_bandgap_tshut_init(bgp, pdev);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300909 if (ret) {
910 dev_err(&pdev->dev,
911 "failed to initialize system tshut IRQ\n");
912 return ret;
913 }
914 }
915
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400916 bgp->fclock = clk_get(NULL, bgp->conf->fclock_name);
Dan Carpenter13369192016-03-02 13:00:55 +0300917 if (IS_ERR(bgp->fclock)) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300918 dev_err(&pdev->dev, "failed to request fclock reference\n");
Eduardo Valentin0c12b5a2013-05-29 15:07:43 +0000919 ret = PTR_ERR(bgp->fclock);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300920 goto free_irqs;
921 }
922
Pavel Macheke34238bf2015-01-18 21:17:10 +0100923 bgp->div_clk = clk_get(NULL, bgp->conf->div_ck_name);
Dan Carpenter13369192016-03-02 13:00:55 +0300924 if (IS_ERR(bgp->div_clk)) {
Pavel Macheke34238bf2015-01-18 21:17:10 +0100925 dev_err(&pdev->dev, "failed to request div_ts_ck clock ref\n");
Eduardo Valentin0c12b5a2013-05-29 15:07:43 +0000926 ret = PTR_ERR(bgp->div_clk);
Luis Henriques882f5812016-11-16 22:15:22 +0000927 goto put_fclock;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300928 }
929
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400930 for (i = 0; i < bgp->conf->sensor_count; i++) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300931 struct temp_sensor_registers *tsr;
932 u32 val;
933
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400934 tsr = bgp->conf->sensors[i].registers;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300935 /*
936 * check if the efuse has a non-zero value if not
937 * it is an untrimmed sample and the temperatures
938 * may not be accurate
939 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400940 val = ti_bandgap_readl(bgp, tsr->bgap_efuse);
Dan Carpenter13369192016-03-02 13:00:55 +0300941 if (!val)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300942 dev_info(&pdev->dev,
943 "Non-trimmed BGAP, Temp not accurate\n");
944 }
945
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400946 clk_rate = clk_round_rate(bgp->div_clk,
947 bgp->conf->sensors[0].ts_data->max_freq);
948 if (clk_rate < bgp->conf->sensors[0].ts_data->min_freq ||
Paul Walmsleyc68789e2013-12-09 18:09:22 -0800949 clk_rate <= 0) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300950 ret = -ENODEV;
951 dev_err(&pdev->dev, "wrong clock rate (%d)\n", clk_rate);
952 goto put_clks;
953 }
954
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400955 ret = clk_set_rate(bgp->div_clk, clk_rate);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300956 if (ret)
957 dev_err(&pdev->dev, "Cannot re-set clock rate. Continuing\n");
958
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400959 bgp->clk_rate = clk_rate;
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400960 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400961 clk_prepare_enable(bgp->fclock);
Radhesh Fadnis6c9c1d62013-02-26 18:53:25 -0400962
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300963
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400964 spin_lock_init(&bgp->lock);
965 bgp->dev = &pdev->dev;
966 platform_set_drvdata(pdev, bgp);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300967
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400968 ti_bandgap_power(bgp, true);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300969
970 /* Set default counter to 1 for now */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400971 if (TI_BANDGAP_HAS(bgp, COUNTER))
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400972 for (i = 0; i < bgp->conf->sensor_count; i++)
973 RMW_BITS(bgp, i, bgap_counter, counter_mask, 1);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300974
Eduardo Valentind3c291a2013-03-15 08:59:55 -0400975 /* Set default thresholds for alert and shutdown */
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400976 for (i = 0; i < bgp->conf->sensor_count; i++) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300977 struct temp_sensor_data *ts_data;
978
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400979 ts_data = bgp->conf->sensors[i].ts_data;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300980
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400981 if (TI_BANDGAP_HAS(bgp, TALERT)) {
Eduardo Valentind3c291a2013-03-15 08:59:55 -0400982 /* Set initial Talert thresholds */
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400983 RMW_BITS(bgp, i, bgap_threshold,
Eduardo Valentind3c291a2013-03-15 08:59:55 -0400984 threshold_tcold_mask, ts_data->t_cold);
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400985 RMW_BITS(bgp, i, bgap_threshold,
Eduardo Valentind3c291a2013-03-15 08:59:55 -0400986 threshold_thot_mask, ts_data->t_hot);
987 /* Enable the alert events */
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400988 RMW_BITS(bgp, i, bgap_mask_ctrl, mask_hot_mask, 1);
989 RMW_BITS(bgp, i, bgap_mask_ctrl, mask_cold_mask, 1);
Eduardo Valentind3c291a2013-03-15 08:59:55 -0400990 }
991
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400992 if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG)) {
Eduardo Valentind3c291a2013-03-15 08:59:55 -0400993 /* Set initial Tshut thresholds */
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400994 RMW_BITS(bgp, i, tshut_threshold,
Eduardo Valentind3c291a2013-03-15 08:59:55 -0400995 tshut_hot_mask, ts_data->tshut_hot);
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400996 RMW_BITS(bgp, i, tshut_threshold,
Eduardo Valentind3c291a2013-03-15 08:59:55 -0400997 tshut_cold_mask, ts_data->tshut_cold);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300998 }
999 }
1000
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001001 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
1002 ti_bandgap_set_continuous_mode(bgp);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001003
1004 /* Set .250 seconds time as default counter */
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001005 if (TI_BANDGAP_HAS(bgp, COUNTER))
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001006 for (i = 0; i < bgp->conf->sensor_count; i++)
1007 RMW_BITS(bgp, i, bgap_counter, counter_mask,
1008 bgp->clk_rate / 4);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001009
1010 /* Every thing is good? Then expose the sensors */
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001011 for (i = 0; i < bgp->conf->sensor_count; i++) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001012 char *domain;
1013
Eduardo Valentinf1553332013-04-08 08:19:13 -04001014 if (bgp->conf->sensors[i].register_cooling) {
1015 ret = bgp->conf->sensors[i].register_cooling(bgp, i);
1016 if (ret)
1017 goto remove_sensors;
1018 }
Eduardo Valentin04a4d102012-09-11 19:06:55 +03001019
Eduardo Valentinf1553332013-04-08 08:19:13 -04001020 if (bgp->conf->expose_sensor) {
1021 domain = bgp->conf->sensors[i].domain;
1022 ret = bgp->conf->expose_sensor(bgp, i, domain);
1023 if (ret)
1024 goto remove_last_cooling;
1025 }
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001026 }
1027
1028 /*
1029 * Enable the Interrupts once everything is set. Otherwise irq handler
1030 * might be called as soon as it is enabled where as rest of framework
1031 * is still getting initialised.
1032 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001033 if (TI_BANDGAP_HAS(bgp, TALERT)) {
1034 ret = ti_bandgap_talert_init(bgp, pdev);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001035 if (ret) {
1036 dev_err(&pdev->dev, "failed to initialize Talert IRQ\n");
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001037 i = bgp->conf->sensor_count;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001038 goto disable_clk;
1039 }
1040 }
1041
1042 return 0;
1043
Eduardo Valentinf1553332013-04-08 08:19:13 -04001044remove_last_cooling:
1045 if (bgp->conf->sensors[i].unregister_cooling)
1046 bgp->conf->sensors[i].unregister_cooling(bgp, i);
1047remove_sensors:
1048 for (i--; i >= 0; i--) {
1049 if (bgp->conf->sensors[i].unregister_cooling)
1050 bgp->conf->sensors[i].unregister_cooling(bgp, i);
1051 if (bgp->conf->remove_sensor)
1052 bgp->conf->remove_sensor(bgp, i);
1053 }
1054 ti_bandgap_power(bgp, false);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001055disable_clk:
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001056 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001057 clk_disable_unprepare(bgp->fclock);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001058put_clks:
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001059 clk_put(bgp->div_clk);
Luis Henriques882f5812016-11-16 22:15:22 +00001060put_fclock:
1061 clk_put(bgp->fclock);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001062free_irqs:
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001063 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001064 free_irq(gpio_to_irq(bgp->tshut_gpio), NULL);
1065 gpio_free(bgp->tshut_gpio);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001066 }
1067
1068 return ret;
1069}
1070
1071static
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001072int ti_bandgap_remove(struct platform_device *pdev)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001073{
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001074 struct ti_bandgap *bgp = platform_get_drvdata(pdev);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001075 int i;
1076
1077 /* First thing is to remove sensor interfaces */
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001078 for (i = 0; i < bgp->conf->sensor_count; i++) {
Eduardo Valentin262235b2013-04-08 08:19:14 -04001079 if (bgp->conf->sensors[i].unregister_cooling)
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001080 bgp->conf->sensors[i].unregister_cooling(bgp, i);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001081
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001082 if (bgp->conf->remove_sensor)
1083 bgp->conf->remove_sensor(bgp, i);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001084 }
1085
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001086 ti_bandgap_power(bgp, false);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001087
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001088 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001089 clk_disable_unprepare(bgp->fclock);
1090 clk_put(bgp->fclock);
1091 clk_put(bgp->div_clk);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001092
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001093 if (TI_BANDGAP_HAS(bgp, TALERT))
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001094 free_irq(bgp->irq, bgp);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001095
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001096 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001097 free_irq(gpio_to_irq(bgp->tshut_gpio), NULL);
1098 gpio_free(bgp->tshut_gpio);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001099 }
1100
1101 return 0;
1102}
1103
Grygorii Strashko3992b622015-02-06 16:55:46 +02001104#ifdef CONFIG_PM_SLEEP
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001105static int ti_bandgap_save_ctxt(struct ti_bandgap *bgp)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001106{
1107 int i;
1108
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001109 for (i = 0; i < bgp->conf->sensor_count; i++) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001110 struct temp_sensor_registers *tsr;
1111 struct temp_sensor_regval *rval;
1112
Eduardo Valentin9879b2c2013-03-19 10:54:23 -04001113 rval = &bgp->regval[i];
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001114 tsr = bgp->conf->sensors[i].registers;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001115
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001116 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
1117 rval->bg_mode_ctrl = ti_bandgap_readl(bgp,
J Keerthy76d2cd32012-09-11 19:06:52 +03001118 tsr->bgap_mode_ctrl);
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001119 if (TI_BANDGAP_HAS(bgp, COUNTER))
1120 rval->bg_counter = ti_bandgap_readl(bgp,
J Keerthy76d2cd32012-09-11 19:06:52 +03001121 tsr->bgap_counter);
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001122 if (TI_BANDGAP_HAS(bgp, TALERT)) {
1123 rval->bg_threshold = ti_bandgap_readl(bgp,
J Keerthy76d2cd32012-09-11 19:06:52 +03001124 tsr->bgap_threshold);
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001125 rval->bg_ctrl = ti_bandgap_readl(bgp,
J Keerthy76d2cd32012-09-11 19:06:52 +03001126 tsr->bgap_mask_ctrl);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001127 }
1128
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001129 if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG))
1130 rval->tshut_threshold = ti_bandgap_readl(bgp,
J Keerthy76d2cd32012-09-11 19:06:52 +03001131 tsr->tshut_threshold);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001132 }
1133
1134 return 0;
1135}
1136
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001137static int ti_bandgap_restore_ctxt(struct ti_bandgap *bgp)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001138{
1139 int i;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001140
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001141 for (i = 0; i < bgp->conf->sensor_count; i++) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001142 struct temp_sensor_registers *tsr;
1143 struct temp_sensor_regval *rval;
1144 u32 val = 0;
1145
Eduardo Valentin9879b2c2013-03-19 10:54:23 -04001146 rval = &bgp->regval[i];
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001147 tsr = bgp->conf->sensors[i].registers;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001148
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001149 if (TI_BANDGAP_HAS(bgp, COUNTER))
1150 val = ti_bandgap_readl(bgp, tsr->bgap_counter);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001151
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001152 if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG))
1153 ti_bandgap_writel(bgp, rval->tshut_threshold,
1154 tsr->tshut_threshold);
Radhesh Fadnisb87ea752012-11-13 14:10:04 -04001155 /* Force immediate temperature measurement and update
1156 * of the DTEMP field
1157 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001158 ti_bandgap_force_single_read(bgp, i);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001159
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001160 if (TI_BANDGAP_HAS(bgp, COUNTER))
1161 ti_bandgap_writel(bgp, rval->bg_counter,
1162 tsr->bgap_counter);
1163 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
1164 ti_bandgap_writel(bgp, rval->bg_mode_ctrl,
1165 tsr->bgap_mode_ctrl);
1166 if (TI_BANDGAP_HAS(bgp, TALERT)) {
1167 ti_bandgap_writel(bgp, rval->bg_threshold,
1168 tsr->bgap_threshold);
1169 ti_bandgap_writel(bgp, rval->bg_ctrl,
1170 tsr->bgap_mask_ctrl);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001171 }
1172 }
1173
1174 return 0;
1175}
1176
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001177static int ti_bandgap_suspend(struct device *dev)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001178{
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001179 struct ti_bandgap *bgp = dev_get_drvdata(dev);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001180 int err;
1181
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001182 err = ti_bandgap_save_ctxt(bgp);
1183 ti_bandgap_power(bgp, false);
Radhesh Fadnis6c9c1d62013-02-26 18:53:25 -04001184
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001185 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001186 clk_disable_unprepare(bgp->fclock);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001187
1188 return err;
1189}
1190
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001191static int ti_bandgap_resume(struct device *dev)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001192{
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001193 struct ti_bandgap *bgp = dev_get_drvdata(dev);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001194
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001195 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001196 clk_prepare_enable(bgp->fclock);
Radhesh Fadnis6c9c1d62013-02-26 18:53:25 -04001197
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001198 ti_bandgap_power(bgp, true);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001199
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001200 return ti_bandgap_restore_ctxt(bgp);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001201}
Jingoo Han5204f8c2014-02-27 20:43:02 +09001202static SIMPLE_DEV_PM_OPS(ti_bandgap_dev_pm_ops, ti_bandgap_suspend,
1203 ti_bandgap_resume);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001204
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001205#define DEV_PM_OPS (&ti_bandgap_dev_pm_ops)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001206#else
1207#define DEV_PM_OPS NULL
1208#endif
1209
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001210static const struct of_device_id of_ti_bandgap_match[] = {
Pavel Machek9c5c87e2015-04-02 16:49:07 +02001211#ifdef CONFIG_OMAP3_THERMAL
1212 {
1213 .compatible = "ti,omap34xx-bandgap",
1214 .data = (void *)&omap34xx_data,
1215 },
Eduardo Valentinb840b6e2015-09-21 17:32:15 -07001216 {
1217 .compatible = "ti,omap36xx-bandgap",
1218 .data = (void *)&omap36xx_data,
1219 },
Pavel Machek9c5c87e2015-04-02 16:49:07 +02001220#endif
Eduardo Valentin1a312702012-07-12 19:02:31 +03001221#ifdef CONFIG_OMAP4_THERMAL
1222 {
1223 .compatible = "ti,omap4430-bandgap",
1224 .data = (void *)&omap4430_data,
1225 },
1226 {
1227 .compatible = "ti,omap4460-bandgap",
1228 .data = (void *)&omap4460_data,
1229 },
1230 {
1231 .compatible = "ti,omap4470-bandgap",
1232 .data = (void *)&omap4470_data,
1233 },
1234#endif
Eduardo Valentin949f5a52012-07-12 19:02:32 +03001235#ifdef CONFIG_OMAP5_THERMAL
1236 {
1237 .compatible = "ti,omap5430-bandgap",
1238 .data = (void *)&omap5430_data,
1239 },
1240#endif
Eduardo Valentin25870e62013-05-29 15:07:45 +00001241#ifdef CONFIG_DRA752_THERMAL
1242 {
1243 .compatible = "ti,dra752-bandgap",
1244 .data = (void *)&dra752_data,
1245 },
1246#endif
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001247 /* Sentinel */
1248 { },
1249};
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001250MODULE_DEVICE_TABLE(of, of_ti_bandgap_match);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001251
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001252static struct platform_driver ti_bandgap_sensor_driver = {
1253 .probe = ti_bandgap_probe,
1254 .remove = ti_bandgap_remove,
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001255 .driver = {
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001256 .name = "ti-soc-thermal",
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001257 .pm = DEV_PM_OPS,
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001258 .of_match_table = of_ti_bandgap_match,
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001259 },
1260};
1261
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001262module_platform_driver(ti_bandgap_sensor_driver);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001263
1264MODULE_DESCRIPTION("OMAP4+ bandgap temperature sensor driver");
1265MODULE_LICENSE("GPL v2");
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001266MODULE_ALIAS("platform:ti-soc-thermal");
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001267MODULE_AUTHOR("Texas Instrument Inc.");