blob: 74c0e3474d6e935a7d16bf29e6304e59b9a01a1f [file] [log] [blame]
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001/*
Eduardo Valentin03e859d2013-03-19 10:54:21 -04002 * TI Bandgap temperature sensor driver
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03003 *
4 * Copyright (C) 2011-2012 Texas Instruments Incorporated - http://www.ti.com/
5 * Author: J Keerthy <j-keerthy@ti.com>
6 * Author: Moiz Sonasath <m-sonasath@ti.com>
7 * Couple of fixes, DT and MFD adaptation:
8 * Eduardo Valentin <eduardo.valentin@ti.com>
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * version 2 as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22 * 02110-1301 USA
23 *
24 */
25
26#include <linux/module.h>
27#include <linux/export.h>
28#include <linux/init.h>
29#include <linux/kernel.h>
30#include <linux/interrupt.h>
31#include <linux/clk.h>
32#include <linux/gpio.h>
33#include <linux/platform_device.h>
34#include <linux/err.h>
35#include <linux/types.h>
Eduardo Valentinebf0bd52013-03-15 09:00:35 -040036#include <linux/spinlock.h>
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +030037#include <linux/reboot.h>
38#include <linux/of_device.h>
39#include <linux/of_platform.h>
40#include <linux/of_irq.h>
Eduardo Valentin57d16172013-06-07 11:11:53 -040041#include <linux/of_gpio.h>
Eduardo Valentin2aeeb8a2012-11-13 14:10:00 -040042#include <linux/io.h>
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +030043
Eduardo Valentin7372add2013-03-19 10:54:19 -040044#include "ti-bandgap.h"
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +030045
Eduardo Valentin8abbe712013-03-15 09:00:05 -040046/*** Helper functions to access registers and their bitfields ***/
47
Eduardo Valentin9c468aa2013-03-15 08:59:56 -040048/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -040049 * ti_bandgap_readl() - simple read helper function
50 * @bgp: pointer to ti_bandgap structure
Eduardo Valentin9c468aa2013-03-15 08:59:56 -040051 * @reg: desired register (offset) to be read
52 *
53 * Helper function to read bandgap registers. It uses the io remapped area.
Nishanth Menon169e8d02013-04-01 12:04:34 -040054 * Return: the register value.
Eduardo Valentin9c468aa2013-03-15 08:59:56 -040055 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -040056static u32 ti_bandgap_readl(struct ti_bandgap *bgp, u32 reg)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +030057{
Eduardo Valentind7f080e2013-03-19 10:54:18 -040058 return readl(bgp->base + reg);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +030059}
60
Eduardo Valentin9c468aa2013-03-15 08:59:56 -040061/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -040062 * ti_bandgap_writel() - simple write helper function
63 * @bgp: pointer to ti_bandgap structure
Eduardo Valentin9c468aa2013-03-15 08:59:56 -040064 * @val: desired register value to be written
65 * @reg: desired register (offset) to be written
66 *
67 * Helper function to write bandgap registers. It uses the io remapped area.
68 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -040069static void ti_bandgap_writel(struct ti_bandgap *bgp, u32 val, u32 reg)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +030070{
Eduardo Valentind7f080e2013-03-19 10:54:18 -040071 writel(val, bgp->base + reg);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +030072}
73
Eduardo Valentin9c468aa2013-03-15 08:59:56 -040074/**
75 * DOC: macro to update bits.
76 *
77 * RMW_BITS() - used to read, modify and update bandgap bitfields.
78 * The value passed will be shifted.
79 */
Eduardo Valentind7f080e2013-03-19 10:54:18 -040080#define RMW_BITS(bgp, id, reg, mask, val) \
Eduardo Valentind3c291a2013-03-15 08:59:55 -040081do { \
82 struct temp_sensor_registers *t; \
83 u32 r; \
84 \
Eduardo Valentind7f080e2013-03-19 10:54:18 -040085 t = bgp->conf->sensors[(id)].registers; \
Eduardo Valentin03e859d2013-03-19 10:54:21 -040086 r = ti_bandgap_readl(bgp, t->reg); \
Eduardo Valentind3c291a2013-03-15 08:59:55 -040087 r &= ~t->mask; \
88 r |= (val) << __ffs(t->mask); \
Eduardo Valentin03e859d2013-03-19 10:54:21 -040089 ti_bandgap_writel(bgp, r, t->reg); \
Eduardo Valentind3c291a2013-03-15 08:59:55 -040090} while (0)
91
Eduardo Valentin6ab52402013-03-15 09:00:06 -040092/*** Basic helper functions ***/
93
Eduardo Valentin7a556e62013-03-15 08:59:58 -040094/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -040095 * ti_bandgap_power() - controls the power state of a bandgap device
96 * @bgp: pointer to ti_bandgap structure
Eduardo Valentin7a556e62013-03-15 08:59:58 -040097 * @on: desired power state (1 - on, 0 - off)
98 *
99 * Used to power on/off a bandgap device instance. Only used on those
100 * that features tempsoff bit.
Nishanth Menon169e8d02013-04-01 12:04:34 -0400101 *
102 * Return: 0 on success, -ENOTSUPP if tempsoff is not supported.
Eduardo Valentin7a556e62013-03-15 08:59:58 -0400103 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400104static int ti_bandgap_power(struct ti_bandgap *bgp, bool on)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300105{
Eduardo Valentin422a3062013-04-01 12:04:33 -0400106 int i, ret = 0;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300107
Eduardo Valentin422a3062013-04-01 12:04:33 -0400108 if (!TI_BANDGAP_HAS(bgp, POWER_SWITCH)) {
109 ret = -ENOTSUPP;
Eduardo Valentin3d84e522013-03-15 08:59:57 -0400110 goto exit;
Eduardo Valentin422a3062013-04-01 12:04:33 -0400111 }
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300112
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400113 for (i = 0; i < bgp->conf->sensor_count; i++)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300114 /* active on 0 */
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400115 RMW_BITS(bgp, i, temp_sensor_ctrl, bgap_tempsoff_mask, !on);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300116
Eduardo Valentin3d84e522013-03-15 08:59:57 -0400117exit:
Eduardo Valentin422a3062013-04-01 12:04:33 -0400118 return ret;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300119}
120
Eduardo Valentin4a6554e2013-03-15 08:59:59 -0400121/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400122 * ti_bandgap_read_temp() - helper function to read sensor temperature
123 * @bgp: pointer to ti_bandgap structure
Eduardo Valentin4a6554e2013-03-15 08:59:59 -0400124 * @id: bandgap sensor id
125 *
126 * Function to concentrate the steps to read sensor temperature register.
127 * This function is desired because, depending on bandgap device version,
128 * it might be needed to freeze the bandgap state machine, before fetching
129 * the register value.
Nishanth Menon169e8d02013-04-01 12:04:34 -0400130 *
131 * Return: temperature in ADC values.
Eduardo Valentin4a6554e2013-03-15 08:59:59 -0400132 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400133static u32 ti_bandgap_read_temp(struct ti_bandgap *bgp, int id)
Eduardo Valentin194a54f2013-02-26 18:53:33 -0400134{
135 struct temp_sensor_registers *tsr;
Eduardo Valentind3c291a2013-03-15 08:59:55 -0400136 u32 temp, reg;
Eduardo Valentin194a54f2013-02-26 18:53:33 -0400137
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400138 tsr = bgp->conf->sensors[id].registers;
Eduardo Valentin194a54f2013-02-26 18:53:33 -0400139 reg = tsr->temp_sensor_ctrl;
140
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400141 if (TI_BANDGAP_HAS(bgp, FREEZE_BIT)) {
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400142 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 1);
Eduardo Valentin194a54f2013-02-26 18:53:33 -0400143 /*
144 * In case we cannot read from cur_dtemp / dtemp_0,
145 * then we read from the last valid temp read
146 */
147 reg = tsr->ctrl_dtemp_1;
148 }
149
150 /* read temperature */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400151 temp = ti_bandgap_readl(bgp, reg);
Eduardo Valentin194a54f2013-02-26 18:53:33 -0400152 temp &= tsr->bgap_dtemp_mask;
153
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400154 if (TI_BANDGAP_HAS(bgp, FREEZE_BIT))
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400155 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 0);
Eduardo Valentin194a54f2013-02-26 18:53:33 -0400156
157 return temp;
158}
159
Eduardo Valentinfb65b882013-03-15 09:00:07 -0400160/*** IRQ handlers ***/
161
Eduardo Valentinee07d552013-03-15 09:00:01 -0400162/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400163 * ti_bandgap_talert_irq_handler() - handles Temperature alert IRQs
Eduardo Valentinee07d552013-03-15 09:00:01 -0400164 * @irq: IRQ number
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400165 * @data: private data (struct ti_bandgap *)
Eduardo Valentinee07d552013-03-15 09:00:01 -0400166 *
167 * This is the Talert handler. Use it only if bandgap device features
168 * HAS(TALERT). This handler goes over all sensors and checks their
169 * conditions and acts accordingly. In case there are events pending,
170 * it will reset the event mask to wait for the opposite event (next event).
171 * Every time there is a new event, it will be reported to thermal layer.
Nishanth Menon169e8d02013-04-01 12:04:34 -0400172 *
173 * Return: IRQ_HANDLED
Eduardo Valentinee07d552013-03-15 09:00:01 -0400174 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400175static irqreturn_t ti_bandgap_talert_irq_handler(int irq, void *data)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300176{
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400177 struct ti_bandgap *bgp = data;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300178 struct temp_sensor_registers *tsr;
Eduardo Valentin194a54f2013-02-26 18:53:33 -0400179 u32 t_hot = 0, t_cold = 0, ctrl;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300180 int i;
181
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400182 spin_lock(&bgp->lock);
183 for (i = 0; i < bgp->conf->sensor_count; i++) {
184 tsr = bgp->conf->sensors[i].registers;
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400185 ctrl = ti_bandgap_readl(bgp, tsr->bgap_status);
Eduardo Valentine555c952013-03-15 09:00:04 -0400186
187 /* Read the status of t_hot */
188 t_hot = ctrl & tsr->status_hot_mask;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300189
190 /* Read the status of t_cold */
Eduardo Valentine555c952013-03-15 09:00:04 -0400191 t_cold = ctrl & tsr->status_cold_mask;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300192
193 if (!t_cold && !t_hot)
194 continue;
195
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400196 ctrl = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300197 /*
198 * One TALERT interrupt: Two sources
199 * If the interrupt is due to t_hot then mask t_hot and
200 * and unmask t_cold else mask t_cold and unmask t_hot
201 */
202 if (t_hot) {
203 ctrl &= ~tsr->mask_hot_mask;
204 ctrl |= tsr->mask_cold_mask;
205 } else if (t_cold) {
206 ctrl &= ~tsr->mask_cold_mask;
207 ctrl |= tsr->mask_hot_mask;
208 }
209
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400210 ti_bandgap_writel(bgp, ctrl, tsr->bgap_mask_ctrl);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300211
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400212 dev_dbg(bgp->dev,
Eduardo Valentin71e303f2012-11-13 14:10:03 -0400213 "%s: IRQ from %s sensor: hotevent %d coldevent %d\n",
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400214 __func__, bgp->conf->sensors[i].domain,
Eduardo Valentin71e303f2012-11-13 14:10:03 -0400215 t_hot, t_cold);
216
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300217 /* report temperature to whom may concern */
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400218 if (bgp->conf->report_temperature)
219 bgp->conf->report_temperature(bgp, i);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300220 }
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400221 spin_unlock(&bgp->lock);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300222
223 return IRQ_HANDLED;
224}
225
Eduardo Valentin79857cd22013-03-15 09:00:02 -0400226/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400227 * ti_bandgap_tshut_irq_handler() - handles Temperature shutdown signal
Eduardo Valentin79857cd22013-03-15 09:00:02 -0400228 * @irq: IRQ number
229 * @data: private data (unused)
230 *
231 * This is the Tshut handler. Use it only if bandgap device features
232 * HAS(TSHUT). If any sensor fires the Tshut signal, we simply shutdown
233 * the system.
Nishanth Menon169e8d02013-04-01 12:04:34 -0400234 *
235 * Return: IRQ_HANDLED
Eduardo Valentin79857cd22013-03-15 09:00:02 -0400236 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400237static irqreturn_t ti_bandgap_tshut_irq_handler(int irq, void *data)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300238{
Ruslan Ruslichenkob3bf0e92013-02-26 18:53:24 -0400239 pr_emerg("%s: TSHUT temperature reached. Needs shut down...\n",
240 __func__);
241
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300242 orderly_poweroff(true);
243
244 return IRQ_HANDLED;
245}
246
Eduardo Valentin2f6af4b2013-03-15 09:00:08 -0400247/*** Helper functions which manipulate conversion ADC <-> mi Celsius ***/
248
Eduardo Valentin2577e932013-03-15 09:00:11 -0400249/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400250 * ti_bandgap_adc_to_mcelsius() - converts an ADC value to mCelsius scale
251 * @bgp: struct ti_bandgap pointer
Eduardo Valentin2577e932013-03-15 09:00:11 -0400252 * @adc_val: value in ADC representation
253 * @t: address where to write the resulting temperature in mCelsius
254 *
255 * Simple conversion from ADC representation to mCelsius. In case the ADC value
256 * is out of the ADC conv table range, it returns -ERANGE, 0 on success.
257 * The conversion table is indexed by the ADC values.
Nishanth Menon169e8d02013-04-01 12:04:34 -0400258 *
259 * Return: 0 if conversion was successful, else -ERANGE in case the @adc_val
260 * argument is out of the ADC conv table range.
Eduardo Valentin2577e932013-03-15 09:00:11 -0400261 */
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300262static
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400263int ti_bandgap_adc_to_mcelsius(struct ti_bandgap *bgp, int adc_val, int *t)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300264{
Eduardo Valentin9879b2c2013-03-19 10:54:23 -0400265 const struct ti_bandgap_data *conf = bgp->conf;
Eduardo Valentin20470592013-03-15 09:00:10 -0400266 int ret = 0;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300267
268 /* look up for temperature in the table and return the temperature */
Eduardo Valentin26a70ed2013-03-15 09:00:14 -0400269 if (adc_val < conf->adc_start_val || adc_val > conf->adc_end_val) {
Eduardo Valentin20470592013-03-15 09:00:10 -0400270 ret = -ERANGE;
271 goto exit;
272 }
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300273
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400274 *t = bgp->conf->conv_table[adc_val - conf->adc_start_val];
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300275
Eduardo Valentin20470592013-03-15 09:00:10 -0400276exit:
277 return ret;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300278}
279
Eduardo Valentine7f60b52013-03-15 09:00:15 -0400280/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400281 * ti_bandgap_mcelsius_to_adc() - converts a mCelsius value to ADC scale
282 * @bgp: struct ti_bandgap pointer
Eduardo Valentine7f60b52013-03-15 09:00:15 -0400283 * @temp: value in mCelsius
284 * @adc: address where to write the resulting temperature in ADC representation
285 *
286 * Simple conversion from mCelsius to ADC values. In case the temp value
287 * is out of the ADC conv table range, it returns -ERANGE, 0 on success.
288 * The conversion table is indexed by the ADC values.
Nishanth Menon169e8d02013-04-01 12:04:34 -0400289 *
290 * Return: 0 if conversion was successful, else -ERANGE in case the @temp
291 * argument is out of the ADC conv table range.
Eduardo Valentine7f60b52013-03-15 09:00:15 -0400292 */
Eduardo Valentine16f0722013-03-15 09:00:12 -0400293static
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400294int ti_bandgap_mcelsius_to_adc(struct ti_bandgap *bgp, long temp, int *adc)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300295{
Eduardo Valentin9879b2c2013-03-19 10:54:23 -0400296 const struct ti_bandgap_data *conf = bgp->conf;
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400297 const int *conv_table = bgp->conf->conv_table;
Eduardo Valentina6194772013-03-15 09:00:13 -0400298 int high, low, mid, ret = 0;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300299
300 low = 0;
Eduardo Valentin26a70ed2013-03-15 09:00:14 -0400301 high = conf->adc_end_val - conf->adc_start_val;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300302 mid = (high + low) / 2;
303
Eduardo Valentina6194772013-03-15 09:00:13 -0400304 if (temp < conv_table[low] || temp > conv_table[high]) {
305 ret = -ERANGE;
306 goto exit;
307 }
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300308
309 while (low < high) {
Eduardo Valentinc8a8f842013-02-26 18:53:36 -0400310 if (temp < conv_table[mid])
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300311 high = mid - 1;
312 else
313 low = mid + 1;
314 mid = (low + high) / 2;
315 }
316
Eduardo Valentin26a70ed2013-03-15 09:00:14 -0400317 *adc = conf->adc_start_val + low;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300318
Eduardo Valentina6194772013-03-15 09:00:13 -0400319exit:
320 return ret;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300321}
322
Eduardo Valentin8a1cefe2013-03-15 09:00:17 -0400323/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400324 * ti_bandgap_add_hyst() - add hysteresis (in mCelsius) to an ADC value
325 * @bgp: struct ti_bandgap pointer
Eduardo Valentin8a1cefe2013-03-15 09:00:17 -0400326 * @adc_val: temperature value in ADC representation
327 * @hyst_val: hysteresis value in mCelsius
328 * @sum: address where to write the resulting temperature (in ADC scale)
329 *
330 * Adds an hysteresis value (in mCelsius) to a ADC temperature value.
Nishanth Menon169e8d02013-04-01 12:04:34 -0400331 *
332 * Return: 0 on success, -ERANGE otherwise.
Eduardo Valentin8a1cefe2013-03-15 09:00:17 -0400333 */
Eduardo Valentin0f0ed7d2013-03-15 09:00:16 -0400334static
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400335int ti_bandgap_add_hyst(struct ti_bandgap *bgp, int adc_val, int hyst_val,
336 u32 *sum)
Eduardo Valentin0f0ed7d2013-03-15 09:00:16 -0400337{
338 int temp, ret;
339
340 /*
341 * Need to add in the mcelsius domain, so we have a temperature
342 * the conv_table range
343 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400344 ret = ti_bandgap_adc_to_mcelsius(bgp, adc_val, &temp);
Eduardo Valentin0f0ed7d2013-03-15 09:00:16 -0400345 if (ret < 0)
346 goto exit;
347
348 temp += hyst_val;
349
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400350 ret = ti_bandgap_mcelsius_to_adc(bgp, temp, sum);
Eduardo Valentin0f0ed7d2013-03-15 09:00:16 -0400351
352exit:
353 return ret;
354}
355
Eduardo Valentinf8ccce22013-03-15 09:00:18 -0400356/*** Helper functions handling device Alert/Shutdown signals ***/
357
Eduardo Valentinf47f6d32013-03-15 09:00:20 -0400358/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400359 * ti_bandgap_unmask_interrupts() - unmasks the events of thot & tcold
360 * @bgp: struct ti_bandgap pointer
Eduardo Valentin61603af2013-03-19 10:54:25 -0400361 * @id: bandgap sensor id
Eduardo Valentinf47f6d32013-03-15 09:00:20 -0400362 * @t_hot: hot temperature value to trigger alert signal
363 * @t_cold: cold temperature value to trigger alert signal
364 *
365 * Checks the requested t_hot and t_cold values and configures the IRQ event
366 * masks accordingly. Call this function only if bandgap features HAS(TALERT).
367 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400368static void ti_bandgap_unmask_interrupts(struct ti_bandgap *bgp, int id,
369 u32 t_hot, u32 t_cold)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300370{
371 struct temp_sensor_registers *tsr;
372 u32 temp, reg_val;
373
374 /* Read the current on die temperature */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400375 temp = ti_bandgap_read_temp(bgp, id);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300376
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400377 tsr = bgp->conf->sensors[id].registers;
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400378 reg_val = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl);
Eduardo Valentin194a54f2013-02-26 18:53:33 -0400379
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300380 if (temp < t_hot)
381 reg_val |= tsr->mask_hot_mask;
382 else
383 reg_val &= ~tsr->mask_hot_mask;
384
385 if (t_cold < temp)
386 reg_val |= tsr->mask_cold_mask;
387 else
388 reg_val &= ~tsr->mask_cold_mask;
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400389 ti_bandgap_writel(bgp, reg_val, tsr->bgap_mask_ctrl);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300390}
391
Eduardo Valentin38d99e82013-03-15 09:00:27 -0400392/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400393 * ti_bandgap_update_alert_threshold() - sequence to update thresholds
394 * @bgp: struct ti_bandgap pointer
Eduardo Valentin38d99e82013-03-15 09:00:27 -0400395 * @id: bandgap sensor id
396 * @val: value (ADC) of a new threshold
397 * @hot: desired threshold to be updated. true if threshold hot, false if
398 * threshold cold
399 *
400 * It will program the required thresholds (hot and cold) for TALERT signal.
401 * This function can be used to update t_hot or t_cold, depending on @hot value.
402 * It checks the resulting t_hot and t_cold values, based on the new passed @val
403 * and configures the thresholds so that t_hot is always greater than t_cold.
404 * Call this function only if bandgap features HAS(TALERT).
Nishanth Menon169e8d02013-04-01 12:04:34 -0400405 *
406 * Return: 0 if no error, else corresponding error
Eduardo Valentin38d99e82013-03-15 09:00:27 -0400407 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400408static int ti_bandgap_update_alert_threshold(struct ti_bandgap *bgp, int id,
409 int val, bool hot)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300410{
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400411 struct temp_sensor_data *ts_data = bgp->conf->sensors[id].ts_data;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300412 struct temp_sensor_registers *tsr;
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400413 u32 thresh_val, reg_val, t_hot, t_cold;
414 int err = 0;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300415
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400416 tsr = bgp->conf->sensors[id].registers;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300417
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400418 /* obtain the current value */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400419 thresh_val = ti_bandgap_readl(bgp, tsr->bgap_threshold);
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400420 t_cold = (thresh_val & tsr->threshold_tcold_mask) >>
421 __ffs(tsr->threshold_tcold_mask);
422 t_hot = (thresh_val & tsr->threshold_thot_mask) >>
423 __ffs(tsr->threshold_thot_mask);
424 if (hot)
425 t_hot = val;
426 else
427 t_cold = val;
428
Eduardo Valentinf5d43b72013-03-19 10:54:26 -0400429 if (t_cold > t_hot) {
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400430 if (hot)
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400431 err = ti_bandgap_add_hyst(bgp, t_hot,
432 -ts_data->hyst_val,
433 &t_cold);
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400434 else
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400435 err = ti_bandgap_add_hyst(bgp, t_cold,
436 ts_data->hyst_val,
437 &t_hot);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300438 }
439
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400440 /* write the new threshold values */
Eduardo Valentin0fb3c242013-03-19 10:54:27 -0400441 reg_val = thresh_val &
442 ~(tsr->threshold_thot_mask | tsr->threshold_tcold_mask);
443 reg_val |= (t_hot << __ffs(tsr->threshold_thot_mask)) |
444 (t_cold << __ffs(tsr->threshold_tcold_mask));
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400445 ti_bandgap_writel(bgp, reg_val, tsr->bgap_threshold);
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400446
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300447 if (err) {
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400448 dev_err(bgp->dev, "failed to reprogram thot threshold\n");
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400449 err = -EIO;
450 goto exit;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300451 }
452
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400453 ti_bandgap_unmask_interrupts(bgp, id, t_hot, t_cold);
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400454exit:
455 return err;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300456}
457
Eduardo Valentine72b7bb2013-03-15 09:00:38 -0400458/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400459 * ti_bandgap_validate() - helper to check the sanity of a struct ti_bandgap
460 * @bgp: struct ti_bandgap pointer
Eduardo Valentine72b7bb2013-03-15 09:00:38 -0400461 * @id: bandgap sensor id
462 *
463 * Checks if the bandgap pointer is valid and if the sensor id is also
464 * applicable.
Nishanth Menon169e8d02013-04-01 12:04:34 -0400465 *
466 * Return: 0 if no errors, -EINVAL for invalid @bgp pointer or -ERANGE if
467 * @id cannot index @bgp sensors.
Eduardo Valentine72b7bb2013-03-15 09:00:38 -0400468 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400469static inline int ti_bandgap_validate(struct ti_bandgap *bgp, int id)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300470{
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400471 int ret = 0;
472
Eduardo Valentin0c12b5a2013-05-29 15:07:43 +0000473 if (!bgp || IS_ERR(bgp)) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300474 pr_err("%s: invalid bandgap pointer\n", __func__);
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400475 ret = -EINVAL;
476 goto exit;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300477 }
478
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400479 if ((id < 0) || (id >= bgp->conf->sensor_count)) {
480 dev_err(bgp->dev, "%s: sensor id out of range (%d)\n",
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300481 __func__, id);
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400482 ret = -ERANGE;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300483 }
484
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400485exit:
486 return ret;
487}
488
Eduardo Valentin9efa93b2013-03-15 09:00:28 -0400489/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400490 * _ti_bandgap_write_threshold() - helper to update TALERT t_cold or t_hot
491 * @bgp: struct ti_bandgap pointer
Eduardo Valentin9efa93b2013-03-15 09:00:28 -0400492 * @id: bandgap sensor id
493 * @val: value (mCelsius) of a new threshold
494 * @hot: desired threshold to be updated. true if threshold hot, false if
495 * threshold cold
496 *
497 * It will update the required thresholds (hot and cold) for TALERT signal.
498 * This function can be used to update t_hot or t_cold, depending on @hot value.
499 * Validates the mCelsius range and update the requested threshold.
500 * Call this function only if bandgap features HAS(TALERT).
Nishanth Menon169e8d02013-04-01 12:04:34 -0400501 *
502 * Return: 0 if no error, else corresponding error value.
Eduardo Valentin9efa93b2013-03-15 09:00:28 -0400503 */
Eduardo Valentin2f8ec2a2013-03-19 10:54:22 -0400504static int _ti_bandgap_write_threshold(struct ti_bandgap *bgp, int id, int val,
505 bool hot)
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400506{
507 struct temp_sensor_data *ts_data;
508 struct temp_sensor_registers *tsr;
509 u32 adc_val;
510 int ret;
511
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400512 ret = ti_bandgap_validate(bgp, id);
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400513 if (ret)
514 goto exit;
515
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400516 if (!TI_BANDGAP_HAS(bgp, TALERT)) {
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400517 ret = -ENOTSUPP;
518 goto exit;
519 }
520
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400521 ts_data = bgp->conf->sensors[id].ts_data;
522 tsr = bgp->conf->sensors[id].registers;
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400523 if (hot) {
524 if (val < ts_data->min_temp + ts_data->hyst_val)
525 ret = -EINVAL;
526 } else {
527 if (val > ts_data->max_temp + ts_data->hyst_val)
528 ret = -EINVAL;
529 }
530
531 if (ret)
532 goto exit;
533
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400534 ret = ti_bandgap_mcelsius_to_adc(bgp, val, &adc_val);
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400535 if (ret < 0)
536 goto exit;
537
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400538 spin_lock(&bgp->lock);
Eduardo Valentind52361c2013-03-19 10:54:28 -0400539 ret = ti_bandgap_update_alert_threshold(bgp, id, adc_val, hot);
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400540 spin_unlock(&bgp->lock);
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400541
542exit:
543 return ret;
544}
545
Eduardo Valentin7a681a52013-03-15 09:00:29 -0400546/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400547 * _ti_bandgap_read_threshold() - helper to read TALERT t_cold or t_hot
548 * @bgp: struct ti_bandgap pointer
Eduardo Valentin7a681a52013-03-15 09:00:29 -0400549 * @id: bandgap sensor id
550 * @val: value (mCelsius) of a threshold
551 * @hot: desired threshold to be read. true if threshold hot, false if
552 * threshold cold
553 *
554 * It will fetch the required thresholds (hot and cold) for TALERT signal.
555 * This function can be used to read t_hot or t_cold, depending on @hot value.
556 * Call this function only if bandgap features HAS(TALERT).
Nishanth Menon169e8d02013-04-01 12:04:34 -0400557 *
558 * Return: 0 if no error, -ENOTSUPP if it has no TALERT support, or the
559 * corresponding error value if some operation fails.
Eduardo Valentin7a681a52013-03-15 09:00:29 -0400560 */
Eduardo Valentin2f8ec2a2013-03-19 10:54:22 -0400561static int _ti_bandgap_read_threshold(struct ti_bandgap *bgp, int id,
562 int *val, bool hot)
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400563{
564 struct temp_sensor_registers *tsr;
565 u32 temp, mask;
566 int ret = 0;
567
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400568 ret = ti_bandgap_validate(bgp, id);
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400569 if (ret)
570 goto exit;
571
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400572 if (!TI_BANDGAP_HAS(bgp, TALERT)) {
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400573 ret = -ENOTSUPP;
574 goto exit;
575 }
576
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400577 tsr = bgp->conf->sensors[id].registers;
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400578 if (hot)
579 mask = tsr->threshold_thot_mask;
580 else
581 mask = tsr->threshold_tcold_mask;
582
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400583 temp = ti_bandgap_readl(bgp, tsr->bgap_threshold);
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400584 temp = (temp & mask) >> __ffs(mask);
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400585 ret |= ti_bandgap_adc_to_mcelsius(bgp, temp, &temp);
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400586 if (ret) {
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400587 dev_err(bgp->dev, "failed to read thot\n");
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400588 ret = -EIO;
589 goto exit;
590 }
591
592 *val = temp;
593
594exit:
Eduardo Valentin648b4c62013-03-19 10:54:17 -0400595 return ret;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300596}
597
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400598/*** Exposed APIs ***/
599
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300600/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400601 * ti_bandgap_read_thot() - reads sensor current thot
Eduardo Valentin61603af2013-03-19 10:54:25 -0400602 * @bgp: pointer to bandgap instance
603 * @id: sensor id
604 * @thot: resulting current thot value
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300605 *
Nishanth Menon169e8d02013-04-01 12:04:34 -0400606 * Return: 0 on success or the proper error code
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300607 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400608int ti_bandgap_read_thot(struct ti_bandgap *bgp, int id, int *thot)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300609{
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400610 return _ti_bandgap_read_threshold(bgp, id, thot, true);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300611}
612
613/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400614 * ti_bandgap_write_thot() - sets sensor current thot
Eduardo Valentin61603af2013-03-19 10:54:25 -0400615 * @bgp: pointer to bandgap instance
616 * @id: sensor id
617 * @val: desired thot value
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300618 *
Nishanth Menon169e8d02013-04-01 12:04:34 -0400619 * Return: 0 on success or the proper error code
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300620 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400621int ti_bandgap_write_thot(struct ti_bandgap *bgp, int id, int val)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300622{
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400623 return _ti_bandgap_write_threshold(bgp, id, val, true);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300624}
625
626/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400627 * ti_bandgap_read_tcold() - reads sensor current tcold
Eduardo Valentin61603af2013-03-19 10:54:25 -0400628 * @bgp: pointer to bandgap instance
629 * @id: sensor id
630 * @tcold: resulting current tcold value
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300631 *
Nishanth Menon169e8d02013-04-01 12:04:34 -0400632 * Return: 0 on success or the proper error code
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300633 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400634int ti_bandgap_read_tcold(struct ti_bandgap *bgp, int id, int *tcold)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300635{
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400636 return _ti_bandgap_read_threshold(bgp, id, tcold, false);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300637}
638
639/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400640 * ti_bandgap_write_tcold() - sets the sensor tcold
Eduardo Valentin61603af2013-03-19 10:54:25 -0400641 * @bgp: pointer to bandgap instance
642 * @id: sensor id
643 * @val: desired tcold value
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300644 *
Nishanth Menon169e8d02013-04-01 12:04:34 -0400645 * Return: 0 on success or the proper error code
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300646 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400647int ti_bandgap_write_tcold(struct ti_bandgap *bgp, int id, int val)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300648{
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400649 return _ti_bandgap_write_threshold(bgp, id, val, false);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300650}
651
652/**
J Keerthy58bccd02013-04-01 12:04:42 -0400653 * ti_bandgap_read_counter() - read the sensor counter
654 * @bgp: pointer to bandgap instance
655 * @id: sensor id
656 * @interval: resulting update interval in miliseconds
657 */
658static void ti_bandgap_read_counter(struct ti_bandgap *bgp, int id,
659 int *interval)
660{
661 struct temp_sensor_registers *tsr;
662 int time;
663
664 tsr = bgp->conf->sensors[id].registers;
665 time = ti_bandgap_readl(bgp, tsr->bgap_counter);
666 time = (time & tsr->counter_mask) >>
667 __ffs(tsr->counter_mask);
668 time = time * 1000 / bgp->clk_rate;
669 *interval = time;
670}
671
672/**
673 * ti_bandgap_read_counter_delay() - read the sensor counter delay
674 * @bgp: pointer to bandgap instance
675 * @id: sensor id
676 * @interval: resulting update interval in miliseconds
677 */
678static void ti_bandgap_read_counter_delay(struct ti_bandgap *bgp, int id,
679 int *interval)
680{
681 struct temp_sensor_registers *tsr;
682 int reg_val;
683
684 tsr = bgp->conf->sensors[id].registers;
685
686 reg_val = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl);
687 reg_val = (reg_val & tsr->mask_counter_delay_mask) >>
688 __ffs(tsr->mask_counter_delay_mask);
689 switch (reg_val) {
690 case 0:
691 *interval = 0;
692 break;
693 case 1:
694 *interval = 1;
695 break;
696 case 2:
697 *interval = 10;
698 break;
699 case 3:
700 *interval = 100;
701 break;
702 case 4:
703 *interval = 250;
704 break;
705 case 5:
706 *interval = 500;
707 break;
708 default:
709 dev_warn(bgp->dev, "Wrong counter delay value read from register %X",
710 reg_val);
711 }
712}
713
714/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400715 * ti_bandgap_read_update_interval() - read the sensor update interval
Eduardo Valentin61603af2013-03-19 10:54:25 -0400716 * @bgp: pointer to bandgap instance
717 * @id: sensor id
718 * @interval: resulting update interval in miliseconds
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300719 *
Nishanth Menon169e8d02013-04-01 12:04:34 -0400720 * Return: 0 on success or the proper error code
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300721 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400722int ti_bandgap_read_update_interval(struct ti_bandgap *bgp, int id,
723 int *interval)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300724{
J Keerthy58bccd02013-04-01 12:04:42 -0400725 int ret = 0;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300726
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400727 ret = ti_bandgap_validate(bgp, id);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300728 if (ret)
J Keerthy58bccd02013-04-01 12:04:42 -0400729 goto exit;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300730
J Keerthy58bccd02013-04-01 12:04:42 -0400731 if (!TI_BANDGAP_HAS(bgp, COUNTER) &&
732 !TI_BANDGAP_HAS(bgp, COUNTER_DELAY)) {
733 ret = -ENOTSUPP;
734 goto exit;
735 }
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300736
J Keerthy58bccd02013-04-01 12:04:42 -0400737 if (TI_BANDGAP_HAS(bgp, COUNTER)) {
738 ti_bandgap_read_counter(bgp, id, interval);
739 goto exit;
740 }
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300741
J Keerthy58bccd02013-04-01 12:04:42 -0400742 ti_bandgap_read_counter_delay(bgp, id, interval);
743exit:
744 return ret;
745}
746
747/**
748 * ti_bandgap_write_counter_delay() - set the counter_delay
749 * @bgp: pointer to bandgap instance
750 * @id: sensor id
751 * @interval: desired update interval in miliseconds
752 *
753 * Return: 0 on success or the proper error code
754 */
755static int ti_bandgap_write_counter_delay(struct ti_bandgap *bgp, int id,
756 u32 interval)
757{
758 int rval;
759
760 switch (interval) {
761 case 0: /* Immediate conversion */
762 rval = 0x0;
763 break;
764 case 1: /* Conversion after ever 1ms */
765 rval = 0x1;
766 break;
767 case 10: /* Conversion after ever 10ms */
768 rval = 0x2;
769 break;
770 case 100: /* Conversion after ever 100ms */
771 rval = 0x3;
772 break;
773 case 250: /* Conversion after ever 250ms */
774 rval = 0x4;
775 break;
776 case 500: /* Conversion after ever 500ms */
777 rval = 0x5;
778 break;
779 default:
780 dev_warn(bgp->dev, "Delay %d ms is not supported\n", interval);
781 return -EINVAL;
782 }
783
784 spin_lock(&bgp->lock);
785 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_counter_delay_mask, rval);
786 spin_unlock(&bgp->lock);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300787
788 return 0;
789}
790
791/**
J Keerthy58bccd02013-04-01 12:04:42 -0400792 * ti_bandgap_write_counter() - set the bandgap sensor counter
793 * @bgp: pointer to bandgap instance
794 * @id: sensor id
795 * @interval: desired update interval in miliseconds
796 */
797static void ti_bandgap_write_counter(struct ti_bandgap *bgp, int id,
798 u32 interval)
799{
800 interval = interval * bgp->clk_rate / 1000;
801 spin_lock(&bgp->lock);
802 RMW_BITS(bgp, id, bgap_counter, counter_mask, interval);
803 spin_unlock(&bgp->lock);
804}
805
806/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400807 * ti_bandgap_write_update_interval() - set the update interval
Eduardo Valentin61603af2013-03-19 10:54:25 -0400808 * @bgp: pointer to bandgap instance
809 * @id: sensor id
810 * @interval: desired update interval in miliseconds
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300811 *
Nishanth Menon169e8d02013-04-01 12:04:34 -0400812 * Return: 0 on success or the proper error code
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300813 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400814int ti_bandgap_write_update_interval(struct ti_bandgap *bgp,
815 int id, u32 interval)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300816{
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400817 int ret = ti_bandgap_validate(bgp, id);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300818 if (ret)
J Keerthy58bccd02013-04-01 12:04:42 -0400819 goto exit;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300820
J Keerthy58bccd02013-04-01 12:04:42 -0400821 if (!TI_BANDGAP_HAS(bgp, COUNTER) &&
822 !TI_BANDGAP_HAS(bgp, COUNTER_DELAY)) {
823 ret = -ENOTSUPP;
824 goto exit;
825 }
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300826
J Keerthy58bccd02013-04-01 12:04:42 -0400827 if (TI_BANDGAP_HAS(bgp, COUNTER)) {
828 ti_bandgap_write_counter(bgp, id, interval);
829 goto exit;
830 }
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300831
J Keerthy58bccd02013-04-01 12:04:42 -0400832 ret = ti_bandgap_write_counter_delay(bgp, id, interval);
833exit:
834 return ret;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300835}
836
837/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400838 * ti_bandgap_read_temperature() - report current temperature
Eduardo Valentin61603af2013-03-19 10:54:25 -0400839 * @bgp: pointer to bandgap instance
840 * @id: sensor id
841 * @temperature: resulting temperature
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300842 *
Nishanth Menon169e8d02013-04-01 12:04:34 -0400843 * Return: 0 on success or the proper error code
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300844 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400845int ti_bandgap_read_temperature(struct ti_bandgap *bgp, int id,
846 int *temperature)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300847{
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300848 u32 temp;
849 int ret;
850
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400851 ret = ti_bandgap_validate(bgp, id);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300852 if (ret)
853 return ret;
854
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400855 spin_lock(&bgp->lock);
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400856 temp = ti_bandgap_read_temp(bgp, id);
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400857 spin_unlock(&bgp->lock);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300858
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400859 ret |= ti_bandgap_adc_to_mcelsius(bgp, temp, &temp);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300860 if (ret)
861 return -EIO;
862
863 *temperature = temp;
864
865 return 0;
866}
867
868/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400869 * ti_bandgap_set_sensor_data() - helper function to store thermal
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300870 * framework related data.
Eduardo Valentin61603af2013-03-19 10:54:25 -0400871 * @bgp: pointer to bandgap instance
872 * @id: sensor id
873 * @data: thermal framework related data to be stored
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300874 *
Nishanth Menon169e8d02013-04-01 12:04:34 -0400875 * Return: 0 on success or the proper error code
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300876 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400877int ti_bandgap_set_sensor_data(struct ti_bandgap *bgp, int id, void *data)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300878{
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400879 int ret = ti_bandgap_validate(bgp, id);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300880 if (ret)
881 return ret;
882
Eduardo Valentin9879b2c2013-03-19 10:54:23 -0400883 bgp->regval[id].data = data;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300884
885 return 0;
886}
887
888/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400889 * ti_bandgap_get_sensor_data() - helper function to get thermal
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300890 * framework related data.
Eduardo Valentin61603af2013-03-19 10:54:25 -0400891 * @bgp: pointer to bandgap instance
892 * @id: sensor id
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300893 *
Nishanth Menon169e8d02013-04-01 12:04:34 -0400894 * Return: data stored by set function with sensor id on success or NULL
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300895 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400896void *ti_bandgap_get_sensor_data(struct ti_bandgap *bgp, int id)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300897{
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400898 int ret = ti_bandgap_validate(bgp, id);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300899 if (ret)
900 return ERR_PTR(ret);
901
Eduardo Valentin9879b2c2013-03-19 10:54:23 -0400902 return bgp->regval[id].data;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300903}
904
Eduardo Valentine195aba2013-03-15 09:00:22 -0400905/*** Helper functions used during device initialization ***/
906
Eduardo Valentin31102a72013-03-15 09:00:26 -0400907/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400908 * ti_bandgap_force_single_read() - executes 1 single ADC conversion
909 * @bgp: pointer to struct ti_bandgap
Eduardo Valentin31102a72013-03-15 09:00:26 -0400910 * @id: sensor id which it is desired to read 1 temperature
911 *
912 * Used to initialize the conversion state machine and set it to a valid
913 * state. Called during device initialization and context restore events.
Nishanth Menon169e8d02013-04-01 12:04:34 -0400914 *
915 * Return: 0
Eduardo Valentin31102a72013-03-15 09:00:26 -0400916 */
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300917static int
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400918ti_bandgap_force_single_read(struct ti_bandgap *bgp, int id)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300919{
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300920 u32 temp = 0, counter = 1000;
921
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300922 /* Select single conversion mode */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400923 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400924 RMW_BITS(bgp, id, bgap_mode_ctrl, mode_ctrl_mask, 0);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300925
926 /* Start of Conversion = 1 */
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400927 RMW_BITS(bgp, id, temp_sensor_ctrl, bgap_soc_mask, 1);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300928 /* Wait until DTEMP is updated */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400929 temp = ti_bandgap_read_temp(bgp, id);
Eduardo Valentin194a54f2013-02-26 18:53:33 -0400930
931 while ((temp == 0) && --counter)
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400932 temp = ti_bandgap_read_temp(bgp, id);
Eduardo Valentind3c291a2013-03-15 08:59:55 -0400933 /* REVISIT: Check correct condition for end of conversion */
Eduardo Valentin194a54f2013-02-26 18:53:33 -0400934
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300935 /* Start of Conversion = 0 */
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400936 RMW_BITS(bgp, id, temp_sensor_ctrl, bgap_soc_mask, 0);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300937
938 return 0;
939}
940
941/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400942 * ti_bandgap_set_continous_mode() - One time enabling of continuous mode
943 * @bgp: pointer to struct ti_bandgap
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300944 *
Eduardo Valentina84b6f42013-03-15 09:00:25 -0400945 * Call this function only if HAS(MODE_CONFIG) is set. As this driver may
946 * be used for junction temperature monitoring, it is desirable that the
947 * sensors are operational all the time, so that alerts are generated
948 * properly.
Nishanth Menon169e8d02013-04-01 12:04:34 -0400949 *
950 * Return: 0
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300951 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400952static int ti_bandgap_set_continuous_mode(struct ti_bandgap *bgp)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300953{
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300954 int i;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300955
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400956 for (i = 0; i < bgp->conf->sensor_count; i++) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300957 /* Perform a single read just before enabling continuous */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400958 ti_bandgap_force_single_read(bgp, i);
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400959 RMW_BITS(bgp, i, bgap_mode_ctrl, mode_ctrl_mask, 1);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300960 }
961
962 return 0;
963}
964
Eduardo Valentind3790b32013-03-15 09:00:30 -0400965/**
J Keerthy2f440b02013-04-01 12:04:45 -0400966 * ti_bandgap_get_trend() - To fetch the temperature trend of a sensor
967 * @bgp: pointer to struct ti_bandgap
968 * @id: id of the individual sensor
969 * @trend: Pointer to trend.
970 *
971 * This function needs to be called to fetch the temperature trend of a
972 * Particular sensor. The function computes the difference in temperature
973 * w.r.t time. For the bandgaps with built in history buffer the temperatures
974 * are read from the buffer and for those without the Buffer -ENOTSUPP is
975 * returned.
976 *
977 * Return: 0 if no error, else return corresponding error. If no
978 * error then the trend value is passed on to trend parameter
979 */
980int ti_bandgap_get_trend(struct ti_bandgap *bgp, int id, int *trend)
981{
982 struct temp_sensor_registers *tsr;
983 u32 temp1, temp2, reg1, reg2;
984 int t1, t2, interval, ret = 0;
985
986 ret = ti_bandgap_validate(bgp, id);
987 if (ret)
988 goto exit;
989
990 if (!TI_BANDGAP_HAS(bgp, HISTORY_BUFFER) ||
991 !TI_BANDGAP_HAS(bgp, FREEZE_BIT)) {
992 ret = -ENOTSUPP;
993 goto exit;
994 }
995
Eduardo Valentinba0049e2013-06-07 19:13:13 +0000996 spin_lock(&bgp->lock);
997
J Keerthy2f440b02013-04-01 12:04:45 -0400998 tsr = bgp->conf->sensors[id].registers;
999
1000 /* Freeze and read the last 2 valid readings */
Eduardo Valentinba0049e2013-06-07 19:13:13 +00001001 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 1);
J Keerthy2f440b02013-04-01 12:04:45 -04001002 reg1 = tsr->ctrl_dtemp_1;
1003 reg2 = tsr->ctrl_dtemp_2;
1004
1005 /* read temperature from history buffer */
1006 temp1 = ti_bandgap_readl(bgp, reg1);
1007 temp1 &= tsr->bgap_dtemp_mask;
1008
1009 temp2 = ti_bandgap_readl(bgp, reg2);
1010 temp2 &= tsr->bgap_dtemp_mask;
1011
1012 /* Convert from adc values to mCelsius temperature */
1013 ret = ti_bandgap_adc_to_mcelsius(bgp, temp1, &t1);
1014 if (ret)
Eduardo Valentinba0049e2013-06-07 19:13:13 +00001015 goto unfreeze;
J Keerthy2f440b02013-04-01 12:04:45 -04001016
1017 ret = ti_bandgap_adc_to_mcelsius(bgp, temp2, &t2);
1018 if (ret)
Eduardo Valentinba0049e2013-06-07 19:13:13 +00001019 goto unfreeze;
J Keerthy2f440b02013-04-01 12:04:45 -04001020
1021 /* Fetch the update interval */
1022 ret = ti_bandgap_read_update_interval(bgp, id, &interval);
Ranganath Krishnane838ff82013-08-23 11:08:23 -05001023 if (ret)
Eduardo Valentinba0049e2013-06-07 19:13:13 +00001024 goto unfreeze;
J Keerthy2f440b02013-04-01 12:04:45 -04001025
Ranganath Krishnane838ff82013-08-23 11:08:23 -05001026 /* Set the interval to 1 ms if bandgap counter delay is not set */
1027 if (interval == 0)
1028 interval = 1;
1029
J Keerthy2f440b02013-04-01 12:04:45 -04001030 *trend = (t1 - t2) / interval;
1031
1032 dev_dbg(bgp->dev, "The temperatures are t1 = %d and t2 = %d and trend =%d\n",
1033 t1, t2, *trend);
1034
Eduardo Valentinba0049e2013-06-07 19:13:13 +00001035unfreeze:
1036 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 0);
1037 spin_unlock(&bgp->lock);
J Keerthy2f440b02013-04-01 12:04:45 -04001038exit:
1039 return ret;
1040}
1041
1042/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001043 * ti_bandgap_tshut_init() - setup and initialize tshut handling
1044 * @bgp: pointer to struct ti_bandgap
Eduardo Valentind3790b32013-03-15 09:00:30 -04001045 * @pdev: pointer to device struct platform_device
1046 *
1047 * Call this function only in case the bandgap features HAS(TSHUT).
1048 * In this case, the driver needs to handle the TSHUT signal as an IRQ.
1049 * The IRQ is wired as a GPIO, and for this purpose, it is required
1050 * to specify which GPIO line is used. TSHUT IRQ is fired anytime
1051 * one of the bandgap sensors violates the TSHUT high/hot threshold.
1052 * And in that case, the system must go off.
Nishanth Menon169e8d02013-04-01 12:04:34 -04001053 *
1054 * Return: 0 if no error, else error status
Eduardo Valentind3790b32013-03-15 09:00:30 -04001055 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001056static int ti_bandgap_tshut_init(struct ti_bandgap *bgp,
1057 struct platform_device *pdev)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001058{
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001059 int gpio_nr = bgp->tshut_gpio;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001060 int status;
1061
1062 /* Request for gpio_86 line */
1063 status = gpio_request(gpio_nr, "tshut");
1064 if (status < 0) {
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001065 dev_err(bgp->dev, "Could not request for TSHUT GPIO:%i\n", 86);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001066 return status;
1067 }
1068 status = gpio_direction_input(gpio_nr);
1069 if (status) {
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001070 dev_err(bgp->dev, "Cannot set input TSHUT GPIO %d\n", gpio_nr);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001071 return status;
1072 }
1073
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001074 status = request_irq(gpio_to_irq(gpio_nr), ti_bandgap_tshut_irq_handler,
1075 IRQF_TRIGGER_RISING, "tshut", NULL);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001076 if (status) {
1077 gpio_free(gpio_nr);
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001078 dev_err(bgp->dev, "request irq failed for TSHUT");
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001079 }
1080
1081 return 0;
1082}
1083
Eduardo Valentin094b8ac2013-03-15 09:00:31 -04001084/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001085 * ti_bandgap_alert_init() - setup and initialize talert handling
1086 * @bgp: pointer to struct ti_bandgap
Eduardo Valentin094b8ac2013-03-15 09:00:31 -04001087 * @pdev: pointer to device struct platform_device
1088 *
1089 * Call this function only in case the bandgap features HAS(TALERT).
1090 * In this case, the driver needs to handle the TALERT signals as an IRQs.
1091 * TALERT is a normal IRQ and it is fired any time thresholds (hot or cold)
1092 * are violated. In these situation, the driver must reprogram the thresholds,
1093 * accordingly to specified policy.
Nishanth Menon169e8d02013-04-01 12:04:34 -04001094 *
1095 * Return: 0 if no error, else return corresponding error.
Eduardo Valentin094b8ac2013-03-15 09:00:31 -04001096 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001097static int ti_bandgap_talert_init(struct ti_bandgap *bgp,
1098 struct platform_device *pdev)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001099{
1100 int ret;
1101
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001102 bgp->irq = platform_get_irq(pdev, 0);
1103 if (bgp->irq < 0) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001104 dev_err(&pdev->dev, "get_irq failed\n");
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001105 return bgp->irq;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001106 }
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001107 ret = request_threaded_irq(bgp->irq, NULL,
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001108 ti_bandgap_talert_irq_handler,
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001109 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001110 "talert", bgp);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001111 if (ret) {
1112 dev_err(&pdev->dev, "Request threaded irq failed.\n");
1113 return ret;
1114 }
1115
1116 return 0;
1117}
1118
Eduardo Valentin61603af2013-03-19 10:54:25 -04001119static const struct of_device_id of_ti_bandgap_match[];
Eduardo Valentine9b6f8c2013-03-15 09:00:32 -04001120/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001121 * ti_bandgap_build() - parse DT and setup a struct ti_bandgap
Eduardo Valentine9b6f8c2013-03-15 09:00:32 -04001122 * @pdev: pointer to device struct platform_device
1123 *
1124 * Used to read the device tree properties accordingly to the bandgap
1125 * matching version. Based on bandgap version and its capabilities it
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001126 * will build a struct ti_bandgap out of the required DT entries.
Nishanth Menon169e8d02013-04-01 12:04:34 -04001127 *
1128 * Return: valid bandgap structure if successful, else returns ERR_PTR
1129 * return value must be verified with IS_ERR.
Eduardo Valentine9b6f8c2013-03-15 09:00:32 -04001130 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001131static struct ti_bandgap *ti_bandgap_build(struct platform_device *pdev)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001132{
1133 struct device_node *node = pdev->dev.of_node;
1134 const struct of_device_id *of_id;
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001135 struct ti_bandgap *bgp;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001136 struct resource *res;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001137 int i;
1138
1139 /* just for the sake */
1140 if (!node) {
1141 dev_err(&pdev->dev, "no platform information available\n");
1142 return ERR_PTR(-EINVAL);
1143 }
1144
Eduardo Valentinf6843562013-03-19 10:54:24 -04001145 bgp = devm_kzalloc(&pdev->dev, sizeof(*bgp), GFP_KERNEL);
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001146 if (!bgp) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001147 dev_err(&pdev->dev, "Unable to allocate mem for driver ref\n");
1148 return ERR_PTR(-ENOMEM);
1149 }
1150
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001151 of_id = of_match_device(of_ti_bandgap_match, &pdev->dev);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001152 if (of_id)
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001153 bgp->conf = of_id->data;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001154
Eduardo Valentin9879b2c2013-03-19 10:54:23 -04001155 /* register shadow for context save and restore */
1156 bgp->regval = devm_kzalloc(&pdev->dev, sizeof(*bgp->regval) *
1157 bgp->conf->sensor_count, GFP_KERNEL);
1158 if (!bgp) {
1159 dev_err(&pdev->dev, "Unable to allocate mem for driver ref\n");
1160 return ERR_PTR(-ENOMEM);
1161 }
1162
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001163 i = 0;
1164 do {
1165 void __iomem *chunk;
1166
1167 res = platform_get_resource(pdev, IORESOURCE_MEM, i);
1168 if (!res)
1169 break;
Thierry Reding97f4be602013-01-21 11:09:19 +01001170 chunk = devm_ioremap_resource(&pdev->dev, res);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001171 if (i == 0)
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001172 bgp->base = chunk;
Thierry Reding97f4be602013-01-21 11:09:19 +01001173 if (IS_ERR(chunk))
1174 return ERR_CAST(chunk);
Eduardo Valentin24796e12013-03-15 08:59:53 -04001175
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001176 i++;
1177 } while (res);
1178
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001179 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
Eduardo Valentin57d16172013-06-07 11:11:53 -04001180 bgp->tshut_gpio = of_get_gpio(node, 0);
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001181 if (!gpio_is_valid(bgp->tshut_gpio)) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001182 dev_err(&pdev->dev, "invalid gpio for tshut (%d)\n",
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001183 bgp->tshut_gpio);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001184 return ERR_PTR(-EINVAL);
1185 }
1186 }
1187
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001188 return bgp;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001189}
1190
Eduardo Valentinf91ddfe2013-03-15 09:00:23 -04001191/*** Device driver call backs ***/
1192
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001193static
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001194int ti_bandgap_probe(struct platform_device *pdev)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001195{
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001196 struct ti_bandgap *bgp;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001197 int clk_rate, ret = 0, i;
1198
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001199 bgp = ti_bandgap_build(pdev);
Eduardo Valentin0c12b5a2013-05-29 15:07:43 +00001200 if (IS_ERR(bgp)) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001201 dev_err(&pdev->dev, "failed to fetch platform data\n");
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001202 return PTR_ERR(bgp);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001203 }
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001204 bgp->dev = &pdev->dev;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001205
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001206 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
1207 ret = ti_bandgap_tshut_init(bgp, pdev);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001208 if (ret) {
1209 dev_err(&pdev->dev,
1210 "failed to initialize system tshut IRQ\n");
1211 return ret;
1212 }
1213 }
1214
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001215 bgp->fclock = clk_get(NULL, bgp->conf->fclock_name);
Eduardo Valentin0c12b5a2013-05-29 15:07:43 +00001216 ret = IS_ERR(bgp->fclock);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001217 if (ret) {
1218 dev_err(&pdev->dev, "failed to request fclock reference\n");
Eduardo Valentin0c12b5a2013-05-29 15:07:43 +00001219 ret = PTR_ERR(bgp->fclock);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001220 goto free_irqs;
1221 }
1222
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001223 bgp->div_clk = clk_get(NULL, bgp->conf->div_ck_name);
Eduardo Valentin0c12b5a2013-05-29 15:07:43 +00001224 ret = IS_ERR(bgp->div_clk);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001225 if (ret) {
1226 dev_err(&pdev->dev,
1227 "failed to request div_ts_ck clock ref\n");
Eduardo Valentin0c12b5a2013-05-29 15:07:43 +00001228 ret = PTR_ERR(bgp->div_clk);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001229 goto free_irqs;
1230 }
1231
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001232 for (i = 0; i < bgp->conf->sensor_count; i++) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001233 struct temp_sensor_registers *tsr;
1234 u32 val;
1235
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001236 tsr = bgp->conf->sensors[i].registers;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001237 /*
1238 * check if the efuse has a non-zero value if not
1239 * it is an untrimmed sample and the temperatures
1240 * may not be accurate
1241 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001242 val = ti_bandgap_readl(bgp, tsr->bgap_efuse);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001243 if (ret || !val)
1244 dev_info(&pdev->dev,
1245 "Non-trimmed BGAP, Temp not accurate\n");
1246 }
1247
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001248 clk_rate = clk_round_rate(bgp->div_clk,
1249 bgp->conf->sensors[0].ts_data->max_freq);
1250 if (clk_rate < bgp->conf->sensors[0].ts_data->min_freq ||
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001251 clk_rate == 0xffffffff) {
1252 ret = -ENODEV;
1253 dev_err(&pdev->dev, "wrong clock rate (%d)\n", clk_rate);
1254 goto put_clks;
1255 }
1256
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001257 ret = clk_set_rate(bgp->div_clk, clk_rate);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001258 if (ret)
1259 dev_err(&pdev->dev, "Cannot re-set clock rate. Continuing\n");
1260
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001261 bgp->clk_rate = clk_rate;
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001262 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001263 clk_prepare_enable(bgp->fclock);
Radhesh Fadnis6c9c1d62013-02-26 18:53:25 -04001264
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001265
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001266 spin_lock_init(&bgp->lock);
1267 bgp->dev = &pdev->dev;
1268 platform_set_drvdata(pdev, bgp);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001269
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001270 ti_bandgap_power(bgp, true);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001271
1272 /* Set default counter to 1 for now */
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001273 if (TI_BANDGAP_HAS(bgp, COUNTER))
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001274 for (i = 0; i < bgp->conf->sensor_count; i++)
1275 RMW_BITS(bgp, i, bgap_counter, counter_mask, 1);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001276
Eduardo Valentind3c291a2013-03-15 08:59:55 -04001277 /* Set default thresholds for alert and shutdown */
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001278 for (i = 0; i < bgp->conf->sensor_count; i++) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001279 struct temp_sensor_data *ts_data;
1280
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001281 ts_data = bgp->conf->sensors[i].ts_data;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001282
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001283 if (TI_BANDGAP_HAS(bgp, TALERT)) {
Eduardo Valentind3c291a2013-03-15 08:59:55 -04001284 /* Set initial Talert thresholds */
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001285 RMW_BITS(bgp, i, bgap_threshold,
Eduardo Valentind3c291a2013-03-15 08:59:55 -04001286 threshold_tcold_mask, ts_data->t_cold);
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001287 RMW_BITS(bgp, i, bgap_threshold,
Eduardo Valentind3c291a2013-03-15 08:59:55 -04001288 threshold_thot_mask, ts_data->t_hot);
1289 /* Enable the alert events */
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001290 RMW_BITS(bgp, i, bgap_mask_ctrl, mask_hot_mask, 1);
1291 RMW_BITS(bgp, i, bgap_mask_ctrl, mask_cold_mask, 1);
Eduardo Valentind3c291a2013-03-15 08:59:55 -04001292 }
1293
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001294 if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG)) {
Eduardo Valentind3c291a2013-03-15 08:59:55 -04001295 /* Set initial Tshut thresholds */
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001296 RMW_BITS(bgp, i, tshut_threshold,
Eduardo Valentind3c291a2013-03-15 08:59:55 -04001297 tshut_hot_mask, ts_data->tshut_hot);
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001298 RMW_BITS(bgp, i, tshut_threshold,
Eduardo Valentind3c291a2013-03-15 08:59:55 -04001299 tshut_cold_mask, ts_data->tshut_cold);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001300 }
1301 }
1302
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001303 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
1304 ti_bandgap_set_continuous_mode(bgp);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001305
1306 /* Set .250 seconds time as default counter */
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001307 if (TI_BANDGAP_HAS(bgp, COUNTER))
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001308 for (i = 0; i < bgp->conf->sensor_count; i++)
1309 RMW_BITS(bgp, i, bgap_counter, counter_mask,
1310 bgp->clk_rate / 4);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001311
1312 /* Every thing is good? Then expose the sensors */
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001313 for (i = 0; i < bgp->conf->sensor_count; i++) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001314 char *domain;
1315
Eduardo Valentinf1553332013-04-08 08:19:13 -04001316 if (bgp->conf->sensors[i].register_cooling) {
1317 ret = bgp->conf->sensors[i].register_cooling(bgp, i);
1318 if (ret)
1319 goto remove_sensors;
1320 }
Eduardo Valentin04a4d102012-09-11 19:06:55 +03001321
Eduardo Valentinf1553332013-04-08 08:19:13 -04001322 if (bgp->conf->expose_sensor) {
1323 domain = bgp->conf->sensors[i].domain;
1324 ret = bgp->conf->expose_sensor(bgp, i, domain);
1325 if (ret)
1326 goto remove_last_cooling;
1327 }
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001328 }
1329
1330 /*
1331 * Enable the Interrupts once everything is set. Otherwise irq handler
1332 * might be called as soon as it is enabled where as rest of framework
1333 * is still getting initialised.
1334 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001335 if (TI_BANDGAP_HAS(bgp, TALERT)) {
1336 ret = ti_bandgap_talert_init(bgp, pdev);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001337 if (ret) {
1338 dev_err(&pdev->dev, "failed to initialize Talert IRQ\n");
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001339 i = bgp->conf->sensor_count;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001340 goto disable_clk;
1341 }
1342 }
1343
1344 return 0;
1345
Eduardo Valentinf1553332013-04-08 08:19:13 -04001346remove_last_cooling:
1347 if (bgp->conf->sensors[i].unregister_cooling)
1348 bgp->conf->sensors[i].unregister_cooling(bgp, i);
1349remove_sensors:
1350 for (i--; i >= 0; i--) {
1351 if (bgp->conf->sensors[i].unregister_cooling)
1352 bgp->conf->sensors[i].unregister_cooling(bgp, i);
1353 if (bgp->conf->remove_sensor)
1354 bgp->conf->remove_sensor(bgp, i);
1355 }
1356 ti_bandgap_power(bgp, false);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001357disable_clk:
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001358 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001359 clk_disable_unprepare(bgp->fclock);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001360put_clks:
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001361 clk_put(bgp->fclock);
1362 clk_put(bgp->div_clk);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001363free_irqs:
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001364 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001365 free_irq(gpio_to_irq(bgp->tshut_gpio), NULL);
1366 gpio_free(bgp->tshut_gpio);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001367 }
1368
1369 return ret;
1370}
1371
1372static
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001373int ti_bandgap_remove(struct platform_device *pdev)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001374{
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001375 struct ti_bandgap *bgp = platform_get_drvdata(pdev);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001376 int i;
1377
1378 /* First thing is to remove sensor interfaces */
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001379 for (i = 0; i < bgp->conf->sensor_count; i++) {
Eduardo Valentin262235b2013-04-08 08:19:14 -04001380 if (bgp->conf->sensors[i].unregister_cooling)
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001381 bgp->conf->sensors[i].unregister_cooling(bgp, i);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001382
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001383 if (bgp->conf->remove_sensor)
1384 bgp->conf->remove_sensor(bgp, i);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001385 }
1386
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001387 ti_bandgap_power(bgp, false);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001388
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001389 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001390 clk_disable_unprepare(bgp->fclock);
1391 clk_put(bgp->fclock);
1392 clk_put(bgp->div_clk);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001393
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001394 if (TI_BANDGAP_HAS(bgp, TALERT))
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001395 free_irq(bgp->irq, bgp);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001396
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001397 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001398 free_irq(gpio_to_irq(bgp->tshut_gpio), NULL);
1399 gpio_free(bgp->tshut_gpio);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001400 }
1401
1402 return 0;
1403}
1404
1405#ifdef CONFIG_PM
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001406static int ti_bandgap_save_ctxt(struct ti_bandgap *bgp)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001407{
1408 int i;
1409
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001410 for (i = 0; i < bgp->conf->sensor_count; i++) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001411 struct temp_sensor_registers *tsr;
1412 struct temp_sensor_regval *rval;
1413
Eduardo Valentin9879b2c2013-03-19 10:54:23 -04001414 rval = &bgp->regval[i];
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001415 tsr = bgp->conf->sensors[i].registers;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001416
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001417 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
1418 rval->bg_mode_ctrl = ti_bandgap_readl(bgp,
J Keerthy76d2cd32012-09-11 19:06:52 +03001419 tsr->bgap_mode_ctrl);
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001420 if (TI_BANDGAP_HAS(bgp, COUNTER))
1421 rval->bg_counter = ti_bandgap_readl(bgp,
J Keerthy76d2cd32012-09-11 19:06:52 +03001422 tsr->bgap_counter);
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001423 if (TI_BANDGAP_HAS(bgp, TALERT)) {
1424 rval->bg_threshold = ti_bandgap_readl(bgp,
J Keerthy76d2cd32012-09-11 19:06:52 +03001425 tsr->bgap_threshold);
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001426 rval->bg_ctrl = ti_bandgap_readl(bgp,
J Keerthy76d2cd32012-09-11 19:06:52 +03001427 tsr->bgap_mask_ctrl);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001428 }
1429
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001430 if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG))
1431 rval->tshut_threshold = ti_bandgap_readl(bgp,
J Keerthy76d2cd32012-09-11 19:06:52 +03001432 tsr->tshut_threshold);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001433 }
1434
1435 return 0;
1436}
1437
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001438static int ti_bandgap_restore_ctxt(struct ti_bandgap *bgp)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001439{
1440 int i;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001441
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001442 for (i = 0; i < bgp->conf->sensor_count; i++) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001443 struct temp_sensor_registers *tsr;
1444 struct temp_sensor_regval *rval;
1445 u32 val = 0;
1446
Eduardo Valentin9879b2c2013-03-19 10:54:23 -04001447 rval = &bgp->regval[i];
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001448 tsr = bgp->conf->sensors[i].registers;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001449
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001450 if (TI_BANDGAP_HAS(bgp, COUNTER))
1451 val = ti_bandgap_readl(bgp, tsr->bgap_counter);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001452
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001453 if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG))
1454 ti_bandgap_writel(bgp, rval->tshut_threshold,
1455 tsr->tshut_threshold);
Radhesh Fadnisb87ea752012-11-13 14:10:04 -04001456 /* Force immediate temperature measurement and update
1457 * of the DTEMP field
1458 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001459 ti_bandgap_force_single_read(bgp, i);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001460
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001461 if (TI_BANDGAP_HAS(bgp, COUNTER))
1462 ti_bandgap_writel(bgp, rval->bg_counter,
1463 tsr->bgap_counter);
1464 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
1465 ti_bandgap_writel(bgp, rval->bg_mode_ctrl,
1466 tsr->bgap_mode_ctrl);
1467 if (TI_BANDGAP_HAS(bgp, TALERT)) {
1468 ti_bandgap_writel(bgp, rval->bg_threshold,
1469 tsr->bgap_threshold);
1470 ti_bandgap_writel(bgp, rval->bg_ctrl,
1471 tsr->bgap_mask_ctrl);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001472 }
1473 }
1474
1475 return 0;
1476}
1477
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001478static int ti_bandgap_suspend(struct device *dev)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001479{
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001480 struct ti_bandgap *bgp = dev_get_drvdata(dev);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001481 int err;
1482
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001483 err = ti_bandgap_save_ctxt(bgp);
1484 ti_bandgap_power(bgp, false);
Radhesh Fadnis6c9c1d62013-02-26 18:53:25 -04001485
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001486 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001487 clk_disable_unprepare(bgp->fclock);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001488
1489 return err;
1490}
1491
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001492static int ti_bandgap_resume(struct device *dev)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001493{
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001494 struct ti_bandgap *bgp = dev_get_drvdata(dev);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001495
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001496 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001497 clk_prepare_enable(bgp->fclock);
Radhesh Fadnis6c9c1d62013-02-26 18:53:25 -04001498
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001499 ti_bandgap_power(bgp, true);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001500
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001501 return ti_bandgap_restore_ctxt(bgp);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001502}
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001503static const struct dev_pm_ops ti_bandgap_dev_pm_ops = {
1504 SET_SYSTEM_SLEEP_PM_OPS(ti_bandgap_suspend,
1505 ti_bandgap_resume)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001506};
1507
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001508#define DEV_PM_OPS (&ti_bandgap_dev_pm_ops)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001509#else
1510#define DEV_PM_OPS NULL
1511#endif
1512
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001513static const struct of_device_id of_ti_bandgap_match[] = {
Eduardo Valentin1a312702012-07-12 19:02:31 +03001514#ifdef CONFIG_OMAP4_THERMAL
1515 {
1516 .compatible = "ti,omap4430-bandgap",
1517 .data = (void *)&omap4430_data,
1518 },
1519 {
1520 .compatible = "ti,omap4460-bandgap",
1521 .data = (void *)&omap4460_data,
1522 },
1523 {
1524 .compatible = "ti,omap4470-bandgap",
1525 .data = (void *)&omap4470_data,
1526 },
1527#endif
Eduardo Valentin949f5a52012-07-12 19:02:32 +03001528#ifdef CONFIG_OMAP5_THERMAL
1529 {
1530 .compatible = "ti,omap5430-bandgap",
1531 .data = (void *)&omap5430_data,
1532 },
1533#endif
Eduardo Valentin25870e62013-05-29 15:07:45 +00001534#ifdef CONFIG_DRA752_THERMAL
1535 {
1536 .compatible = "ti,dra752-bandgap",
1537 .data = (void *)&dra752_data,
1538 },
1539#endif
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001540 /* Sentinel */
1541 { },
1542};
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001543MODULE_DEVICE_TABLE(of, of_ti_bandgap_match);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001544
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001545static struct platform_driver ti_bandgap_sensor_driver = {
1546 .probe = ti_bandgap_probe,
1547 .remove = ti_bandgap_remove,
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001548 .driver = {
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001549 .name = "ti-soc-thermal",
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001550 .pm = DEV_PM_OPS,
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001551 .of_match_table = of_ti_bandgap_match,
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001552 },
1553};
1554
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001555module_platform_driver(ti_bandgap_sensor_driver);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001556
1557MODULE_DESCRIPTION("OMAP4+ bandgap temperature sensor driver");
1558MODULE_LICENSE("GPL v2");
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001559MODULE_ALIAS("platform:ti-soc-thermal");
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001560MODULE_AUTHOR("Texas Instrument Inc.");