blob: 7d67f72e60d45e042b027f3ecdeae83e207aae26 [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 Valentin2aeeb8a2012-11-13 14:10:00 -040041#include <linux/io.h>
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +030042
Eduardo Valentin7372add2013-03-19 10:54:19 -040043#include "ti-bandgap.h"
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +030044
Eduardo Valentin8abbe712013-03-15 09:00:05 -040045/*** Helper functions to access registers and their bitfields ***/
46
Eduardo Valentin9c468aa2013-03-15 08:59:56 -040047/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -040048 * ti_bandgap_readl() - simple read helper function
49 * @bgp: pointer to ti_bandgap structure
Eduardo Valentin9c468aa2013-03-15 08:59:56 -040050 * @reg: desired register (offset) to be read
51 *
52 * Helper function to read bandgap registers. It uses the io remapped area.
53 * Returns the register value.
54 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -040055static u32 ti_bandgap_readl(struct ti_bandgap *bgp, u32 reg)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +030056{
Eduardo Valentind7f080e2013-03-19 10:54:18 -040057 return readl(bgp->base + reg);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +030058}
59
Eduardo Valentin9c468aa2013-03-15 08:59:56 -040060/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -040061 * ti_bandgap_writel() - simple write helper function
62 * @bgp: pointer to ti_bandgap structure
Eduardo Valentin9c468aa2013-03-15 08:59:56 -040063 * @val: desired register value to be written
64 * @reg: desired register (offset) to be written
65 *
66 * Helper function to write bandgap registers. It uses the io remapped area.
67 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -040068static void ti_bandgap_writel(struct ti_bandgap *bgp, u32 val, u32 reg)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +030069{
Eduardo Valentind7f080e2013-03-19 10:54:18 -040070 writel(val, bgp->base + reg);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +030071}
72
Eduardo Valentin9c468aa2013-03-15 08:59:56 -040073/**
74 * DOC: macro to update bits.
75 *
76 * RMW_BITS() - used to read, modify and update bandgap bitfields.
77 * The value passed will be shifted.
78 */
Eduardo Valentind7f080e2013-03-19 10:54:18 -040079#define RMW_BITS(bgp, id, reg, mask, val) \
Eduardo Valentind3c291a2013-03-15 08:59:55 -040080do { \
81 struct temp_sensor_registers *t; \
82 u32 r; \
83 \
Eduardo Valentind7f080e2013-03-19 10:54:18 -040084 t = bgp->conf->sensors[(id)].registers; \
Eduardo Valentin03e859d2013-03-19 10:54:21 -040085 r = ti_bandgap_readl(bgp, t->reg); \
Eduardo Valentind3c291a2013-03-15 08:59:55 -040086 r &= ~t->mask; \
87 r |= (val) << __ffs(t->mask); \
Eduardo Valentin03e859d2013-03-19 10:54:21 -040088 ti_bandgap_writel(bgp, r, t->reg); \
Eduardo Valentind3c291a2013-03-15 08:59:55 -040089} while (0)
90
Eduardo Valentin6ab52402013-03-15 09:00:06 -040091/*** Basic helper functions ***/
92
Eduardo Valentin7a556e62013-03-15 08:59:58 -040093/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -040094 * ti_bandgap_power() - controls the power state of a bandgap device
95 * @bgp: pointer to ti_bandgap structure
Eduardo Valentin7a556e62013-03-15 08:59:58 -040096 * @on: desired power state (1 - on, 0 - off)
97 *
98 * Used to power on/off a bandgap device instance. Only used on those
99 * that features tempsoff bit.
100 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400101static int ti_bandgap_power(struct ti_bandgap *bgp, bool on)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300102{
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300103 int i;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300104
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400105 if (!TI_BANDGAP_HAS(bgp, POWER_SWITCH))
Eduardo Valentin3d84e522013-03-15 08:59:57 -0400106 goto exit;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300107
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400108 for (i = 0; i < bgp->conf->sensor_count; i++)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300109 /* active on 0 */
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400110 RMW_BITS(bgp, i, temp_sensor_ctrl, bgap_tempsoff_mask, !on);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300111
Eduardo Valentin3d84e522013-03-15 08:59:57 -0400112exit:
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300113 return 0;
114}
115
Eduardo Valentin4a6554e2013-03-15 08:59:59 -0400116/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400117 * ti_bandgap_read_temp() - helper function to read sensor temperature
118 * @bgp: pointer to ti_bandgap structure
Eduardo Valentin4a6554e2013-03-15 08:59:59 -0400119 * @id: bandgap sensor id
120 *
121 * Function to concentrate the steps to read sensor temperature register.
122 * This function is desired because, depending on bandgap device version,
123 * it might be needed to freeze the bandgap state machine, before fetching
124 * the register value.
125 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400126static u32 ti_bandgap_read_temp(struct ti_bandgap *bgp, int id)
Eduardo Valentin194a54f2013-02-26 18:53:33 -0400127{
128 struct temp_sensor_registers *tsr;
Eduardo Valentind3c291a2013-03-15 08:59:55 -0400129 u32 temp, reg;
Eduardo Valentin194a54f2013-02-26 18:53:33 -0400130
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400131 tsr = bgp->conf->sensors[id].registers;
Eduardo Valentin194a54f2013-02-26 18:53:33 -0400132 reg = tsr->temp_sensor_ctrl;
133
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400134 if (TI_BANDGAP_HAS(bgp, FREEZE_BIT)) {
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400135 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 1);
Eduardo Valentin194a54f2013-02-26 18:53:33 -0400136 /*
137 * In case we cannot read from cur_dtemp / dtemp_0,
138 * then we read from the last valid temp read
139 */
140 reg = tsr->ctrl_dtemp_1;
141 }
142
143 /* read temperature */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400144 temp = ti_bandgap_readl(bgp, reg);
Eduardo Valentin194a54f2013-02-26 18:53:33 -0400145 temp &= tsr->bgap_dtemp_mask;
146
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400147 if (TI_BANDGAP_HAS(bgp, FREEZE_BIT))
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400148 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 0);
Eduardo Valentin194a54f2013-02-26 18:53:33 -0400149
150 return temp;
151}
152
Eduardo Valentinfb65b882013-03-15 09:00:07 -0400153/*** IRQ handlers ***/
154
Eduardo Valentinee07d552013-03-15 09:00:01 -0400155/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400156 * ti_bandgap_talert_irq_handler() - handles Temperature alert IRQs
Eduardo Valentinee07d552013-03-15 09:00:01 -0400157 * @irq: IRQ number
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400158 * @data: private data (struct ti_bandgap *)
Eduardo Valentinee07d552013-03-15 09:00:01 -0400159 *
160 * This is the Talert handler. Use it only if bandgap device features
161 * HAS(TALERT). This handler goes over all sensors and checks their
162 * conditions and acts accordingly. In case there are events pending,
163 * it will reset the event mask to wait for the opposite event (next event).
164 * Every time there is a new event, it will be reported to thermal layer.
165 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400166static irqreturn_t ti_bandgap_talert_irq_handler(int irq, void *data)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300167{
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400168 struct ti_bandgap *bgp = data;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300169 struct temp_sensor_registers *tsr;
Eduardo Valentin194a54f2013-02-26 18:53:33 -0400170 u32 t_hot = 0, t_cold = 0, ctrl;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300171 int i;
172
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400173 spin_lock(&bgp->lock);
174 for (i = 0; i < bgp->conf->sensor_count; i++) {
175 tsr = bgp->conf->sensors[i].registers;
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400176 ctrl = ti_bandgap_readl(bgp, tsr->bgap_status);
Eduardo Valentine555c952013-03-15 09:00:04 -0400177
178 /* Read the status of t_hot */
179 t_hot = ctrl & tsr->status_hot_mask;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300180
181 /* Read the status of t_cold */
Eduardo Valentine555c952013-03-15 09:00:04 -0400182 t_cold = ctrl & tsr->status_cold_mask;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300183
184 if (!t_cold && !t_hot)
185 continue;
186
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400187 ctrl = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300188 /*
189 * One TALERT interrupt: Two sources
190 * If the interrupt is due to t_hot then mask t_hot and
191 * and unmask t_cold else mask t_cold and unmask t_hot
192 */
193 if (t_hot) {
194 ctrl &= ~tsr->mask_hot_mask;
195 ctrl |= tsr->mask_cold_mask;
196 } else if (t_cold) {
197 ctrl &= ~tsr->mask_cold_mask;
198 ctrl |= tsr->mask_hot_mask;
199 }
200
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400201 ti_bandgap_writel(bgp, ctrl, tsr->bgap_mask_ctrl);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300202
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400203 dev_dbg(bgp->dev,
Eduardo Valentin71e303f2012-11-13 14:10:03 -0400204 "%s: IRQ from %s sensor: hotevent %d coldevent %d\n",
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400205 __func__, bgp->conf->sensors[i].domain,
Eduardo Valentin71e303f2012-11-13 14:10:03 -0400206 t_hot, t_cold);
207
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300208 /* report temperature to whom may concern */
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400209 if (bgp->conf->report_temperature)
210 bgp->conf->report_temperature(bgp, i);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300211 }
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400212 spin_unlock(&bgp->lock);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300213
214 return IRQ_HANDLED;
215}
216
Eduardo Valentin79857cd22013-03-15 09:00:02 -0400217/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400218 * ti_bandgap_tshut_irq_handler() - handles Temperature shutdown signal
Eduardo Valentin79857cd22013-03-15 09:00:02 -0400219 * @irq: IRQ number
220 * @data: private data (unused)
221 *
222 * This is the Tshut handler. Use it only if bandgap device features
223 * HAS(TSHUT). If any sensor fires the Tshut signal, we simply shutdown
224 * the system.
225 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400226static irqreturn_t ti_bandgap_tshut_irq_handler(int irq, void *data)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300227{
Ruslan Ruslichenkob3bf0e92013-02-26 18:53:24 -0400228 pr_emerg("%s: TSHUT temperature reached. Needs shut down...\n",
229 __func__);
230
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300231 orderly_poweroff(true);
232
233 return IRQ_HANDLED;
234}
235
Eduardo Valentin2f6af4b2013-03-15 09:00:08 -0400236/*** Helper functions which manipulate conversion ADC <-> mi Celsius ***/
237
Eduardo Valentin2577e932013-03-15 09:00:11 -0400238/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400239 * ti_bandgap_adc_to_mcelsius() - converts an ADC value to mCelsius scale
240 * @bgp: struct ti_bandgap pointer
Eduardo Valentin2577e932013-03-15 09:00:11 -0400241 * @adc_val: value in ADC representation
242 * @t: address where to write the resulting temperature in mCelsius
243 *
244 * Simple conversion from ADC representation to mCelsius. In case the ADC value
245 * is out of the ADC conv table range, it returns -ERANGE, 0 on success.
246 * The conversion table is indexed by the ADC values.
247 */
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300248static
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400249int ti_bandgap_adc_to_mcelsius(struct ti_bandgap *bgp, int adc_val, int *t)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300250{
Eduardo Valentin9879b2c2013-03-19 10:54:23 -0400251 const struct ti_bandgap_data *conf = bgp->conf;
Eduardo Valentin20470592013-03-15 09:00:10 -0400252 int ret = 0;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300253
254 /* look up for temperature in the table and return the temperature */
Eduardo Valentin26a70ed2013-03-15 09:00:14 -0400255 if (adc_val < conf->adc_start_val || adc_val > conf->adc_end_val) {
Eduardo Valentin20470592013-03-15 09:00:10 -0400256 ret = -ERANGE;
257 goto exit;
258 }
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300259
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400260 *t = bgp->conf->conv_table[adc_val - conf->adc_start_val];
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300261
Eduardo Valentin20470592013-03-15 09:00:10 -0400262exit:
263 return ret;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300264}
265
Eduardo Valentine7f60b52013-03-15 09:00:15 -0400266/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400267 * ti_bandgap_mcelsius_to_adc() - converts a mCelsius value to ADC scale
268 * @bgp: struct ti_bandgap pointer
Eduardo Valentine7f60b52013-03-15 09:00:15 -0400269 * @temp: value in mCelsius
270 * @adc: address where to write the resulting temperature in ADC representation
271 *
272 * Simple conversion from mCelsius to ADC values. In case the temp value
273 * is out of the ADC conv table range, it returns -ERANGE, 0 on success.
274 * The conversion table is indexed by the ADC values.
275 */
Eduardo Valentine16f0722013-03-15 09:00:12 -0400276static
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400277int ti_bandgap_mcelsius_to_adc(struct ti_bandgap *bgp, long temp, int *adc)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300278{
Eduardo Valentin9879b2c2013-03-19 10:54:23 -0400279 const struct ti_bandgap_data *conf = bgp->conf;
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400280 const int *conv_table = bgp->conf->conv_table;
Eduardo Valentina6194772013-03-15 09:00:13 -0400281 int high, low, mid, ret = 0;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300282
283 low = 0;
Eduardo Valentin26a70ed2013-03-15 09:00:14 -0400284 high = conf->adc_end_val - conf->adc_start_val;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300285 mid = (high + low) / 2;
286
Eduardo Valentina6194772013-03-15 09:00:13 -0400287 if (temp < conv_table[low] || temp > conv_table[high]) {
288 ret = -ERANGE;
289 goto exit;
290 }
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300291
292 while (low < high) {
Eduardo Valentinc8a8f842013-02-26 18:53:36 -0400293 if (temp < conv_table[mid])
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300294 high = mid - 1;
295 else
296 low = mid + 1;
297 mid = (low + high) / 2;
298 }
299
Eduardo Valentin26a70ed2013-03-15 09:00:14 -0400300 *adc = conf->adc_start_val + low;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300301
Eduardo Valentina6194772013-03-15 09:00:13 -0400302exit:
303 return ret;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300304}
305
Eduardo Valentin8a1cefe2013-03-15 09:00:17 -0400306/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400307 * ti_bandgap_add_hyst() - add hysteresis (in mCelsius) to an ADC value
308 * @bgp: struct ti_bandgap pointer
Eduardo Valentin8a1cefe2013-03-15 09:00:17 -0400309 * @adc_val: temperature value in ADC representation
310 * @hyst_val: hysteresis value in mCelsius
311 * @sum: address where to write the resulting temperature (in ADC scale)
312 *
313 * Adds an hysteresis value (in mCelsius) to a ADC temperature value.
314 * Returns 0 on success, -ERANGE otherwise.
315 */
Eduardo Valentin0f0ed7d2013-03-15 09:00:16 -0400316static
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400317int ti_bandgap_add_hyst(struct ti_bandgap *bgp, int adc_val, int hyst_val,
318 u32 *sum)
Eduardo Valentin0f0ed7d2013-03-15 09:00:16 -0400319{
320 int temp, ret;
321
322 /*
323 * Need to add in the mcelsius domain, so we have a temperature
324 * the conv_table range
325 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400326 ret = ti_bandgap_adc_to_mcelsius(bgp, adc_val, &temp);
Eduardo Valentin0f0ed7d2013-03-15 09:00:16 -0400327 if (ret < 0)
328 goto exit;
329
330 temp += hyst_val;
331
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400332 ret = ti_bandgap_mcelsius_to_adc(bgp, temp, sum);
Eduardo Valentin0f0ed7d2013-03-15 09:00:16 -0400333
334exit:
335 return ret;
336}
337
Eduardo Valentinf8ccce22013-03-15 09:00:18 -0400338/*** Helper functions handling device Alert/Shutdown signals ***/
339
Eduardo Valentinf47f6d32013-03-15 09:00:20 -0400340/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400341 * ti_bandgap_unmask_interrupts() - unmasks the events of thot & tcold
342 * @bgp: struct ti_bandgap pointer
Eduardo Valentinf47f6d32013-03-15 09:00:20 -0400343 * @t_hot: hot temperature value to trigger alert signal
344 * @t_cold: cold temperature value to trigger alert signal
345 *
346 * Checks the requested t_hot and t_cold values and configures the IRQ event
347 * masks accordingly. Call this function only if bandgap features HAS(TALERT).
348 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400349static void ti_bandgap_unmask_interrupts(struct ti_bandgap *bgp, int id,
350 u32 t_hot, u32 t_cold)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300351{
352 struct temp_sensor_registers *tsr;
353 u32 temp, reg_val;
354
355 /* Read the current on die temperature */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400356 temp = ti_bandgap_read_temp(bgp, id);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300357
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400358 tsr = bgp->conf->sensors[id].registers;
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400359 reg_val = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl);
Eduardo Valentin194a54f2013-02-26 18:53:33 -0400360
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300361 if (temp < t_hot)
362 reg_val |= tsr->mask_hot_mask;
363 else
364 reg_val &= ~tsr->mask_hot_mask;
365
366 if (t_cold < temp)
367 reg_val |= tsr->mask_cold_mask;
368 else
369 reg_val &= ~tsr->mask_cold_mask;
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400370 ti_bandgap_writel(bgp, reg_val, tsr->bgap_mask_ctrl);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300371}
372
Eduardo Valentin38d99e82013-03-15 09:00:27 -0400373/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400374 * ti_bandgap_update_alert_threshold() - sequence to update thresholds
375 * @bgp: struct ti_bandgap pointer
Eduardo Valentin38d99e82013-03-15 09:00:27 -0400376 * @id: bandgap sensor id
377 * @val: value (ADC) of a new threshold
378 * @hot: desired threshold to be updated. true if threshold hot, false if
379 * threshold cold
380 *
381 * It will program the required thresholds (hot and cold) for TALERT signal.
382 * This function can be used to update t_hot or t_cold, depending on @hot value.
383 * It checks the resulting t_hot and t_cold values, based on the new passed @val
384 * and configures the thresholds so that t_hot is always greater than t_cold.
385 * Call this function only if bandgap features HAS(TALERT).
386 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400387static int ti_bandgap_update_alert_threshold(struct ti_bandgap *bgp, int id,
388 int val, bool hot)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300389{
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400390 struct temp_sensor_data *ts_data = bgp->conf->sensors[id].ts_data;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300391 struct temp_sensor_registers *tsr;
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400392 u32 thresh_val, reg_val, t_hot, t_cold;
393 int err = 0;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300394
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400395 tsr = bgp->conf->sensors[id].registers;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300396
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400397 /* obtain the current value */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400398 thresh_val = ti_bandgap_readl(bgp, tsr->bgap_threshold);
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400399 t_cold = (thresh_val & tsr->threshold_tcold_mask) >>
400 __ffs(tsr->threshold_tcold_mask);
401 t_hot = (thresh_val & tsr->threshold_thot_mask) >>
402 __ffs(tsr->threshold_thot_mask);
403 if (hot)
404 t_hot = val;
405 else
406 t_cold = val;
407
408 if (t_cold < t_hot) {
409 if (hot)
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400410 err = ti_bandgap_add_hyst(bgp, t_hot,
411 -ts_data->hyst_val,
412 &t_cold);
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400413 else
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400414 err = ti_bandgap_add_hyst(bgp, t_cold,
415 ts_data->hyst_val,
416 &t_hot);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300417 }
418
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400419 /* write the new threshold values */
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300420 reg_val = thresh_val & ~tsr->threshold_thot_mask;
421 reg_val |= (t_hot << __ffs(tsr->threshold_thot_mask));
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400422 reg_val |= thresh_val & ~tsr->threshold_tcold_mask;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300423 reg_val |= (t_cold << __ffs(tsr->threshold_tcold_mask));
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400424 ti_bandgap_writel(bgp, reg_val, tsr->bgap_threshold);
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400425
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300426 if (err) {
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400427 dev_err(bgp->dev, "failed to reprogram thot threshold\n");
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400428 err = -EIO;
429 goto exit;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300430 }
431
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400432 ti_bandgap_unmask_interrupts(bgp, id, t_hot, t_cold);
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400433exit:
434 return err;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300435}
436
Eduardo Valentine72b7bb2013-03-15 09:00:38 -0400437/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400438 * ti_bandgap_validate() - helper to check the sanity of a struct ti_bandgap
439 * @bgp: struct ti_bandgap pointer
Eduardo Valentine72b7bb2013-03-15 09:00:38 -0400440 * @id: bandgap sensor id
441 *
442 * Checks if the bandgap pointer is valid and if the sensor id is also
443 * applicable.
444 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400445static inline int ti_bandgap_validate(struct ti_bandgap *bgp, int id)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300446{
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400447 int ret = 0;
448
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400449 if (IS_ERR_OR_NULL(bgp)) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300450 pr_err("%s: invalid bandgap pointer\n", __func__);
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400451 ret = -EINVAL;
452 goto exit;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300453 }
454
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400455 if ((id < 0) || (id >= bgp->conf->sensor_count)) {
456 dev_err(bgp->dev, "%s: sensor id out of range (%d)\n",
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300457 __func__, id);
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400458 ret = -ERANGE;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300459 }
460
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400461exit:
462 return ret;
463}
464
Eduardo Valentin9efa93b2013-03-15 09:00:28 -0400465/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400466 * _ti_bandgap_write_threshold() - helper to update TALERT t_cold or t_hot
467 * @bgp: struct ti_bandgap pointer
Eduardo Valentin9efa93b2013-03-15 09:00:28 -0400468 * @id: bandgap sensor id
469 * @val: value (mCelsius) of a new threshold
470 * @hot: desired threshold to be updated. true if threshold hot, false if
471 * threshold cold
472 *
473 * It will update the required thresholds (hot and cold) for TALERT signal.
474 * This function can be used to update t_hot or t_cold, depending on @hot value.
475 * Validates the mCelsius range and update the requested threshold.
476 * Call this function only if bandgap features HAS(TALERT).
477 */
Eduardo Valentin2f8ec2a2013-03-19 10:54:22 -0400478static int _ti_bandgap_write_threshold(struct ti_bandgap *bgp, int id, int val,
479 bool hot)
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400480{
481 struct temp_sensor_data *ts_data;
482 struct temp_sensor_registers *tsr;
483 u32 adc_val;
484 int ret;
485
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400486 ret = ti_bandgap_validate(bgp, id);
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400487 if (ret)
488 goto exit;
489
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400490 if (!TI_BANDGAP_HAS(bgp, TALERT)) {
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400491 ret = -ENOTSUPP;
492 goto exit;
493 }
494
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400495 ts_data = bgp->conf->sensors[id].ts_data;
496 tsr = bgp->conf->sensors[id].registers;
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400497 if (hot) {
498 if (val < ts_data->min_temp + ts_data->hyst_val)
499 ret = -EINVAL;
500 } else {
501 if (val > ts_data->max_temp + ts_data->hyst_val)
502 ret = -EINVAL;
503 }
504
505 if (ret)
506 goto exit;
507
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400508 ret = ti_bandgap_mcelsius_to_adc(bgp, val, &adc_val);
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400509 if (ret < 0)
510 goto exit;
511
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400512 spin_lock(&bgp->lock);
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400513 ti_bandgap_update_alert_threshold(bgp, id, adc_val, hot);
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400514 spin_unlock(&bgp->lock);
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400515
516exit:
517 return ret;
518}
519
Eduardo Valentin7a681a52013-03-15 09:00:29 -0400520/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400521 * _ti_bandgap_read_threshold() - helper to read TALERT t_cold or t_hot
522 * @bgp: struct ti_bandgap pointer
Eduardo Valentin7a681a52013-03-15 09:00:29 -0400523 * @id: bandgap sensor id
524 * @val: value (mCelsius) of a threshold
525 * @hot: desired threshold to be read. true if threshold hot, false if
526 * threshold cold
527 *
528 * It will fetch the required thresholds (hot and cold) for TALERT signal.
529 * This function can be used to read t_hot or t_cold, depending on @hot value.
530 * Call this function only if bandgap features HAS(TALERT).
531 */
Eduardo Valentin2f8ec2a2013-03-19 10:54:22 -0400532static int _ti_bandgap_read_threshold(struct ti_bandgap *bgp, int id,
533 int *val, bool hot)
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400534{
535 struct temp_sensor_registers *tsr;
536 u32 temp, mask;
537 int ret = 0;
538
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400539 ret = ti_bandgap_validate(bgp, id);
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400540 if (ret)
541 goto exit;
542
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400543 if (!TI_BANDGAP_HAS(bgp, TALERT)) {
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400544 ret = -ENOTSUPP;
545 goto exit;
546 }
547
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400548 tsr = bgp->conf->sensors[id].registers;
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400549 if (hot)
550 mask = tsr->threshold_thot_mask;
551 else
552 mask = tsr->threshold_tcold_mask;
553
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400554 temp = ti_bandgap_readl(bgp, tsr->bgap_threshold);
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400555 temp = (temp & mask) >> __ffs(mask);
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400556 ret |= ti_bandgap_adc_to_mcelsius(bgp, temp, &temp);
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400557 if (ret) {
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400558 dev_err(bgp->dev, "failed to read thot\n");
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400559 ret = -EIO;
560 goto exit;
561 }
562
563 *val = temp;
564
565exit:
Eduardo Valentin648b4c62013-03-19 10:54:17 -0400566 return ret;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300567}
568
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400569/*** Exposed APIs ***/
570
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300571/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400572 * ti_bandgap_read_thot() - reads sensor current thot
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400573 * @bgp - pointer to bandgap instance
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300574 * @id - sensor id
575 * @thot - resulting current thot value
576 *
577 * returns 0 on success or the proper error code
578 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400579int ti_bandgap_read_thot(struct ti_bandgap *bgp, int id, int *thot)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300580{
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400581 return _ti_bandgap_read_threshold(bgp, id, thot, true);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300582}
583
584/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400585 * ti_bandgap_write_thot() - sets sensor current thot
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400586 * @bgp - pointer to bandgap instance
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300587 * @id - sensor id
588 * @val - desired thot value
589 *
590 * returns 0 on success or the proper error code
591 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400592int ti_bandgap_write_thot(struct ti_bandgap *bgp, int id, int val)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300593{
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400594 return _ti_bandgap_write_threshold(bgp, id, val, true);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300595}
596
597/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400598 * ti_bandgap_read_tcold() - reads sensor current tcold
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400599 * @bgp - pointer to bandgap instance
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300600 * @id - sensor id
601 * @tcold - resulting current tcold value
602 *
603 * returns 0 on success or the proper error code
604 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400605int ti_bandgap_read_tcold(struct ti_bandgap *bgp, int id, int *tcold)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300606{
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400607 return _ti_bandgap_read_threshold(bgp, id, tcold, false);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300608}
609
610/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400611 * ti_bandgap_write_tcold() - sets the sensor tcold
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400612 * @bgp - pointer to bandgap instance
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300613 * @id - sensor id
614 * @val - desired tcold value
615 *
616 * returns 0 on success or the proper error code
617 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400618int ti_bandgap_write_tcold(struct ti_bandgap *bgp, int id, int val)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300619{
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400620 return _ti_bandgap_write_threshold(bgp, id, val, false);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300621}
622
623/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400624 * ti_bandgap_read_update_interval() - read the sensor update interval
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400625 * @bgp - pointer to bandgap instance
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300626 * @id - sensor id
627 * @interval - resulting update interval in miliseconds
628 *
629 * returns 0 on success or the proper error code
630 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400631int ti_bandgap_read_update_interval(struct ti_bandgap *bgp, int id,
632 int *interval)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300633{
634 struct temp_sensor_registers *tsr;
635 u32 time;
636 int ret;
637
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400638 ret = ti_bandgap_validate(bgp, id);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300639 if (ret)
640 return ret;
641
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400642 if (!TI_BANDGAP_HAS(bgp, COUNTER))
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300643 return -ENOTSUPP;
644
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400645 tsr = bgp->conf->sensors[id].registers;
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400646 time = ti_bandgap_readl(bgp, tsr->bgap_counter);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300647 time = (time & tsr->counter_mask) >> __ffs(tsr->counter_mask);
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400648 time = time * 1000 / bgp->clk_rate;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300649
650 *interval = time;
651
652 return 0;
653}
654
655/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400656 * ti_bandgap_write_update_interval() - set the update interval
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400657 * @bgp - pointer to bandgap instance
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300658 * @id - sensor id
659 * @interval - desired update interval in miliseconds
660 *
661 * returns 0 on success or the proper error code
662 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400663int ti_bandgap_write_update_interval(struct ti_bandgap *bgp,
664 int id, u32 interval)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300665{
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400666 int ret = ti_bandgap_validate(bgp, id);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300667 if (ret)
668 return ret;
669
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400670 if (!TI_BANDGAP_HAS(bgp, COUNTER))
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300671 return -ENOTSUPP;
672
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400673 interval = interval * bgp->clk_rate / 1000;
674 spin_lock(&bgp->lock);
675 RMW_BITS(bgp, id, bgap_counter, counter_mask, interval);
676 spin_unlock(&bgp->lock);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300677
678 return 0;
679}
680
681/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400682 * ti_bandgap_read_temperature() - report current temperature
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400683 * @bgp - pointer to bandgap instance
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300684 * @id - sensor id
685 * @temperature - resulting temperature
686 *
687 * returns 0 on success or the proper error code
688 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400689int ti_bandgap_read_temperature(struct ti_bandgap *bgp, int id,
690 int *temperature)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300691{
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300692 u32 temp;
693 int ret;
694
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400695 ret = ti_bandgap_validate(bgp, id);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300696 if (ret)
697 return ret;
698
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400699 spin_lock(&bgp->lock);
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400700 temp = ti_bandgap_read_temp(bgp, id);
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400701 spin_unlock(&bgp->lock);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300702
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400703 ret |= ti_bandgap_adc_to_mcelsius(bgp, temp, &temp);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300704 if (ret)
705 return -EIO;
706
707 *temperature = temp;
708
709 return 0;
710}
711
712/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400713 * ti_bandgap_set_sensor_data() - helper function to store thermal
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300714 * framework related data.
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400715 * @bgp - pointer to bandgap instance
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300716 * @id - sensor id
717 * @data - thermal framework related data to be stored
718 *
719 * returns 0 on success or the proper error code
720 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400721int ti_bandgap_set_sensor_data(struct ti_bandgap *bgp, int id, void *data)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300722{
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400723 int ret = ti_bandgap_validate(bgp, id);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300724 if (ret)
725 return ret;
726
Eduardo Valentin9879b2c2013-03-19 10:54:23 -0400727 bgp->regval[id].data = data;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300728
729 return 0;
730}
731
732/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400733 * ti_bandgap_get_sensor_data() - helper function to get thermal
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300734 * framework related data.
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400735 * @bgp - pointer to bandgap instance
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300736 * @id - sensor id
737 *
738 * returns data stored by set function with sensor id on success or NULL
739 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400740void *ti_bandgap_get_sensor_data(struct ti_bandgap *bgp, int id)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300741{
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400742 int ret = ti_bandgap_validate(bgp, id);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300743 if (ret)
744 return ERR_PTR(ret);
745
Eduardo Valentin9879b2c2013-03-19 10:54:23 -0400746 return bgp->regval[id].data;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300747}
748
Eduardo Valentine195aba2013-03-15 09:00:22 -0400749/*** Helper functions used during device initialization ***/
750
Eduardo Valentin31102a72013-03-15 09:00:26 -0400751/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400752 * ti_bandgap_force_single_read() - executes 1 single ADC conversion
753 * @bgp: pointer to struct ti_bandgap
Eduardo Valentin31102a72013-03-15 09:00:26 -0400754 * @id: sensor id which it is desired to read 1 temperature
755 *
756 * Used to initialize the conversion state machine and set it to a valid
757 * state. Called during device initialization and context restore events.
758 */
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300759static int
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400760ti_bandgap_force_single_read(struct ti_bandgap *bgp, int id)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300761{
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300762 u32 temp = 0, counter = 1000;
763
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300764 /* Select single conversion mode */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400765 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400766 RMW_BITS(bgp, id, bgap_mode_ctrl, mode_ctrl_mask, 0);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300767
768 /* Start of Conversion = 1 */
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400769 RMW_BITS(bgp, id, temp_sensor_ctrl, bgap_soc_mask, 1);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300770 /* Wait until DTEMP is updated */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400771 temp = ti_bandgap_read_temp(bgp, id);
Eduardo Valentin194a54f2013-02-26 18:53:33 -0400772
773 while ((temp == 0) && --counter)
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400774 temp = ti_bandgap_read_temp(bgp, id);
Eduardo Valentind3c291a2013-03-15 08:59:55 -0400775 /* REVISIT: Check correct condition for end of conversion */
Eduardo Valentin194a54f2013-02-26 18:53:33 -0400776
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300777 /* Start of Conversion = 0 */
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400778 RMW_BITS(bgp, id, temp_sensor_ctrl, bgap_soc_mask, 0);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300779
780 return 0;
781}
782
783/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400784 * ti_bandgap_set_continous_mode() - One time enabling of continuous mode
785 * @bgp: pointer to struct ti_bandgap
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300786 *
Eduardo Valentina84b6f42013-03-15 09:00:25 -0400787 * Call this function only if HAS(MODE_CONFIG) is set. As this driver may
788 * be used for junction temperature monitoring, it is desirable that the
789 * sensors are operational all the time, so that alerts are generated
790 * properly.
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300791 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400792static int ti_bandgap_set_continuous_mode(struct ti_bandgap *bgp)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300793{
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300794 int i;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300795
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400796 for (i = 0; i < bgp->conf->sensor_count; i++) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300797 /* Perform a single read just before enabling continuous */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400798 ti_bandgap_force_single_read(bgp, i);
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400799 RMW_BITS(bgp, i, bgap_mode_ctrl, mode_ctrl_mask, 1);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300800 }
801
802 return 0;
803}
804
Eduardo Valentind3790b32013-03-15 09:00:30 -0400805/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400806 * ti_bandgap_tshut_init() - setup and initialize tshut handling
807 * @bgp: pointer to struct ti_bandgap
Eduardo Valentind3790b32013-03-15 09:00:30 -0400808 * @pdev: pointer to device struct platform_device
809 *
810 * Call this function only in case the bandgap features HAS(TSHUT).
811 * In this case, the driver needs to handle the TSHUT signal as an IRQ.
812 * The IRQ is wired as a GPIO, and for this purpose, it is required
813 * to specify which GPIO line is used. TSHUT IRQ is fired anytime
814 * one of the bandgap sensors violates the TSHUT high/hot threshold.
815 * And in that case, the system must go off.
816 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400817static int ti_bandgap_tshut_init(struct ti_bandgap *bgp,
818 struct platform_device *pdev)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300819{
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400820 int gpio_nr = bgp->tshut_gpio;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300821 int status;
822
823 /* Request for gpio_86 line */
824 status = gpio_request(gpio_nr, "tshut");
825 if (status < 0) {
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400826 dev_err(bgp->dev, "Could not request for TSHUT GPIO:%i\n", 86);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300827 return status;
828 }
829 status = gpio_direction_input(gpio_nr);
830 if (status) {
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400831 dev_err(bgp->dev, "Cannot set input TSHUT GPIO %d\n", gpio_nr);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300832 return status;
833 }
834
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400835 status = request_irq(gpio_to_irq(gpio_nr), ti_bandgap_tshut_irq_handler,
836 IRQF_TRIGGER_RISING, "tshut", NULL);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300837 if (status) {
838 gpio_free(gpio_nr);
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400839 dev_err(bgp->dev, "request irq failed for TSHUT");
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300840 }
841
842 return 0;
843}
844
Eduardo Valentin094b8ac2013-03-15 09:00:31 -0400845/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400846 * ti_bandgap_alert_init() - setup and initialize talert handling
847 * @bgp: pointer to struct ti_bandgap
Eduardo Valentin094b8ac2013-03-15 09:00:31 -0400848 * @pdev: pointer to device struct platform_device
849 *
850 * Call this function only in case the bandgap features HAS(TALERT).
851 * In this case, the driver needs to handle the TALERT signals as an IRQs.
852 * TALERT is a normal IRQ and it is fired any time thresholds (hot or cold)
853 * are violated. In these situation, the driver must reprogram the thresholds,
854 * accordingly to specified policy.
855 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400856static int ti_bandgap_talert_init(struct ti_bandgap *bgp,
857 struct platform_device *pdev)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300858{
859 int ret;
860
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400861 bgp->irq = platform_get_irq(pdev, 0);
862 if (bgp->irq < 0) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300863 dev_err(&pdev->dev, "get_irq failed\n");
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400864 return bgp->irq;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300865 }
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400866 ret = request_threaded_irq(bgp->irq, NULL,
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400867 ti_bandgap_talert_irq_handler,
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300868 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400869 "talert", bgp);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300870 if (ret) {
871 dev_err(&pdev->dev, "Request threaded irq failed.\n");
872 return ret;
873 }
874
875 return 0;
876}
877
Eduardo Valentine9b6f8c2013-03-15 09:00:32 -0400878/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400879 * ti_bandgap_build() - parse DT and setup a struct ti_bandgap
880 * @bgp: pointer to struct ti_bandgap
Eduardo Valentine9b6f8c2013-03-15 09:00:32 -0400881 * @pdev: pointer to device struct platform_device
882 *
883 * Used to read the device tree properties accordingly to the bandgap
884 * matching version. Based on bandgap version and its capabilities it
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400885 * will build a struct ti_bandgap out of the required DT entries.
Eduardo Valentine9b6f8c2013-03-15 09:00:32 -0400886 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400887static const struct of_device_id of_ti_bandgap_match[];
888static struct ti_bandgap *ti_bandgap_build(struct platform_device *pdev)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300889{
890 struct device_node *node = pdev->dev.of_node;
891 const struct of_device_id *of_id;
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400892 struct ti_bandgap *bgp;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300893 struct resource *res;
894 u32 prop;
895 int i;
896
897 /* just for the sake */
898 if (!node) {
899 dev_err(&pdev->dev, "no platform information available\n");
900 return ERR_PTR(-EINVAL);
901 }
902
Eduardo Valentinf6843562013-03-19 10:54:24 -0400903 bgp = devm_kzalloc(&pdev->dev, sizeof(*bgp), GFP_KERNEL);
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400904 if (!bgp) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300905 dev_err(&pdev->dev, "Unable to allocate mem for driver ref\n");
906 return ERR_PTR(-ENOMEM);
907 }
908
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400909 of_id = of_match_device(of_ti_bandgap_match, &pdev->dev);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300910 if (of_id)
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400911 bgp->conf = of_id->data;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300912
Eduardo Valentin9879b2c2013-03-19 10:54:23 -0400913 /* register shadow for context save and restore */
914 bgp->regval = devm_kzalloc(&pdev->dev, sizeof(*bgp->regval) *
915 bgp->conf->sensor_count, GFP_KERNEL);
916 if (!bgp) {
917 dev_err(&pdev->dev, "Unable to allocate mem for driver ref\n");
918 return ERR_PTR(-ENOMEM);
919 }
920
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300921 i = 0;
922 do {
923 void __iomem *chunk;
924
925 res = platform_get_resource(pdev, IORESOURCE_MEM, i);
926 if (!res)
927 break;
Thierry Reding97f4be602013-01-21 11:09:19 +0100928 chunk = devm_ioremap_resource(&pdev->dev, res);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300929 if (i == 0)
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400930 bgp->base = chunk;
Thierry Reding97f4be602013-01-21 11:09:19 +0100931 if (IS_ERR(chunk))
932 return ERR_CAST(chunk);
Eduardo Valentin24796e12013-03-15 08:59:53 -0400933
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300934 i++;
935 } while (res);
936
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400937 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300938 if (of_property_read_u32(node, "ti,tshut-gpio", &prop) < 0) {
939 dev_err(&pdev->dev, "missing tshut gpio in device tree\n");
940 return ERR_PTR(-EINVAL);
941 }
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400942 bgp->tshut_gpio = prop;
943 if (!gpio_is_valid(bgp->tshut_gpio)) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300944 dev_err(&pdev->dev, "invalid gpio for tshut (%d)\n",
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400945 bgp->tshut_gpio);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300946 return ERR_PTR(-EINVAL);
947 }
948 }
949
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400950 return bgp;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300951}
952
Eduardo Valentinf91ddfe2013-03-15 09:00:23 -0400953/*** Device driver call backs ***/
954
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300955static
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400956int ti_bandgap_probe(struct platform_device *pdev)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300957{
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400958 struct ti_bandgap *bgp;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300959 int clk_rate, ret = 0, i;
960
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400961 bgp = ti_bandgap_build(pdev);
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400962 if (IS_ERR_OR_NULL(bgp)) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300963 dev_err(&pdev->dev, "failed to fetch platform data\n");
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400964 return PTR_ERR(bgp);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300965 }
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400966 bgp->dev = &pdev->dev;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300967
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400968 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
969 ret = ti_bandgap_tshut_init(bgp, pdev);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300970 if (ret) {
971 dev_err(&pdev->dev,
972 "failed to initialize system tshut IRQ\n");
973 return ret;
974 }
975 }
976
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400977 bgp->fclock = clk_get(NULL, bgp->conf->fclock_name);
978 ret = IS_ERR_OR_NULL(bgp->fclock);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300979 if (ret) {
980 dev_err(&pdev->dev, "failed to request fclock reference\n");
981 goto free_irqs;
982 }
983
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400984 bgp->div_clk = clk_get(NULL, bgp->conf->div_ck_name);
985 ret = IS_ERR_OR_NULL(bgp->div_clk);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300986 if (ret) {
987 dev_err(&pdev->dev,
988 "failed to request div_ts_ck clock ref\n");
989 goto free_irqs;
990 }
991
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400992 for (i = 0; i < bgp->conf->sensor_count; i++) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300993 struct temp_sensor_registers *tsr;
994 u32 val;
995
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400996 tsr = bgp->conf->sensors[i].registers;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300997 /*
998 * check if the efuse has a non-zero value if not
999 * it is an untrimmed sample and the temperatures
1000 * may not be accurate
1001 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001002 val = ti_bandgap_readl(bgp, tsr->bgap_efuse);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001003 if (ret || !val)
1004 dev_info(&pdev->dev,
1005 "Non-trimmed BGAP, Temp not accurate\n");
1006 }
1007
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001008 clk_rate = clk_round_rate(bgp->div_clk,
1009 bgp->conf->sensors[0].ts_data->max_freq);
1010 if (clk_rate < bgp->conf->sensors[0].ts_data->min_freq ||
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001011 clk_rate == 0xffffffff) {
1012 ret = -ENODEV;
1013 dev_err(&pdev->dev, "wrong clock rate (%d)\n", clk_rate);
1014 goto put_clks;
1015 }
1016
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001017 ret = clk_set_rate(bgp->div_clk, clk_rate);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001018 if (ret)
1019 dev_err(&pdev->dev, "Cannot re-set clock rate. Continuing\n");
1020
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001021 bgp->clk_rate = clk_rate;
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001022 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001023 clk_prepare_enable(bgp->fclock);
Radhesh Fadnis6c9c1d62013-02-26 18:53:25 -04001024
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001025
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001026 spin_lock_init(&bgp->lock);
1027 bgp->dev = &pdev->dev;
1028 platform_set_drvdata(pdev, bgp);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001029
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001030 ti_bandgap_power(bgp, true);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001031
1032 /* Set default counter to 1 for now */
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001033 if (TI_BANDGAP_HAS(bgp, COUNTER))
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001034 for (i = 0; i < bgp->conf->sensor_count; i++)
1035 RMW_BITS(bgp, i, bgap_counter, counter_mask, 1);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001036
Eduardo Valentind3c291a2013-03-15 08:59:55 -04001037 /* Set default thresholds for alert and shutdown */
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001038 for (i = 0; i < bgp->conf->sensor_count; i++) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001039 struct temp_sensor_data *ts_data;
1040
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001041 ts_data = bgp->conf->sensors[i].ts_data;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001042
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001043 if (TI_BANDGAP_HAS(bgp, TALERT)) {
Eduardo Valentind3c291a2013-03-15 08:59:55 -04001044 /* Set initial Talert thresholds */
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001045 RMW_BITS(bgp, i, bgap_threshold,
Eduardo Valentind3c291a2013-03-15 08:59:55 -04001046 threshold_tcold_mask, ts_data->t_cold);
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001047 RMW_BITS(bgp, i, bgap_threshold,
Eduardo Valentind3c291a2013-03-15 08:59:55 -04001048 threshold_thot_mask, ts_data->t_hot);
1049 /* Enable the alert events */
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001050 RMW_BITS(bgp, i, bgap_mask_ctrl, mask_hot_mask, 1);
1051 RMW_BITS(bgp, i, bgap_mask_ctrl, mask_cold_mask, 1);
Eduardo Valentind3c291a2013-03-15 08:59:55 -04001052 }
1053
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001054 if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG)) {
Eduardo Valentind3c291a2013-03-15 08:59:55 -04001055 /* Set initial Tshut thresholds */
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001056 RMW_BITS(bgp, i, tshut_threshold,
Eduardo Valentind3c291a2013-03-15 08:59:55 -04001057 tshut_hot_mask, ts_data->tshut_hot);
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001058 RMW_BITS(bgp, i, tshut_threshold,
Eduardo Valentind3c291a2013-03-15 08:59:55 -04001059 tshut_cold_mask, ts_data->tshut_cold);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001060 }
1061 }
1062
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001063 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
1064 ti_bandgap_set_continuous_mode(bgp);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001065
1066 /* Set .250 seconds time as default counter */
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001067 if (TI_BANDGAP_HAS(bgp, COUNTER))
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001068 for (i = 0; i < bgp->conf->sensor_count; i++)
1069 RMW_BITS(bgp, i, bgap_counter, counter_mask,
1070 bgp->clk_rate / 4);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001071
1072 /* Every thing is good? Then expose the sensors */
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001073 for (i = 0; i < bgp->conf->sensor_count; i++) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001074 char *domain;
1075
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001076 if (bgp->conf->sensors[i].register_cooling)
1077 bgp->conf->sensors[i].register_cooling(bgp, i);
Eduardo Valentin04a4d102012-09-11 19:06:55 +03001078
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001079 domain = bgp->conf->sensors[i].domain;
1080 if (bgp->conf->expose_sensor)
1081 bgp->conf->expose_sensor(bgp, i, domain);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001082 }
1083
1084 /*
1085 * Enable the Interrupts once everything is set. Otherwise irq handler
1086 * might be called as soon as it is enabled where as rest of framework
1087 * is still getting initialised.
1088 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001089 if (TI_BANDGAP_HAS(bgp, TALERT)) {
1090 ret = ti_bandgap_talert_init(bgp, pdev);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001091 if (ret) {
1092 dev_err(&pdev->dev, "failed to initialize Talert IRQ\n");
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001093 i = bgp->conf->sensor_count;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001094 goto disable_clk;
1095 }
1096 }
1097
1098 return 0;
1099
1100disable_clk:
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001101 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001102 clk_disable_unprepare(bgp->fclock);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001103put_clks:
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001104 clk_put(bgp->fclock);
1105 clk_put(bgp->div_clk);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001106free_irqs:
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001107 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001108 free_irq(gpio_to_irq(bgp->tshut_gpio), NULL);
1109 gpio_free(bgp->tshut_gpio);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001110 }
1111
1112 return ret;
1113}
1114
1115static
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001116int ti_bandgap_remove(struct platform_device *pdev)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001117{
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001118 struct ti_bandgap *bgp = platform_get_drvdata(pdev);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001119 int i;
1120
1121 /* First thing is to remove sensor interfaces */
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001122 for (i = 0; i < bgp->conf->sensor_count; i++) {
1123 if (bgp->conf->sensors[i].register_cooling)
1124 bgp->conf->sensors[i].unregister_cooling(bgp, i);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001125
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001126 if (bgp->conf->remove_sensor)
1127 bgp->conf->remove_sensor(bgp, i);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001128 }
1129
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001130 ti_bandgap_power(bgp, false);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001131
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001132 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001133 clk_disable_unprepare(bgp->fclock);
1134 clk_put(bgp->fclock);
1135 clk_put(bgp->div_clk);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001136
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001137 if (TI_BANDGAP_HAS(bgp, TALERT))
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001138 free_irq(bgp->irq, bgp);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001139
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001140 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001141 free_irq(gpio_to_irq(bgp->tshut_gpio), NULL);
1142 gpio_free(bgp->tshut_gpio);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001143 }
1144
1145 return 0;
1146}
1147
1148#ifdef CONFIG_PM
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001149static int ti_bandgap_save_ctxt(struct ti_bandgap *bgp)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001150{
1151 int i;
1152
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001153 for (i = 0; i < bgp->conf->sensor_count; i++) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001154 struct temp_sensor_registers *tsr;
1155 struct temp_sensor_regval *rval;
1156
Eduardo Valentin9879b2c2013-03-19 10:54:23 -04001157 rval = &bgp->regval[i];
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001158 tsr = bgp->conf->sensors[i].registers;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001159
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001160 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
1161 rval->bg_mode_ctrl = ti_bandgap_readl(bgp,
J Keerthy76d2cd32012-09-11 19:06:52 +03001162 tsr->bgap_mode_ctrl);
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001163 if (TI_BANDGAP_HAS(bgp, COUNTER))
1164 rval->bg_counter = ti_bandgap_readl(bgp,
J Keerthy76d2cd32012-09-11 19:06:52 +03001165 tsr->bgap_counter);
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001166 if (TI_BANDGAP_HAS(bgp, TALERT)) {
1167 rval->bg_threshold = ti_bandgap_readl(bgp,
J Keerthy76d2cd32012-09-11 19:06:52 +03001168 tsr->bgap_threshold);
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001169 rval->bg_ctrl = ti_bandgap_readl(bgp,
J Keerthy76d2cd32012-09-11 19:06:52 +03001170 tsr->bgap_mask_ctrl);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001171 }
1172
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001173 if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG))
1174 rval->tshut_threshold = ti_bandgap_readl(bgp,
J Keerthy76d2cd32012-09-11 19:06:52 +03001175 tsr->tshut_threshold);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001176 }
1177
1178 return 0;
1179}
1180
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001181static int ti_bandgap_restore_ctxt(struct ti_bandgap *bgp)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001182{
1183 int i;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001184
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001185 for (i = 0; i < bgp->conf->sensor_count; i++) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001186 struct temp_sensor_registers *tsr;
1187 struct temp_sensor_regval *rval;
1188 u32 val = 0;
1189
Eduardo Valentin9879b2c2013-03-19 10:54:23 -04001190 rval = &bgp->regval[i];
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001191 tsr = bgp->conf->sensors[i].registers;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001192
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001193 if (TI_BANDGAP_HAS(bgp, COUNTER))
1194 val = ti_bandgap_readl(bgp, tsr->bgap_counter);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001195
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001196 if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG))
1197 ti_bandgap_writel(bgp, rval->tshut_threshold,
1198 tsr->tshut_threshold);
Radhesh Fadnisb87ea752012-11-13 14:10:04 -04001199 /* Force immediate temperature measurement and update
1200 * of the DTEMP field
1201 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001202 ti_bandgap_force_single_read(bgp, i);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001203
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001204 if (TI_BANDGAP_HAS(bgp, COUNTER))
1205 ti_bandgap_writel(bgp, rval->bg_counter,
1206 tsr->bgap_counter);
1207 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
1208 ti_bandgap_writel(bgp, rval->bg_mode_ctrl,
1209 tsr->bgap_mode_ctrl);
1210 if (TI_BANDGAP_HAS(bgp, TALERT)) {
1211 ti_bandgap_writel(bgp, rval->bg_threshold,
1212 tsr->bgap_threshold);
1213 ti_bandgap_writel(bgp, rval->bg_ctrl,
1214 tsr->bgap_mask_ctrl);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001215 }
1216 }
1217
1218 return 0;
1219}
1220
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001221static int ti_bandgap_suspend(struct device *dev)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001222{
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001223 struct ti_bandgap *bgp = dev_get_drvdata(dev);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001224 int err;
1225
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001226 err = ti_bandgap_save_ctxt(bgp);
1227 ti_bandgap_power(bgp, false);
Radhesh Fadnis6c9c1d62013-02-26 18:53:25 -04001228
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001229 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001230 clk_disable_unprepare(bgp->fclock);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001231
1232 return err;
1233}
1234
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001235static int ti_bandgap_resume(struct device *dev)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001236{
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001237 struct ti_bandgap *bgp = dev_get_drvdata(dev);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001238
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001239 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001240 clk_prepare_enable(bgp->fclock);
Radhesh Fadnis6c9c1d62013-02-26 18:53:25 -04001241
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001242 ti_bandgap_power(bgp, true);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001243
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001244 return ti_bandgap_restore_ctxt(bgp);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001245}
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001246static const struct dev_pm_ops ti_bandgap_dev_pm_ops = {
1247 SET_SYSTEM_SLEEP_PM_OPS(ti_bandgap_suspend,
1248 ti_bandgap_resume)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001249};
1250
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001251#define DEV_PM_OPS (&ti_bandgap_dev_pm_ops)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001252#else
1253#define DEV_PM_OPS NULL
1254#endif
1255
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001256static const struct of_device_id of_ti_bandgap_match[] = {
Eduardo Valentin1a312702012-07-12 19:02:31 +03001257#ifdef CONFIG_OMAP4_THERMAL
1258 {
1259 .compatible = "ti,omap4430-bandgap",
1260 .data = (void *)&omap4430_data,
1261 },
1262 {
1263 .compatible = "ti,omap4460-bandgap",
1264 .data = (void *)&omap4460_data,
1265 },
1266 {
1267 .compatible = "ti,omap4470-bandgap",
1268 .data = (void *)&omap4470_data,
1269 },
1270#endif
Eduardo Valentin949f5a52012-07-12 19:02:32 +03001271#ifdef CONFIG_OMAP5_THERMAL
1272 {
1273 .compatible = "ti,omap5430-bandgap",
1274 .data = (void *)&omap5430_data,
1275 },
1276#endif
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001277 /* Sentinel */
1278 { },
1279};
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001280MODULE_DEVICE_TABLE(of, of_ti_bandgap_match);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001281
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001282static struct platform_driver ti_bandgap_sensor_driver = {
1283 .probe = ti_bandgap_probe,
1284 .remove = ti_bandgap_remove,
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001285 .driver = {
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001286 .name = "ti-soc-thermal",
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001287 .pm = DEV_PM_OPS,
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001288 .of_match_table = of_ti_bandgap_match,
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001289 },
1290};
1291
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001292module_platform_driver(ti_bandgap_sensor_driver);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001293
1294MODULE_DESCRIPTION("OMAP4+ bandgap temperature sensor driver");
1295MODULE_LICENSE("GPL v2");
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001296MODULE_ALIAS("platform:ti-soc-thermal");
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001297MODULE_AUTHOR("Texas Instrument Inc.");